4 * This file is part of the Symfony CMF package.
6 * (c) 2011-2015 Symfony CMF
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Cmf\Component\Routing\Tests\Routing;
14 use Symfony\Cmf\Component\Routing\Event\Events;
15 use Symfony\Cmf\Component\Routing\Event\RouterMatchEvent;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
18 use Symfony\Cmf\Component\Routing\DynamicRouter;
19 use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
21 class DynamicRouterTest extends CmfUnitTestCase
23 protected $routeDocument;
27 /** @var DynamicRouter */
32 protected $url = '/foo/bar';
34 public function setUp()
36 $this->routeDocument = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults'));
38 $this->matcher = $this->buildMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
39 $this->generator = $this->buildMock('Symfony\Cmf\Component\Routing\VersatileGeneratorInterface', array('supports', 'generate', 'setContext', 'getContext', 'getRouteDebugMessage'));
40 $this->enhancer = $this->buildMock('Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface', array('enhance'));
42 $this->context = $this->buildMock('Symfony\Component\Routing\RequestContext');
43 $this->request = Request::create($this->url);
45 $this->router = new DynamicRouter($this->context, $this->matcher, $this->generator);
46 $this->router->addRouteEnhancer($this->enhancer);
50 * rather trivial, but we want 100% coverage.
52 public function testContext()
54 $this->router->setContext($this->context);
55 $this->assertSame($this->context, $this->router->getContext());
58 public function testRouteCollectionEmpty()
60 $collection = $this->router->getRouteCollection();
61 $this->assertInstanceOf('Symfony\Component\Routing\RouteCollection', $collection);
64 public function testRouteCollectionLazy()
66 $provider = $this->getMock('Symfony\Cmf\Component\Routing\RouteProviderInterface');
67 $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', null, $provider);
69 $collection = $router->getRouteCollection();
70 $this->assertInstanceOf('Symfony\Cmf\Component\Routing\LazyRouteCollection', $collection);
73 /// generator tests ///
75 public function testGetGenerator()
77 $this->generator->expects($this->once())
78 ->method('setContext')
79 ->with($this->equalTo($this->context));
81 $generator = $this->router->getGenerator();
82 $this->assertInstanceOf('Symfony\Component\Routing\Generator\UrlGeneratorInterface', $generator);
83 $this->assertSame($this->generator, $generator);
86 public function testGenerate()
88 $name = 'my_route_name';
89 $parameters = array('foo' => 'bar');
90 $absolute = UrlGeneratorInterface::ABSOLUTE_PATH;
92 $this->generator->expects($this->once())
94 ->with($name, $parameters, $absolute)
95 ->will($this->returnValue('http://test'))
98 $url = $this->router->generate($name, $parameters, $absolute);
99 $this->assertEquals('http://test', $url);
102 public function testSupports()
105 $this->generator->expects($this->once())
107 ->with($this->equalTo($name))
108 ->will($this->returnValue(true))
111 $this->assertTrue($this->router->supports($name));
114 public function testSupportsNonversatile()
116 $generator = $this->buildMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface', array('generate', 'setContext', 'getContext'));
117 $router = new DynamicRouter($this->context, $this->matcher, $generator);
118 $this->assertInternalType('string', $router->getRouteDebugMessage('test'));
120 $this->assertTrue($router->supports('some string'));
121 $this->assertFalse($router->supports($this));
126 public function testGetMatcher()
128 $this->matcher->expects($this->once())
129 ->method('setContext')
130 ->with($this->equalTo($this->context));
132 $matcher = $this->router->getMatcher();
133 $this->assertInstanceOf('Symfony\Component\Routing\Matcher\UrlMatcherInterface', $matcher);
134 $this->assertSame($this->matcher, $matcher);
140 public function testMatchUrl()
142 $routeDefaults = array('foo' => 'bar');
143 $this->matcher->expects($this->once())
146 ->will($this->returnValue($routeDefaults))
149 $expected = array('this' => 'that');
150 $this->enhancer->expects($this->once())
152 ->with($this->equalTo($routeDefaults), $this->equalTo($this->request))
153 ->will($this->returnValue($expected))
156 $results = $this->router->match($this->url);
158 $this->assertEquals($expected, $results);
161 public function testMatchRequestWithUrlMatcher()
163 $routeDefaults = array('foo' => 'bar');
165 $this->matcher->expects($this->once())
168 ->will($this->returnValue($routeDefaults))
171 $expected = array('this' => 'that');
172 $this->enhancer->expects($this->once())
174 // somehow request object gets confused, check on instance only
175 ->with($this->equalTo($routeDefaults), $this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
176 ->will($this->returnValue($expected))
179 $results = $this->router->matchRequest($this->request);
181 $this->assertEquals($expected, $results);
184 public function testMatchRequest()
186 $routeDefaults = array('foo' => 'bar');
188 $matcher = $this->buildMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface', array('matchRequest', 'setContext', 'getContext'));
189 $router = new DynamicRouter($this->context, $matcher, $this->generator);
191 $matcher->expects($this->once())
192 ->method('matchRequest')
193 ->with($this->request)
194 ->will($this->returnValue($routeDefaults))
197 $expected = array('this' => 'that');
198 $this->enhancer->expects($this->once())
200 ->with($this->equalTo($routeDefaults), $this->equalTo($this->request))
201 ->will($this->returnValue($expected))
204 $router->addRouteEnhancer($this->enhancer);
206 $this->assertEquals($expected, $router->matchRequest($this->request));
210 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
213 public function testMatchFilter()
215 $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '#/different/prefix.*#');
216 $router->addRouteEnhancer($this->enhancer);
218 $this->matcher->expects($this->never())
222 $this->enhancer->expects($this->never())
226 $router->match($this->url);
230 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
232 public function testMatchRequestFilter()
234 $matcher = $this->buildMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface', array('matchRequest', 'setContext', 'getContext'));
236 $router = new DynamicRouter($this->context, $matcher, $this->generator, '#/different/prefix.*#');
237 $router->addRouteEnhancer($this->enhancer);
239 $matcher->expects($this->never())
240 ->method('matchRequest')
243 $this->enhancer->expects($this->never())
247 $router->matchRequest($this->request);
251 * @expectedException \InvalidArgumentException
254 public function testMatchUrlWithRequestMatcher()
256 $matcher = $this->buildMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface', array('matchRequest', 'setContext', 'getContext'));
257 $router = new DynamicRouter($this->context, $matcher, $this->generator);
259 $router->match($this->url);
263 * @expectedException \InvalidArgumentException
265 public function testInvalidMatcher()
267 new DynamicRouter($this->context, $this, $this->generator);
270 public function testRouteDebugMessage()
272 $this->generator->expects($this->once())
273 ->method('getRouteDebugMessage')
274 ->with($this->equalTo('test'), $this->equalTo(array()))
275 ->will($this->returnValue('debug message'))
278 $this->assertEquals('debug message', $this->router->getRouteDebugMessage('test'));
281 public function testRouteDebugMessageNonversatile()
283 $generator = $this->buildMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface', array('generate', 'setContext', 'getContext'));
284 $router = new DynamicRouter($this->context, $this->matcher, $generator);
285 $this->assertInternalType('string', $router->getRouteDebugMessage('test'));
291 public function testEventHandler()
293 $eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
294 $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $eventDispatcher);
296 $eventDispatcher->expects($this->once())
298 ->with(Events::PRE_DYNAMIC_MATCH, $this->equalTo(new RouterMatchEvent()))
301 $routeDefaults = array('foo' => 'bar');
302 $this->matcher->expects($this->once())
305 ->will($this->returnValue($routeDefaults))
308 $this->assertEquals($routeDefaults, $router->match($this->url));
311 public function testEventHandlerRequest()
313 $eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
314 $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $eventDispatcher);
317 $eventDispatcher->expects($this->once())
319 ->with(Events::PRE_DYNAMIC_MATCH_REQUEST, $this->callback(function ($event) use ($that) {
320 $that->assertInstanceOf('Symfony\Cmf\Component\Routing\Event\RouterMatchEvent', $event);
321 $that->assertEquals($that->request, $event->getRequest());
327 $routeDefaults = array('foo' => 'bar');
328 $this->matcher->expects($this->once())
331 ->will($this->returnValue($routeDefaults))
334 $this->assertEquals($routeDefaults, $router->matchRequest($this->request));
337 public function testEventHandlerGenerate()
339 $eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
340 $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $eventDispatcher);
342 $oldname = 'old_route_name';
343 $newname = 'new_route_name';
344 $oldparameters = array('foo' => 'bar');
345 $newparameters = array('a' => 'b');
346 $oldReferenceType = false;
347 $newReferenceType = true;
350 $eventDispatcher->expects($this->once())
352 ->with(Events::PRE_DYNAMIC_GENERATE, $this->callback(function ($event) use ($that, $oldname, $newname, $oldparameters, $newparameters, $oldReferenceType, $newReferenceType) {
353 $that->assertInstanceOf('Symfony\Cmf\Component\Routing\Event\RouterGenerateEvent', $event);
354 if (empty($that->seen)) {
355 // phpunit is calling the callback twice, and because we update the event the second time fails
360 $that->assertEquals($oldname, $event->getRoute());
361 $that->assertEquals($oldparameters, $event->getParameters());
362 $that->assertEquals($oldReferenceType, $event->getReferenceType());
363 $event->setRoute($newname);
364 $event->setParameters($newparameters);
365 $event->setReferenceType($newReferenceType);
371 $this->generator->expects($this->once())
373 ->with($newname, $newparameters, $newReferenceType)
374 ->will($this->returnValue('http://test'))
377 $this->assertEquals('http://test', $router->generate($oldname, $oldparameters, $oldReferenceType));