Update to drupal-org-drupal 8.0.0-rc2. For more information, see https://www.drupal.org/node/2598668
This commit is contained in:
parent
f32e58e4b1
commit
8e18df8c36
3062 changed files with 15044 additions and 172506 deletions
|
@ -1,730 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony CMF package.
|
||||
*
|
||||
* (c) 2011-2014 Symfony CMF
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Cmf\Component\Routing\Tests\Routing;
|
||||
|
||||
use Symfony\Cmf\Component\Routing\VersatileGeneratorInterface;
|
||||
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
|
||||
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
|
||||
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
|
||||
use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
||||
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
use Symfony\Cmf\Component\Routing\ChainRouter;
|
||||
use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
|
||||
class ChainRouterTest extends CmfUnitTestCase
|
||||
{
|
||||
/**
|
||||
* @var ChainRouter
|
||||
*/
|
||||
private $router;
|
||||
/**
|
||||
* @var RequestContext|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $context;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->router = new ChainRouter($this->getMock('Psr\Log\LoggerInterface'));
|
||||
$this->context = $this->getMock('Symfony\Component\Routing\RequestContext');
|
||||
}
|
||||
|
||||
public function testPriority()
|
||||
{
|
||||
$this->assertEquals(array(), $this->router->all());
|
||||
|
||||
list($low, $high) = $this->createRouterMocks();
|
||||
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 100);
|
||||
|
||||
$this->assertEquals(array(
|
||||
$high,
|
||||
$low,
|
||||
), $this->router->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* Routers are supposed to be sorted only once.
|
||||
* This test will check that by trying to get all routers several times.
|
||||
*
|
||||
* @covers \Symfony\Cmf\Component\Routing\ChainRouter::sortRouters
|
||||
* @covers \Symfony\Cmf\Component\Routing\ChainRouter::all
|
||||
*/
|
||||
public function testSortRouters()
|
||||
{
|
||||
list($low, $medium, $high) = $this->createRouterMocks();
|
||||
// We're using a mock here and not $this->router because we need to ensure that the sorting operation is done only once.
|
||||
/** @var $router ChainRouter|\PHPUnit_Framework_MockObject_MockObject */
|
||||
$router = $this->buildMock('Symfony\Cmf\Component\Routing\ChainRouter', array('sortRouters'));
|
||||
$router
|
||||
->expects($this->once())
|
||||
->method('sortRouters')
|
||||
->will(
|
||||
$this->returnValue(
|
||||
array($high, $medium, $low)
|
||||
)
|
||||
)
|
||||
;
|
||||
|
||||
$router->add($low, 10);
|
||||
$router->add($medium, 50);
|
||||
$router->add($high, 100);
|
||||
$expectedSortedRouters = array($high, $medium, $low);
|
||||
// Let's get all routers 5 times, we should only sort once.
|
||||
for ($i = 0; $i < 5; ++$i) {
|
||||
$this->assertSame($expectedSortedRouters, $router->all());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This test ensures that if a router is being added on the fly, the sorting is reset.
|
||||
*
|
||||
* @covers \Symfony\Cmf\Component\Routing\ChainRouter::sortRouters
|
||||
* @covers \Symfony\Cmf\Component\Routing\ChainRouter::all
|
||||
* @covers \Symfony\Cmf\Component\Routing\ChainRouter::add
|
||||
*/
|
||||
public function testReSortRouters()
|
||||
{
|
||||
list($low, $medium, $high) = $this->createRouterMocks();
|
||||
$highest = clone $high;
|
||||
// We're using a mock here and not $this->router because we need to ensure that the sorting operation is done only once.
|
||||
/** @var $router ChainRouter|\PHPUnit_Framework_MockObject_MockObject */
|
||||
$router = $this->buildMock('Symfony\Cmf\Component\Routing\ChainRouter', array('sortRouters'));
|
||||
$router
|
||||
->expects($this->at(0))
|
||||
->method('sortRouters')
|
||||
->will(
|
||||
$this->returnValue(
|
||||
array($high, $medium, $low)
|
||||
)
|
||||
)
|
||||
;
|
||||
// The second time sortRouters() is called, we're supposed to get the newly added router ($highest)
|
||||
$router
|
||||
->expects($this->at(1))
|
||||
->method('sortRouters')
|
||||
->will(
|
||||
$this->returnValue(
|
||||
array($highest, $high, $medium, $low)
|
||||
)
|
||||
)
|
||||
;
|
||||
|
||||
$router->add($low, 10);
|
||||
$router->add($medium, 50);
|
||||
$router->add($high, 100);
|
||||
$this->assertSame(array($high, $medium, $low), $router->all());
|
||||
|
||||
// Now adding another router on the fly, sorting must have been reset
|
||||
$router->add($highest, 101);
|
||||
$this->assertSame(array($highest, $high, $medium, $low), $router->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* context must be propagated to chained routers and be stored locally
|
||||
*/
|
||||
public function testContext()
|
||||
{
|
||||
list($low, $high) = $this->createRouterMocks();
|
||||
|
||||
$low
|
||||
->expects($this->once())
|
||||
->method('setContext')
|
||||
->with($this->context)
|
||||
;
|
||||
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('setContext')
|
||||
->with($this->context)
|
||||
;
|
||||
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 100);
|
||||
|
||||
$this->router->setContext($this->context);
|
||||
$this->assertSame($this->context, $this->router->getContext());
|
||||
}
|
||||
|
||||
/**
|
||||
* context must be propagated also when routers are added after context is set
|
||||
*/
|
||||
public function testContextOrder()
|
||||
{
|
||||
list($low, $high) = $this->createRouterMocks();
|
||||
|
||||
$low
|
||||
->expects($this->once())
|
||||
->method('setContext')
|
||||
->with($this->context)
|
||||
;
|
||||
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('setContext')
|
||||
->with($this->context)
|
||||
;
|
||||
|
||||
$this->router->setContext($this->context);
|
||||
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 100);
|
||||
|
||||
$this->router->all();
|
||||
|
||||
$this->assertSame($this->context, $this->router->getContext());
|
||||
}
|
||||
|
||||
/**
|
||||
* The first usable match is used, no further routers are queried once a match is found
|
||||
*/
|
||||
public function testMatch()
|
||||
{
|
||||
$url = '/test';
|
||||
list($lower, $low, $high) = $this->createRouterMocks();
|
||||
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->throwException(new \Symfony\Component\Routing\Exception\ResourceNotFoundException))
|
||||
;
|
||||
$low
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->returnValue(array('test')))
|
||||
;
|
||||
$lower
|
||||
->expects($this->never())
|
||||
->method('match');
|
||||
$this->router->add($lower, 5);
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 100);
|
||||
|
||||
$result = $this->router->match('/test');
|
||||
$this->assertEquals(array('test'), $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* The first usable match is used, no further routers are queried once a match is found
|
||||
*/
|
||||
public function testMatchRequest()
|
||||
{
|
||||
$url = '/test';
|
||||
list($lower, $low, $high) = $this->createRouterMocks();
|
||||
|
||||
$highest = $this->getMock('Symfony\Cmf\Component\Routing\Tests\Routing\RequestMatcher');
|
||||
|
||||
$request = Request::create('/test');
|
||||
|
||||
$highest
|
||||
->expects($this->once())
|
||||
->method('matchRequest')
|
||||
->will($this->throwException(new \Symfony\Component\Routing\Exception\ResourceNotFoundException))
|
||||
;
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->throwException(new \Symfony\Component\Routing\Exception\ResourceNotFoundException))
|
||||
;
|
||||
$low
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->returnValue(array('test')))
|
||||
;
|
||||
$lower
|
||||
->expects($this->never())
|
||||
->method('match')
|
||||
;
|
||||
|
||||
$this->router->add($lower, 5);
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 100);
|
||||
$this->router->add($highest, 200);
|
||||
|
||||
$result = $this->router->matchRequest($request);
|
||||
$this->assertEquals(array('test'), $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call match on ChainRouter that has RequestMatcher in the chain.
|
||||
*/
|
||||
public function testMatchWithRequestMatchers()
|
||||
{
|
||||
$url = '/test';
|
||||
|
||||
list($low) = $this->createRouterMocks();
|
||||
|
||||
$high = $this->getMock('Symfony\Cmf\Component\Routing\Tests\Routing\RequestMatcher');
|
||||
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('matchRequest')
|
||||
->with($this->callback(function (Request $r) use ($url) {
|
||||
return $r->getPathInfo() === $url;
|
||||
}))
|
||||
->will($this->throwException(new \Symfony\Component\Routing\Exception\ResourceNotFoundException))
|
||||
;
|
||||
$low
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->returnValue(array('test')))
|
||||
;
|
||||
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 20);
|
||||
|
||||
$result = $this->router->match($url);
|
||||
$this->assertEquals(array('test'), $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* If there is a method not allowed but another router matches, that one is used
|
||||
*/
|
||||
public function testMatchAndNotAllowed()
|
||||
{
|
||||
$url = '/test';
|
||||
list($low, $high) = $this->createRouterMocks();
|
||||
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->throwException(new \Symfony\Component\Routing\Exception\MethodNotAllowedException(array())))
|
||||
;
|
||||
$low
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->returnValue(array('test')))
|
||||
;
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 100);
|
||||
|
||||
$result = $this->router->match('/test');
|
||||
$this->assertEquals(array('test'), $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* If there is a method not allowed but another router matches, that one is used
|
||||
*/
|
||||
public function testMatchRequestAndNotAllowed()
|
||||
{
|
||||
$url = '/test';
|
||||
list($low, $high) = $this->createRouterMocks();
|
||||
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->throwException(new \Symfony\Component\Routing\Exception\MethodNotAllowedException(array())))
|
||||
;
|
||||
$low
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->returnValue(array('test')))
|
||||
;
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 100);
|
||||
|
||||
$result = $this->router->matchRequest(Request::create('/test'));
|
||||
$this->assertEquals(array('test'), $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
|
||||
*/
|
||||
public function testMatchNotFound()
|
||||
{
|
||||
$url = '/test';
|
||||
list($low, $high) = $this->createRouterMocks();
|
||||
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->throwException(new ResourceNotFoundException()))
|
||||
;
|
||||
$low
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->throwException(new ResourceNotFoundException()))
|
||||
;
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 100);
|
||||
|
||||
$this->router->match('/test');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
|
||||
*/
|
||||
public function testMatchRequestNotFound()
|
||||
{
|
||||
$url = '/test';
|
||||
list($low, $high) = $this->createRouterMocks();
|
||||
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->throwException(new ResourceNotFoundException()))
|
||||
;
|
||||
$low
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->throwException(new ResourceNotFoundException()))
|
||||
;
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 100);
|
||||
|
||||
$this->router->matchRequest(Request::create('/test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Call match on ChainRouter that has RequestMatcher in the chain.
|
||||
*
|
||||
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
|
||||
* @expectedExceptionMessage None of the routers in the chain matched url '/test'
|
||||
*/
|
||||
public function testMatchWithRequestMatchersNotFound()
|
||||
{
|
||||
$url = '/test';
|
||||
$request = Request::create('/test');
|
||||
|
||||
$high = $this->getMock('Symfony\Cmf\Component\Routing\Tests\Routing\RequestMatcher');
|
||||
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('matchRequest')
|
||||
->with($request)
|
||||
->will($this->throwException(new \Symfony\Component\Routing\Exception\ResourceNotFoundException))
|
||||
;
|
||||
|
||||
$this->router->add($high, 20);
|
||||
|
||||
$this->router->match($url);
|
||||
}
|
||||
|
||||
/**
|
||||
* If any of the routers throws a not allowed exception and no other matches, we need to see this
|
||||
*
|
||||
* @expectedException \Symfony\Component\Routing\Exception\MethodNotAllowedException
|
||||
*/
|
||||
public function testMatchMethodNotAllowed()
|
||||
{
|
||||
$url = '/test';
|
||||
list($low, $high) = $this->createRouterMocks();
|
||||
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->throwException(new MethodNotAllowedException(array())))
|
||||
;
|
||||
$low
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->throwException(new ResourceNotFoundException()))
|
||||
;
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 100);
|
||||
|
||||
$this->router->match('/test');
|
||||
}
|
||||
|
||||
/**
|
||||
* If any of the routers throws a not allowed exception and no other matches, we need to see this
|
||||
*
|
||||
* @expectedException \Symfony\Component\Routing\Exception\MethodNotAllowedException
|
||||
*/
|
||||
public function testMatchRequestMethodNotAllowed()
|
||||
{
|
||||
$url = '/test';
|
||||
list($low, $high) = $this->createRouterMocks();
|
||||
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->throwException(new MethodNotAllowedException(array())))
|
||||
;
|
||||
$low
|
||||
->expects($this->once())
|
||||
->method('match')
|
||||
->with($url)
|
||||
->will($this->throwException(new ResourceNotFoundException()))
|
||||
;
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 100);
|
||||
|
||||
$this->router->matchRequest(Request::create('/test'));
|
||||
}
|
||||
|
||||
public function testGenerate()
|
||||
{
|
||||
$url = '/test';
|
||||
$name = 'test';
|
||||
$parameters = array('test' => 'value');
|
||||
list($lower, $low, $high) = $this->createRouterMocks();
|
||||
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('generate')
|
||||
->with($name, $parameters, false)
|
||||
->will($this->throwException(new RouteNotFoundException()))
|
||||
;
|
||||
$low
|
||||
->expects($this->once())
|
||||
->method('generate')
|
||||
->with($name, $parameters, false)
|
||||
->will($this->returnValue($url))
|
||||
;
|
||||
$lower
|
||||
->expects($this->never())
|
||||
->method('generate')
|
||||
;
|
||||
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 100);
|
||||
|
||||
$result = $this->router->generate($name, $parameters);
|
||||
$this->assertEquals($url, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
|
||||
*/
|
||||
public function testGenerateNotFound()
|
||||
{
|
||||
$name = 'test';
|
||||
$parameters = array('test' => 'value');
|
||||
list($low, $high) = $this->createRouterMocks();
|
||||
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('generate')
|
||||
->with($name, $parameters, false)
|
||||
->will($this->throwException(new RouteNotFoundException()))
|
||||
;
|
||||
$low->expects($this->once())
|
||||
->method('generate')
|
||||
->with($name, $parameters, false)
|
||||
->will($this->throwException(new RouteNotFoundException()))
|
||||
;
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 100);
|
||||
|
||||
$this->router->generate($name, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Route is an object but no versatile generator around to do the debug message.
|
||||
*
|
||||
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
|
||||
*/
|
||||
public function testGenerateObjectNotFound()
|
||||
{
|
||||
$name = new \stdClass();
|
||||
$parameters = array('test' => 'value');
|
||||
|
||||
$defaultRouter = $this->getMock('Symfony\Component\Routing\RouterInterface');
|
||||
|
||||
$defaultRouter
|
||||
->expects($this->never())
|
||||
->method('generate')
|
||||
;
|
||||
|
||||
$this->router->add($defaultRouter, 200);
|
||||
|
||||
$this->router->generate($name, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* A versatile router will generate the debug message.
|
||||
*
|
||||
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
|
||||
*/
|
||||
public function testGenerateObjectNotFoundVersatile()
|
||||
{
|
||||
$name = new \stdClass();
|
||||
$parameters = array('test' => 'value');
|
||||
|
||||
$chainedRouter = $this->getMock('Symfony\Cmf\Component\Routing\Tests\Routing\VersatileRouter');
|
||||
$chainedRouter
|
||||
->expects($this->once())
|
||||
->method('supports')
|
||||
->will($this->returnValue(true))
|
||||
;
|
||||
$chainedRouter->expects($this->once())
|
||||
->method('generate')
|
||||
->with($name, $parameters, false)
|
||||
->will($this->throwException(new RouteNotFoundException()))
|
||||
;
|
||||
$chainedRouter->expects($this->once())
|
||||
->method('getRouteDebugMessage')
|
||||
->with($name, $parameters)
|
||||
->will($this->returnValue('message'))
|
||||
;
|
||||
|
||||
$this->router->add($chainedRouter, 10);
|
||||
|
||||
$this->router->generate($name, $parameters);
|
||||
}
|
||||
|
||||
public function testGenerateObjectName()
|
||||
{
|
||||
$name = new \stdClass();
|
||||
$parameters = array('test' => 'value');
|
||||
|
||||
$defaultRouter = $this->getMock('Symfony\Component\Routing\RouterInterface');
|
||||
$chainedRouter = $this->getMock('Symfony\Cmf\Component\Routing\Tests\Routing\VersatileRouter');
|
||||
|
||||
$defaultRouter
|
||||
->expects($this->never())
|
||||
->method('generate')
|
||||
;
|
||||
$chainedRouter
|
||||
->expects($this->once())
|
||||
->method('supports')
|
||||
->will($this->returnValue(true))
|
||||
;
|
||||
$chainedRouter
|
||||
->expects($this->once())
|
||||
->method('generate')
|
||||
->with($name, $parameters, false)
|
||||
->will($this->returnValue($name))
|
||||
;
|
||||
|
||||
$this->router->add($defaultRouter, 200);
|
||||
$this->router->add($chainedRouter, 100);
|
||||
|
||||
$result = $this->router->generate($name, $parameters);
|
||||
$this->assertEquals($name, $result);
|
||||
}
|
||||
|
||||
public function testWarmup()
|
||||
{
|
||||
$dir = 'test_dir';
|
||||
list($low) = $this->createRouterMocks();
|
||||
|
||||
$low
|
||||
->expects($this->never())
|
||||
->method('warmUp')
|
||||
;
|
||||
$high = $this->getMock('Symfony\Cmf\Component\Routing\Tests\Routing\WarmableRouterMock');
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('warmUp')
|
||||
->with($dir)
|
||||
;
|
||||
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 100);
|
||||
|
||||
$this->router->warmUp($dir);
|
||||
}
|
||||
|
||||
public function testRouteCollection()
|
||||
{
|
||||
list($low, $high) = $this->createRouterMocks();
|
||||
$lowcol = new RouteCollection();
|
||||
$lowcol->add('low', $this->buildMock('Symfony\Component\Routing\Route'));
|
||||
$highcol = new RouteCollection();
|
||||
$highcol->add('high', $this->buildMock('Symfony\Component\Routing\Route'));
|
||||
|
||||
$low
|
||||
->expects($this->once())
|
||||
->method('getRouteCollection')
|
||||
->will($this->returnValue($lowcol))
|
||||
;
|
||||
$high
|
||||
->expects($this->once())
|
||||
->method('getRouteCollection')
|
||||
->will($this->returnValue($highcol))
|
||||
;
|
||||
|
||||
$this->router->add($low, 10);
|
||||
$this->router->add($high, 100);
|
||||
|
||||
$collection = $this->router->getRouteCollection();
|
||||
$this->assertInstanceOf('Symfony\Component\Routing\RouteCollection', $collection);
|
||||
|
||||
$names = array();
|
||||
foreach ($collection->all() as $name => $route) {
|
||||
$this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
|
||||
$names[] = $name;
|
||||
}
|
||||
$this->assertEquals(array('high', 'low'), $names);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
|
||||
*/
|
||||
public function testSupport()
|
||||
{
|
||||
|
||||
$router = $this->getMock('Symfony\Cmf\Component\Routing\Tests\Routing\VersatileRouter');
|
||||
$router
|
||||
->expects($this->once())
|
||||
->method('supports')
|
||||
->will($this->returnValue(false))
|
||||
;
|
||||
|
||||
$router
|
||||
->expects($this->never())
|
||||
->method('generate')
|
||||
->will($this->returnValue(false))
|
||||
;
|
||||
|
||||
$this->router->add($router);
|
||||
|
||||
$this->router->generate('foobar');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return RouterInterface[]|\PHPUnit_Framework_MockObject_MockObject[]
|
||||
*/
|
||||
protected function createRouterMocks()
|
||||
{
|
||||
return array(
|
||||
$this->getMock('Symfony\Component\Routing\RouterInterface'),
|
||||
$this->getMock('Symfony\Component\Routing\RouterInterface'),
|
||||
$this->getMock('Symfony\Component\Routing\RouterInterface'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class WarmableRouterMock implements RouterInterface, WarmableInterface
|
||||
{
|
||||
}
|
||||
|
||||
abstract class RequestMatcher implements RouterInterface, RequestMatcherInterface
|
||||
{
|
||||
}
|
||||
|
||||
abstract class VersatileRouter implements VersatileGeneratorInterface, RequestMatcherInterface
|
||||
{
|
||||
}
|
|
@ -1,457 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony CMF package.
|
||||
*
|
||||
* (c) 2011-2014 Symfony CMF
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Cmf\Component\Routing\Tests\Routing;
|
||||
|
||||
use Symfony\Cmf\Component\Routing\RouteReferrersReadInterface;
|
||||
|
||||
use Symfony\Cmf\Component\Routing\ContentAwareGenerator;
|
||||
use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
|
||||
|
||||
class ContentAwareGeneratorTest extends CmfUnitTestCase
|
||||
{
|
||||
protected $contentDocument;
|
||||
protected $routeDocument;
|
||||
protected $routeCompiled;
|
||||
protected $provider;
|
||||
|
||||
/**
|
||||
* @var ContentAwareGenerator
|
||||
*/
|
||||
protected $generator;
|
||||
protected $context;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->contentDocument = $this->buildMock('Symfony\Cmf\Component\Routing\RouteReferrersReadInterface');
|
||||
$this->routeDocument = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile'));
|
||||
$this->routeCompiled = $this->buildMock('Symfony\Component\Routing\CompiledRoute');
|
||||
$this->provider = $this->buildMock('Symfony\Cmf\Component\Routing\RouteProviderInterface');
|
||||
$this->context = $this->buildMock('Symfony\Component\Routing\RequestContext');
|
||||
|
||||
$this->generator = new TestableContentAwareGenerator($this->provider);
|
||||
}
|
||||
|
||||
public function testGenerateFromContent()
|
||||
{
|
||||
$this->provider->expects($this->never())
|
||||
->method('getRouteByName')
|
||||
;
|
||||
$this->contentDocument->expects($this->once())
|
||||
->method('getRoutes')
|
||||
->will($this->returnValue(array($this->routeDocument)))
|
||||
;
|
||||
$this->routeDocument->expects($this->once())
|
||||
->method('compile')
|
||||
->will($this->returnValue($this->routeCompiled))
|
||||
;
|
||||
|
||||
$this->assertEquals('result_url', $this->generator->generate($this->contentDocument));
|
||||
}
|
||||
|
||||
public function testGenerateFromContentId()
|
||||
{
|
||||
$this->provider->expects($this->never())
|
||||
->method('getRouteByName')
|
||||
;
|
||||
|
||||
$contentRepository = $this->buildMock('Symfony\Cmf\Component\Routing\ContentRepositoryInterface', array('findById', 'getContentId'));
|
||||
$contentRepository->expects($this->once())
|
||||
->method('findById')
|
||||
->with('/content/id')
|
||||
->will($this->returnValue($this->contentDocument))
|
||||
;
|
||||
$this->generator->setContentRepository($contentRepository);
|
||||
|
||||
$this->contentDocument->expects($this->once())
|
||||
->method('getRoutes')
|
||||
->will($this->returnValue(array($this->routeDocument)))
|
||||
;
|
||||
|
||||
$this->routeDocument->expects($this->once())
|
||||
->method('compile')
|
||||
->will($this->returnValue($this->routeCompiled))
|
||||
;
|
||||
|
||||
$this->assertEquals('result_url', $this->generator->generate('', array('content_id' => '/content/id')));
|
||||
}
|
||||
|
||||
public function testGenerateEmptyRouteString()
|
||||
{
|
||||
$this->provider->expects($this->never())
|
||||
->method('getRouteByName')
|
||||
;
|
||||
|
||||
$this->contentDocument->expects($this->once())
|
||||
->method('getRoutes')
|
||||
->will($this->returnValue(array($this->routeDocument)))
|
||||
;
|
||||
|
||||
$this->routeDocument->expects($this->once())
|
||||
->method('compile')
|
||||
->will($this->returnValue($this->routeCompiled))
|
||||
;
|
||||
|
||||
$this->assertEquals('result_url', $this->generator->generate($this->contentDocument));
|
||||
}
|
||||
|
||||
public function testGenerateRouteMultilang()
|
||||
{
|
||||
$route_en = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile', 'getContent'));
|
||||
$route_en->setLocale('en');
|
||||
$route_de = $this->routeDocument;
|
||||
$route_de->setLocale('de');
|
||||
|
||||
$this->contentDocument->expects($this->once())
|
||||
->method('getRoutes')
|
||||
->will($this->returnValue(array($route_en, $route_de)))
|
||||
;
|
||||
$route_en->expects($this->once())
|
||||
->method('getContent')
|
||||
->will($this->returnValue($this->contentDocument))
|
||||
;
|
||||
$route_en->expects($this->never())
|
||||
->method('compile')
|
||||
;
|
||||
$route_de->expects($this->once())
|
||||
->method('compile')
|
||||
->will($this->returnValue($this->routeCompiled))
|
||||
;
|
||||
|
||||
$this->assertEquals('result_url', $this->generator->generate($route_en, array('_locale' => 'de')));
|
||||
}
|
||||
|
||||
public function testGenerateRouteMultilangDefaultLocale()
|
||||
{
|
||||
$route = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock');
|
||||
$route->expects($this->any())
|
||||
->method('compile')
|
||||
->will($this->returnValue($this->routeCompiled))
|
||||
;
|
||||
$route->expects($this->any())
|
||||
->method('getRequirement')
|
||||
->with('_locale')
|
||||
->will($this->returnValue('de|en'))
|
||||
;
|
||||
$route->expects($this->any())
|
||||
->method('getDefault')
|
||||
->with('_locale')
|
||||
->will($this->returnValue('en'))
|
||||
;
|
||||
$this->routeCompiled->expects($this->any())
|
||||
->method('getVariables')
|
||||
->will($this->returnValue(array()))
|
||||
;
|
||||
|
||||
$this->assertEquals('result_url', $this->generator->generate($route, array('_locale' => 'en')));
|
||||
}
|
||||
|
||||
public function testGenerateRouteMultilangLocaleNomatch()
|
||||
{
|
||||
$route_en = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile', 'getContent'));
|
||||
$route_en->setLocale('en');
|
||||
$route_de = $this->routeDocument;
|
||||
$route_de->setLocale('de');
|
||||
|
||||
$this->contentDocument->expects($this->once())
|
||||
->method('getRoutes')
|
||||
->will($this->returnValue(array($route_en, $route_de)))
|
||||
;
|
||||
$route_en->expects($this->once())
|
||||
->method('getContent')
|
||||
->will($this->returnValue($this->contentDocument))
|
||||
;
|
||||
$route_en->expects($this->once())
|
||||
->method('compile')
|
||||
->will($this->returnValue($this->routeCompiled))
|
||||
;
|
||||
$route_de->expects($this->never())
|
||||
->method('compile')
|
||||
;
|
||||
|
||||
$this->assertEquals('result_url', $this->generator->generate($route_en, array('_locale' => 'fr')));
|
||||
}
|
||||
|
||||
public function testGenerateNoncmfRouteMultilang()
|
||||
{
|
||||
$route_en = $this->buildMock('Symfony\Component\Routing\Route', array('getDefaults', 'compile', 'getContent'));
|
||||
|
||||
$route_en->expects($this->once())
|
||||
->method('compile')
|
||||
->will($this->returnValue($this->routeCompiled))
|
||||
;
|
||||
|
||||
$this->assertEquals('result_url', $this->generator->generate($route_en, array('_locale' => 'de')));
|
||||
}
|
||||
|
||||
public function testGenerateRoutenameMultilang()
|
||||
{
|
||||
$name = 'foo/bar';
|
||||
$route_en = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile', 'getContent'));
|
||||
$route_en->setLocale('en');
|
||||
$route_de = $this->routeDocument;
|
||||
$route_de->setLocale('de');
|
||||
|
||||
$this->provider->expects($this->once())
|
||||
->method('getRouteByName')
|
||||
->with($name)
|
||||
->will($this->returnValue($route_en))
|
||||
;
|
||||
$this->contentDocument->expects($this->once())
|
||||
->method('getRoutes')
|
||||
->will($this->returnValue(array($route_en, $route_de)))
|
||||
;
|
||||
$route_en->expects($this->once())
|
||||
->method('getContent')
|
||||
->will($this->returnValue($this->contentDocument))
|
||||
;
|
||||
$route_en->expects($this->never())
|
||||
->method('compile')
|
||||
;
|
||||
$route_de->expects($this->once())
|
||||
->method('compile')
|
||||
->will($this->returnValue($this->routeCompiled))
|
||||
;
|
||||
|
||||
$this->assertEquals('result_url', $this->generator->generate($name, array('_locale' => 'de')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
|
||||
*/
|
||||
public function testGenerateRoutenameMultilangNotFound()
|
||||
{
|
||||
$name = 'foo/bar';
|
||||
|
||||
$this->provider->expects($this->once())
|
||||
->method('getRouteByName')
|
||||
->with($name)
|
||||
->will($this->returnValue(null))
|
||||
;
|
||||
|
||||
$this->generator->generate($name, array('_locale' => 'de'));
|
||||
}
|
||||
|
||||
public function testGenerateDocumentMultilang()
|
||||
{
|
||||
$route_en = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile'));
|
||||
$route_en->setLocale('en');
|
||||
$route_de = $this->routeDocument;
|
||||
$route_de->setLocale('de');
|
||||
|
||||
$this->contentDocument->expects($this->once())
|
||||
->method('getRoutes')
|
||||
->will($this->returnValue(array($route_en, $route_de)))
|
||||
;
|
||||
$route_en->expects($this->never())
|
||||
->method('compile')
|
||||
;
|
||||
$route_de->expects($this->once())
|
||||
->method('compile')
|
||||
->will($this->returnValue($this->routeCompiled))
|
||||
;
|
||||
|
||||
$this->assertEquals('result_url', $this->generator->generate($this->contentDocument, array('_locale' => 'de')));
|
||||
}
|
||||
|
||||
public function testGenerateDocumentMultilangLocaleNomatch()
|
||||
{
|
||||
$route_en = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile'));
|
||||
$route_en->setLocale('en');
|
||||
$route_de = $this->routeDocument;
|
||||
$route_de->setLocale('de');
|
||||
|
||||
$this->contentDocument->expects($this->once())
|
||||
->method('getRoutes')
|
||||
->will($this->returnValue(array($route_en, $route_de)))
|
||||
;
|
||||
$route_en->expects($this->once())
|
||||
->method('compile')
|
||||
->will($this->returnValue($this->routeCompiled))
|
||||
;
|
||||
$route_de->expects($this->never())
|
||||
->method('compile')
|
||||
;
|
||||
|
||||
$this->assertEquals('result_url', $this->generator->generate($this->contentDocument, array('_locale' => 'fr')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate without any information.
|
||||
*
|
||||
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
|
||||
*/
|
||||
public function testGenerateNoContent()
|
||||
{
|
||||
$this->generator->generate('', array());
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate with an object that is neither a route nor route aware.
|
||||
*
|
||||
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
|
||||
*/
|
||||
public function testGenerateInvalidContent()
|
||||
{
|
||||
$this->generator->generate($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate with a content_id but there is no content repository.
|
||||
*
|
||||
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
|
||||
*/
|
||||
public function testGenerateNoContentRepository()
|
||||
{
|
||||
$this->provider->expects($this->never())
|
||||
->method('getRouteByName')
|
||||
;
|
||||
|
||||
$this->generator->generate('', array('content_id' => '/content/id'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate with content_id but the content is not found.
|
||||
*
|
||||
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
|
||||
*/
|
||||
public function testGenerateNoContentFoundInRepository()
|
||||
{
|
||||
$this->provider->expects($this->never())
|
||||
->method('getRouteByName')
|
||||
;
|
||||
|
||||
$contentRepository = $this->buildMock('Symfony\Cmf\Component\Routing\ContentRepositoryInterface', array('findById', 'getContentId'));
|
||||
$contentRepository->expects($this->once())
|
||||
->method('findById')
|
||||
->with('/content/id')
|
||||
->will($this->returnValue(null))
|
||||
;
|
||||
$this->generator->setContentRepository($contentRepository);
|
||||
|
||||
$this->generator->generate('', array('content_id' => '/content/id'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate with content_id but the object at id is not route aware.
|
||||
*
|
||||
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
|
||||
*/
|
||||
public function testGenerateWrongContentClassInRepository()
|
||||
{
|
||||
$this->provider->expects($this->never())
|
||||
->method('getRouteByName')
|
||||
;
|
||||
|
||||
$contentRepository = $this->buildMock('Symfony\Cmf\Component\Routing\ContentRepositoryInterface', array('findById', 'getContentId'));
|
||||
$contentRepository->expects($this->once())
|
||||
->method('findById')
|
||||
->with('/content/id')
|
||||
->will($this->returnValue($this))
|
||||
;
|
||||
$this->generator->setContentRepository($contentRepository);
|
||||
|
||||
$this->generator->generate('', array('content_id' => '/content/id'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate from a content that has no routes associated.
|
||||
*
|
||||
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
|
||||
*/
|
||||
public function testGenerateNoRoutes()
|
||||
{
|
||||
$this->contentDocument->expects($this->once())
|
||||
->method('getRoutes')
|
||||
->will($this->returnValue(array()));
|
||||
|
||||
$this->generator->generate($this->contentDocument);
|
||||
}
|
||||
/**
|
||||
* Generate from a content that returns something that is not a route as route.
|
||||
*
|
||||
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
|
||||
*/
|
||||
public function testGenerateInvalidRoute()
|
||||
{
|
||||
$this->contentDocument->expects($this->once())
|
||||
->method('getRoutes')
|
||||
->will($this->returnValue(array($this)));
|
||||
|
||||
$this->generator->generate($this->contentDocument);
|
||||
}
|
||||
|
||||
public function testGetLocaleAttribute()
|
||||
{
|
||||
$this->generator->setDefaultLocale('en');
|
||||
|
||||
$attributes = array('_locale' => 'fr');
|
||||
$this->assertEquals('fr', $this->generator->getLocale($attributes));
|
||||
}
|
||||
|
||||
public function testGetLocaleDefault()
|
||||
{
|
||||
$this->generator->setDefaultLocale('en');
|
||||
|
||||
$attributes = array();
|
||||
$this->assertEquals('en', $this->generator->getLocale($attributes));
|
||||
}
|
||||
|
||||
public function testGetLocaleContext()
|
||||
{
|
||||
$this->generator->setDefaultLocale('en');
|
||||
|
||||
$this->generator->getContext()->setParameter('_locale', 'de');
|
||||
|
||||
$attributes = array();
|
||||
$this->assertEquals('de', $this->generator->getLocale($attributes));
|
||||
}
|
||||
|
||||
public function testSupports()
|
||||
{
|
||||
$this->assertTrue($this->generator->supports(''));
|
||||
$this->assertTrue($this->generator->supports(null));
|
||||
$this->assertTrue($this->generator->supports($this->contentDocument));
|
||||
$this->assertFalse($this->generator->supports($this));
|
||||
}
|
||||
|
||||
public function testGetRouteDebugMessage()
|
||||
{
|
||||
$this->assertContains('/some/content', $this->generator->getRouteDebugMessage(null, array('content_id' => '/some/content')));
|
||||
$this->assertContains('Route aware content Symfony\Cmf\Component\Routing\Tests\Routing\RouteAware', $this->generator->getRouteDebugMessage(new RouteAware()));
|
||||
$this->assertContains('/some/content', $this->generator->getRouteDebugMessage('/some/content'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwrite doGenerate to reduce amount of mocking needed
|
||||
*/
|
||||
class TestableContentAwareGenerator extends ContentAwareGenerator
|
||||
{
|
||||
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array())
|
||||
{
|
||||
return 'result_url';
|
||||
}
|
||||
|
||||
// expose as public
|
||||
public function getLocale($parameters)
|
||||
{
|
||||
return parent::getLocale($parameters);
|
||||
}
|
||||
}
|
||||
|
||||
class RouteAware implements RouteReferrersReadInterface
|
||||
{
|
||||
public function getRoutes()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
}
|
|
@ -1,330 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony CMF package.
|
||||
*
|
||||
* (c) 2011-2014 Symfony CMF
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Cmf\Component\Routing\Tests\Routing;
|
||||
|
||||
use Symfony\Cmf\Component\Routing\Event\Events;
|
||||
use Symfony\Cmf\Component\Routing\Event\RouterMatchEvent;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
|
||||
use Symfony\Cmf\Component\Routing\DynamicRouter;
|
||||
|
||||
use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
|
||||
|
||||
class DynamicRouterTest extends CmfUnitTestCase
|
||||
{
|
||||
protected $routeDocument;
|
||||
protected $matcher;
|
||||
protected $generator;
|
||||
protected $enhancer;
|
||||
/** @var DynamicRouter */
|
||||
protected $router;
|
||||
protected $context;
|
||||
public $request;
|
||||
|
||||
protected $url = '/foo/bar';
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->routeDocument = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults'));
|
||||
|
||||
$this->matcher = $this->buildMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
|
||||
$this->generator = $this->buildMock('Symfony\Cmf\Component\Routing\VersatileGeneratorInterface', array('supports', 'generate', 'setContext', 'getContext', 'getRouteDebugMessage'));
|
||||
$this->enhancer = $this->buildMock('Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface', array('enhance'));
|
||||
|
||||
$this->context = $this->buildMock('Symfony\Component\Routing\RequestContext');
|
||||
$this->request = Request::create($this->url);
|
||||
|
||||
$this->router = new DynamicRouter($this->context, $this->matcher, $this->generator);
|
||||
$this->router->addRouteEnhancer($this->enhancer);
|
||||
}
|
||||
|
||||
/**
|
||||
* rather trivial, but we want 100% coverage
|
||||
*/
|
||||
public function testContext()
|
||||
{
|
||||
$this->router->setContext($this->context);
|
||||
$this->assertSame($this->context, $this->router->getContext());
|
||||
}
|
||||
|
||||
public function testRouteCollectionEmpty()
|
||||
{
|
||||
$collection = $this->router->getRouteCollection();
|
||||
$this->assertInstanceOf('Symfony\Component\Routing\RouteCollection', $collection);
|
||||
}
|
||||
|
||||
public function testRouteCollectionLazy()
|
||||
{
|
||||
$provider = $this->getMock('Symfony\Cmf\Component\Routing\RouteProviderInterface');
|
||||
$router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', null, $provider);
|
||||
|
||||
$collection = $router->getRouteCollection();
|
||||
$this->assertInstanceOf('Symfony\Cmf\Component\Routing\LazyRouteCollection', $collection);
|
||||
}
|
||||
|
||||
/// generator tests ///
|
||||
|
||||
public function testGetGenerator()
|
||||
{
|
||||
$this->generator->expects($this->once())
|
||||
->method('setContext')
|
||||
->with($this->equalTo($this->context));
|
||||
|
||||
$generator = $this->router->getGenerator();
|
||||
$this->assertInstanceOf('Symfony\Component\Routing\Generator\UrlGeneratorInterface', $generator);
|
||||
$this->assertSame($this->generator, $generator);
|
||||
}
|
||||
|
||||
public function testGenerate()
|
||||
{
|
||||
$name = 'my_route_name';
|
||||
$parameters = array('foo' => 'bar');
|
||||
$absolute = true;
|
||||
|
||||
$this->generator->expects($this->once())
|
||||
->method('generate')
|
||||
->with($name, $parameters, $absolute)
|
||||
->will($this->returnValue('http://test'))
|
||||
;
|
||||
|
||||
$url = $this->router->generate($name, $parameters, $absolute);
|
||||
$this->assertEquals('http://test', $url);
|
||||
}
|
||||
|
||||
public function testSupports()
|
||||
{
|
||||
$name = 'foo/bar';
|
||||
$this->generator->expects($this->once())
|
||||
->method('supports')
|
||||
->with($this->equalTo($name))
|
||||
->will($this->returnValue(true))
|
||||
;
|
||||
|
||||
$this->assertTrue($this->router->supports($name));
|
||||
}
|
||||
|
||||
public function testSupportsNonversatile()
|
||||
{
|
||||
$generator = $this->buildMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface', array('generate', 'setContext', 'getContext'));
|
||||
$router = new DynamicRouter($this->context, $this->matcher, $generator);
|
||||
$this->assertInternalType('string', $router->getRouteDebugMessage('test'));
|
||||
|
||||
$this->assertTrue($router->supports('some string'));
|
||||
$this->assertFalse($router->supports($this));
|
||||
}
|
||||
|
||||
/// match tests ///
|
||||
|
||||
public function testGetMatcher()
|
||||
{
|
||||
$this->matcher->expects($this->once())
|
||||
->method('setContext')
|
||||
->with($this->equalTo($this->context));
|
||||
|
||||
$matcher = $this->router->getMatcher();
|
||||
$this->assertInstanceOf('Symfony\Component\Routing\Matcher\UrlMatcherInterface', $matcher);
|
||||
$this->assertSame($this->matcher, $matcher);
|
||||
}
|
||||
|
||||
public function testMatchUrl()
|
||||
{
|
||||
$routeDefaults = array('foo' => 'bar');
|
||||
$this->matcher->expects($this->once())
|
||||
->method('match')
|
||||
->with($this->url)
|
||||
->will($this->returnValue($routeDefaults))
|
||||
;
|
||||
|
||||
$expected = array('this' => 'that');
|
||||
$this->enhancer->expects($this->once())
|
||||
->method('enhance')
|
||||
->with($this->equalTo($routeDefaults), $this->equalTo($this->request))
|
||||
->will($this->returnValue($expected))
|
||||
;
|
||||
|
||||
$results = $this->router->match($this->url);
|
||||
|
||||
$this->assertEquals($expected, $results);
|
||||
}
|
||||
|
||||
public function testMatchRequestWithUrlMatcher()
|
||||
{
|
||||
$routeDefaults = array('foo' => 'bar');
|
||||
|
||||
$this->matcher->expects($this->once())
|
||||
->method('match')
|
||||
->with($this->url)
|
||||
->will($this->returnValue($routeDefaults))
|
||||
;
|
||||
|
||||
$expected = array('this' => 'that');
|
||||
$this->enhancer->expects($this->once())
|
||||
->method('enhance')
|
||||
// somehow request object gets confused, check on instance only
|
||||
->with($this->equalTo($routeDefaults), $this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
|
||||
->will($this->returnValue($expected))
|
||||
;
|
||||
|
||||
$results = $this->router->matchRequest($this->request);
|
||||
|
||||
$this->assertEquals($expected, $results);
|
||||
}
|
||||
|
||||
public function testMatchRequest()
|
||||
{
|
||||
$routeDefaults = array('foo' => 'bar');
|
||||
|
||||
$matcher = $this->buildMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface', array('matchRequest', 'setContext', 'getContext'));
|
||||
$router = new DynamicRouter($this->context, $matcher, $this->generator);
|
||||
|
||||
$matcher->expects($this->once())
|
||||
->method('matchRequest')
|
||||
->with($this->request)
|
||||
->will($this->returnValue($routeDefaults))
|
||||
;
|
||||
|
||||
$expected = array('this' => 'that');
|
||||
$this->enhancer->expects($this->once())
|
||||
->method('enhance')
|
||||
->with($this->equalTo($routeDefaults), $this->equalTo($this->request))
|
||||
->will($this->returnValue($expected))
|
||||
;
|
||||
|
||||
$router->addRouteEnhancer($this->enhancer);
|
||||
|
||||
$this->assertEquals($expected, $router->matchRequest($this->request));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
|
||||
*/
|
||||
public function testMatchFilter()
|
||||
{
|
||||
$router = new DynamicRouter($this->context, $this->matcher, $this->generator, '#/different/prefix.*#');
|
||||
$router->addRouteEnhancer($this->enhancer);
|
||||
|
||||
$this->matcher->expects($this->never())
|
||||
->method('match')
|
||||
;
|
||||
|
||||
$this->enhancer->expects($this->never())
|
||||
->method('enhance')
|
||||
;
|
||||
|
||||
$router->match($this->url);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
|
||||
*/
|
||||
public function testMatchRequestFilter()
|
||||
{
|
||||
$matcher = $this->buildMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface', array('matchRequest', 'setContext', 'getContext'));
|
||||
|
||||
$router = new DynamicRouter($this->context, $matcher, $this->generator, '#/different/prefix.*#');
|
||||
$router->addRouteEnhancer($this->enhancer);
|
||||
|
||||
$matcher->expects($this->never())
|
||||
->method('matchRequest')
|
||||
;
|
||||
|
||||
$this->enhancer->expects($this->never())
|
||||
->method('enhance')
|
||||
;
|
||||
|
||||
$router->matchRequest($this->request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testMatchUrlWithRequestMatcher()
|
||||
{
|
||||
$matcher = $this->buildMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface', array('matchRequest', 'setContext', 'getContext'));
|
||||
$router = new DynamicRouter($this->context, $matcher, $this->generator);
|
||||
|
||||
$router->match($this->url);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testInvalidMatcher()
|
||||
{
|
||||
new DynamicRouter($this->context, $this, $this->generator);
|
||||
}
|
||||
|
||||
public function testRouteDebugMessage()
|
||||
{
|
||||
$this->generator->expects($this->once())
|
||||
->method('getRouteDebugMessage')
|
||||
->with($this->equalTo('test'), $this->equalTo(array()))
|
||||
->will($this->returnValue('debug message'))
|
||||
;
|
||||
|
||||
$this->assertEquals('debug message', $this->router->getRouteDebugMessage('test'));
|
||||
}
|
||||
|
||||
public function testRouteDebugMessageNonversatile()
|
||||
{
|
||||
$generator = $this->buildMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface', array('generate', 'setContext', 'getContext'));
|
||||
$router = new DynamicRouter($this->context, $this->matcher, $generator);
|
||||
$this->assertInternalType('string', $router->getRouteDebugMessage('test'));
|
||||
}
|
||||
|
||||
public function testEventHandler()
|
||||
{
|
||||
$eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||
$router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $eventDispatcher);
|
||||
|
||||
$eventDispatcher->expects($this->once())
|
||||
->method('dispatch')
|
||||
->with(Events::PRE_DYNAMIC_MATCH, $this->equalTo(new RouterMatchEvent()))
|
||||
;
|
||||
|
||||
$routeDefaults = array('foo' => 'bar');
|
||||
$this->matcher->expects($this->once())
|
||||
->method('match')
|
||||
->with($this->url)
|
||||
->will($this->returnValue($routeDefaults))
|
||||
;
|
||||
|
||||
$this->assertEquals($routeDefaults, $router->match($this->url));
|
||||
}
|
||||
|
||||
public function testEventHandlerRequest()
|
||||
{
|
||||
$eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||
$router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $eventDispatcher);
|
||||
|
||||
$that = $this;
|
||||
$eventDispatcher->expects($this->once())
|
||||
->method('dispatch')
|
||||
->with(Events::PRE_DYNAMIC_MATCH_REQUEST, $this->callback(function ($event) use ($that) {
|
||||
$that->assertInstanceOf('Symfony\Cmf\Component\Routing\Event\RouterMatchEvent', $event);
|
||||
$that->assertEquals($that->request, $event->getRequest());
|
||||
|
||||
return true;
|
||||
}))
|
||||
;
|
||||
|
||||
$routeDefaults = array('foo' => 'bar');
|
||||
$this->matcher->expects($this->once())
|
||||
->method('match')
|
||||
->with($this->url)
|
||||
->will($this->returnValue($routeDefaults))
|
||||
;
|
||||
|
||||
$this->assertEquals($routeDefaults, $router->matchRequest($this->request));
|
||||
}
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony CMF package.
|
||||
*
|
||||
* (c) 2011-2014 Symfony CMF
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Cmf\Component\Routing;
|
||||
|
||||
use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
|
||||
use Symfony\Component\Routing\Route;
|
||||
|
||||
/**
|
||||
* Tests the lazy route collection.
|
||||
*
|
||||
* @group cmf/routing
|
||||
*/
|
||||
class LazyRouteCollectionTest extends CmfUnitTestCase
|
||||
{
|
||||
/**
|
||||
* Tests the iterator without a paged route provider.
|
||||
*/
|
||||
public function testGetIterator()
|
||||
{
|
||||
$routeProvider = $this->getMock('Symfony\Cmf\Component\Routing\RouteProviderInterface');
|
||||
$testRoutes = array(
|
||||
'route_1' => new Route('/route-1'),
|
||||
'route_2"' => new Route('/route-2'),
|
||||
);
|
||||
$routeProvider->expects($this->exactly(2))
|
||||
->method('getRoutesByNames')
|
||||
->with(null)
|
||||
->will($this->returnValue($testRoutes));
|
||||
$lazyRouteCollection = new LazyRouteCollection($routeProvider);
|
||||
$this->assertEquals($testRoutes, iterator_to_array($lazyRouteCollection->getIterator()));
|
||||
$this->assertEquals($testRoutes, $lazyRouteCollection->all());
|
||||
}
|
||||
}
|
|
@ -1,132 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony CMF package.
|
||||
*
|
||||
* (c) 2011-2014 Symfony CMF
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Cmf\Component\Routing;
|
||||
|
||||
use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
|
||||
use Symfony\Component\Routing\Route;
|
||||
|
||||
/**
|
||||
* Tests the page route collection.
|
||||
*
|
||||
* @group cmf/routing
|
||||
*/
|
||||
class PagedRouteCollectionTest extends CmfUnitTestCase
|
||||
{
|
||||
/**
|
||||
* Contains a mocked route provider.
|
||||
*
|
||||
* @var \Symfony\Cmf\Component\Routing\PagedRouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $routeProvider;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->routeProvider = $this->getMock('Symfony\Cmf\Component\Routing\PagedRouteProviderInterface');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests iterating a small amount of routes.
|
||||
*
|
||||
* @dataProvider providerIterator
|
||||
*/
|
||||
public function testIterator($amountRoutes, $routesLoadedInParallel, $expectedCalls = array())
|
||||
{
|
||||
$routes = array();
|
||||
for ($i = 0; $i < $amountRoutes; $i++) {
|
||||
$routes['test_' . $i] = new Route("/example-$i");
|
||||
}
|
||||
$names = array_keys($routes);
|
||||
|
||||
foreach ($expectedCalls as $i => $range)
|
||||
{
|
||||
$this->routeProvider->expects($this->at($i))
|
||||
->method('getRoutesPaged')
|
||||
->with($range[0], $range[1])
|
||||
->will($this->returnValue(array_slice($routes, $range[0], $range[1])));
|
||||
}
|
||||
|
||||
$route_collection = new PagedRouteCollection($this->routeProvider, $routesLoadedInParallel);
|
||||
|
||||
$counter = 0;
|
||||
foreach ($route_collection as $route_name => $route) {
|
||||
// Ensure the route did not changed.
|
||||
$this->assertEquals($routes[$route_name], $route);
|
||||
// Ensure that the order did not changed.
|
||||
$this->assertEquals($route_name, $names[$counter]);
|
||||
$counter++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for testIterator().
|
||||
*/
|
||||
public function providerIterator()
|
||||
{
|
||||
$data = array();
|
||||
// Non total routes.
|
||||
$data[] = array(0, 20, array(array(0, 20)));
|
||||
// Less total routes than loaded in parallel.
|
||||
$data[] = array(10, 20, array(array(0, 20)));
|
||||
// Exact the same amount of routes then loaded in parallel.
|
||||
$data[] = array(20, 20, array(array(0, 20), array(20, 20)));
|
||||
// Less than twice the amount.
|
||||
$data[] = array(39, 20, array(array(0, 20), array(20, 20)));
|
||||
// More total routes than loaded in parallel.
|
||||
$data[] = array(40, 20, array(array(0, 20), array(20, 20), array(40, 20)));
|
||||
$data[] = array(41, 20, array(array(0, 20), array(20, 20), array(40, 20)));
|
||||
// why not.
|
||||
$data[] = array(42, 23, array(array(0, 23), array(23, 23)));
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the count() method.
|
||||
*/
|
||||
public function testCount()
|
||||
{
|
||||
$this->routeProvider->expects($this->once())
|
||||
->method('getRoutesCount')
|
||||
->will($this->returnValue(12));
|
||||
$routeCollection = new PagedRouteCollection($this->routeProvider);
|
||||
$this->assertEquals(12, $routeCollection->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the rewind method once the iterator is at the end.
|
||||
*/
|
||||
public function testIteratingAndRewind()
|
||||
{
|
||||
$routes = array();
|
||||
for ($i = 0; $i < 30; $i++) {
|
||||
$routes['test_' . $i] = new Route("/example-$i");
|
||||
}
|
||||
$this->routeProvider->expects($this->any())
|
||||
->method('getRoutesPaged')
|
||||
->will($this->returnValueMap(array(
|
||||
array(0, 10, array_slice($routes, 0, 10)),
|
||||
array(10, 10, array_slice($routes, 9, 10)),
|
||||
array(20, 10, array()),
|
||||
)));
|
||||
|
||||
$routeCollection = new PagedRouteCollection($this->routeProvider, 10);
|
||||
|
||||
// Force the iterating process.
|
||||
$routeCollection->rewind();
|
||||
for ($i = 0; $i < 29; $i++) {
|
||||
$routeCollection->next();
|
||||
}
|
||||
$routeCollection->rewind();
|
||||
|
||||
$this->assertEquals('test_0', $routeCollection->key());
|
||||
$this->assertEquals($routes['test_0'], $routeCollection->current());
|
||||
}
|
||||
}
|
|
@ -1,149 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony CMF package.
|
||||
*
|
||||
* (c) 2011-2014 Symfony CMF
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Cmf\Component\Routing\Tests\Routing;
|
||||
|
||||
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
use Symfony\Component\Routing\Route;
|
||||
|
||||
use Symfony\Cmf\Component\Routing\ProviderBasedGenerator;
|
||||
use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
|
||||
|
||||
class ProviderBasedGeneratorTest extends CmfUnitTestCase
|
||||
{
|
||||
protected $routeDocument;
|
||||
protected $routeCompiled;
|
||||
protected $provider;
|
||||
|
||||
/** @var ProviderBasedGenerator */
|
||||
protected $generator;
|
||||
protected $context;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->routeDocument = $this->buildMock('Symfony\Component\Routing\Route', array('getDefaults', 'compile'));
|
||||
$this->routeCompiled = $this->buildMock('Symfony\Component\Routing\CompiledRoute');
|
||||
$this->provider = $this->buildMock('Symfony\Cmf\Component\Routing\RouteProviderInterface');
|
||||
$this->context = $this->buildMock('Symfony\Component\Routing\RequestContext');
|
||||
|
||||
$this->generator= new TestableProviderBasedGenerator($this->provider);
|
||||
}
|
||||
|
||||
public function testGenerateFromName()
|
||||
{
|
||||
$name = 'foo/bar';
|
||||
|
||||
$this->provider->expects($this->once())
|
||||
->method('getRouteByName')
|
||||
->with($name)
|
||||
->will($this->returnValue($this->routeDocument))
|
||||
;
|
||||
$this->routeDocument->expects($this->once())
|
||||
->method('compile')
|
||||
->will($this->returnValue($this->routeCompiled))
|
||||
;
|
||||
|
||||
$this->assertEquals('result_url', $this->generator->generate($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
|
||||
*/
|
||||
public function testGenerateNotFound()
|
||||
{
|
||||
$name = 'foo/bar';
|
||||
|
||||
$this->provider->expects($this->once())
|
||||
->method('getRouteByName')
|
||||
->with($name)
|
||||
->will($this->returnValue(null))
|
||||
;
|
||||
|
||||
$this->generator->generate($name);
|
||||
}
|
||||
|
||||
public function testGenerateFromRoute()
|
||||
{
|
||||
$this->provider->expects($this->never())
|
||||
->method('getRouteByName')
|
||||
;
|
||||
$this->routeDocument->expects($this->once())
|
||||
->method('compile')
|
||||
->will($this->returnValue($this->routeCompiled))
|
||||
;
|
||||
|
||||
$this->assertEquals('result_url', $this->generator->generate($this->routeDocument));
|
||||
}
|
||||
|
||||
public function testSupports()
|
||||
{
|
||||
$this->assertTrue($this->generator->supports('foo/bar'));
|
||||
$this->assertTrue($this->generator->supports($this->routeDocument));
|
||||
$this->assertFalse($this->generator->supports($this));
|
||||
}
|
||||
|
||||
public function testGetRouteDebugMessage()
|
||||
{
|
||||
$this->assertContains('/some/key', $this->generator->getRouteDebugMessage(new RouteObject()));
|
||||
$this->assertContains('/de/test', $this->generator->getRouteDebugMessage(new Route('/de/test')));
|
||||
$this->assertContains('/some/route', $this->generator->getRouteDebugMessage('/some/route'));
|
||||
$this->assertContains('a:1:{s:10:"route_name";s:7:"example";}', $this->generator->getRouteDebugMessage(array('route_name' => 'example')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the generate method with passing in a route object into generate().
|
||||
*
|
||||
* @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
|
||||
*/
|
||||
public function testGenerateByRoute()
|
||||
{
|
||||
$this->generator = new ProviderBasedGenerator($this->provider);
|
||||
|
||||
// Setup a route with a numeric parameter, but pass in a string, so it
|
||||
// fails and getRouteDebugMessage should be triggered.
|
||||
$route = new Route('/test');
|
||||
$route->setPath('/test/{number}');
|
||||
$route->setRequirement('number', '\+d');
|
||||
|
||||
$this->generator->setStrictRequirements(true);
|
||||
|
||||
$context = new RequestContext();
|
||||
$this->generator->setContext($context);
|
||||
|
||||
$this->assertSame(null, $this->generator->generate($route, array('number' => 'string')));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwrite doGenerate to reduce amount of mocking needed
|
||||
*/
|
||||
class TestableProviderBasedGenerator extends ProviderBasedGenerator
|
||||
{
|
||||
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array())
|
||||
{
|
||||
return 'result_url';
|
||||
}
|
||||
}
|
||||
|
||||
class RouteObject implements RouteObjectInterface
|
||||
{
|
||||
public function getRouteKey()
|
||||
{
|
||||
return '/some/key';
|
||||
}
|
||||
|
||||
public function getContent()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,55 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony CMF package.
|
||||
*
|
||||
* (c) 2011-2014 Symfony CMF
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Cmf\Component\Routing\Tests\Routing;
|
||||
|
||||
use Symfony\Component\Routing\Route as SymfonyRoute;
|
||||
|
||||
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
|
||||
|
||||
class RouteMock extends SymfonyRoute implements RouteObjectInterface
|
||||
{
|
||||
private $locale;
|
||||
|
||||
public function setLocale($locale)
|
||||
{
|
||||
$this->locale = $locale;
|
||||
}
|
||||
|
||||
public function getContent()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getDefaults()
|
||||
{
|
||||
$defaults = array();
|
||||
if (! is_null($this->locale)) {
|
||||
$defaults['_locale'] = $this->locale;
|
||||
}
|
||||
|
||||
return $defaults;
|
||||
}
|
||||
|
||||
public function getRequirement($key)
|
||||
{
|
||||
if (! $key == '_locale') {
|
||||
throw new \Exception;
|
||||
}
|
||||
|
||||
return $this->locale;
|
||||
}
|
||||
|
||||
public function getRouteKey()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
Reference in a new issue