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 const 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(self::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');
151 $this->enhancer->expects($this->once())
153 ->with($this->equalTo($routeDefaults), $this->callback(function (Request $request) use ($test) {
154 return DynamicRouterTest::URL === $request->server->get('REQUEST_URI');
156 ->will($this->returnValue($expected))
159 $results = $this->router->match(self::URL);
161 $this->assertEquals($expected, $results);
164 public function testMatchRequestWithUrlMatcher()
166 $routeDefaults = array('foo' => 'bar');
168 $this->matcher->expects($this->once())
171 ->will($this->returnValue($routeDefaults))
174 $expected = array('this' => 'that');
176 $this->enhancer->expects($this->once())
178 ->with($this->equalTo($routeDefaults), $this->callback(function (Request $request) use ($test) {
179 return DynamicRouterTest::URL === $request->server->get('REQUEST_URI');
181 ->will($this->returnValue($expected))
184 $results = $this->router->matchRequest($this->request);
186 $this->assertEquals($expected, $results);
189 public function testMatchRequest()
191 $routeDefaults = array('foo' => 'bar');
193 $matcher = $this->buildMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface', array('matchRequest', 'setContext', 'getContext'));
194 $router = new DynamicRouter($this->context, $matcher, $this->generator);
196 $matcher->expects($this->once())
197 ->method('matchRequest')
198 ->with($this->request)
199 ->will($this->returnValue($routeDefaults))
202 $expected = array('this' => 'that');
204 $this->enhancer->expects($this->once())
206 ->with($this->equalTo($routeDefaults), $this->callback(function (Request $request) use ($test) {
207 return DynamicRouterTest::URL === $request->server->get('REQUEST_URI');
209 ->will($this->returnValue($expected))
212 $router->addRouteEnhancer($this->enhancer);
214 $this->assertEquals($expected, $router->matchRequest($this->request));
218 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
221 public function testMatchFilter()
223 $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '#/different/prefix.*#');
224 $router->addRouteEnhancer($this->enhancer);
226 $this->matcher->expects($this->never())
230 $this->enhancer->expects($this->never())
234 $router->match(self::URL);
238 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
240 public function testMatchRequestFilter()
242 $matcher = $this->buildMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface', array('matchRequest', 'setContext', 'getContext'));
244 $router = new DynamicRouter($this->context, $matcher, $this->generator, '#/different/prefix.*#');
245 $router->addRouteEnhancer($this->enhancer);
247 $matcher->expects($this->never())
248 ->method('matchRequest')
251 $this->enhancer->expects($this->never())
255 $router->matchRequest($this->request);
259 * @expectedException \InvalidArgumentException
262 public function testMatchUrlWithRequestMatcher()
264 $matcher = $this->buildMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface', array('matchRequest', 'setContext', 'getContext'));
265 $router = new DynamicRouter($this->context, $matcher, $this->generator);
267 $router->match(self::URL);
271 * @expectedException \InvalidArgumentException
273 public function testInvalidMatcher()
275 new DynamicRouter($this->context, $this, $this->generator);
278 public function testRouteDebugMessage()
280 $this->generator->expects($this->once())
281 ->method('getRouteDebugMessage')
282 ->with($this->equalTo('test'), $this->equalTo(array()))
283 ->will($this->returnValue('debug message'))
286 $this->assertEquals('debug message', $this->router->getRouteDebugMessage('test'));
289 public function testRouteDebugMessageNonversatile()
291 $generator = $this->buildMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface', array('generate', 'setContext', 'getContext'));
292 $router = new DynamicRouter($this->context, $this->matcher, $generator);
293 $this->assertInternalType('string', $router->getRouteDebugMessage('test'));
299 public function testEventHandler()
301 $eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
302 $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $eventDispatcher);
304 $eventDispatcher->expects($this->once())
306 ->with(Events::PRE_DYNAMIC_MATCH, $this->equalTo(new RouterMatchEvent()))
309 $routeDefaults = array('foo' => 'bar');
310 $this->matcher->expects($this->once())
313 ->will($this->returnValue($routeDefaults))
316 $this->assertEquals($routeDefaults, $router->match(self::URL));
319 public function testEventHandlerRequest()
321 $eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
322 $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $eventDispatcher);
325 $eventDispatcher->expects($this->once())
327 ->with(Events::PRE_DYNAMIC_MATCH_REQUEST, $this->callback(function ($event) use ($that) {
328 $that->assertInstanceOf('Symfony\Cmf\Component\Routing\Event\RouterMatchEvent', $event);
329 $that->assertEquals($that->request, $event->getRequest());
335 $routeDefaults = array('foo' => 'bar');
336 $this->matcher->expects($this->once())
339 ->will($this->returnValue($routeDefaults))
342 $this->assertEquals($routeDefaults, $router->matchRequest($this->request));
345 public function testEventHandlerGenerate()
347 $eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
348 $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $eventDispatcher);
350 $oldname = 'old_route_name';
351 $newname = 'new_route_name';
352 $oldparameters = array('foo' => 'bar');
353 $newparameters = array('a' => 'b');
354 $oldReferenceType = false;
355 $newReferenceType = true;
358 $eventDispatcher->expects($this->once())
360 ->with(Events::PRE_DYNAMIC_GENERATE, $this->callback(function ($event) use ($that, $oldname, $newname, $oldparameters, $newparameters, $oldReferenceType, $newReferenceType) {
361 $that->assertInstanceOf('Symfony\Cmf\Component\Routing\Event\RouterGenerateEvent', $event);
362 if (empty($that->seen)) {
363 // phpunit is calling the callback twice, and because we update the event the second time fails
368 $that->assertEquals($oldname, $event->getRoute());
369 $that->assertEquals($oldparameters, $event->getParameters());
370 $that->assertEquals($oldReferenceType, $event->getReferenceType());
371 $event->setRoute($newname);
372 $event->setParameters($newparameters);
373 $event->setReferenceType($newReferenceType);
379 $this->generator->expects($this->once())
381 ->with($newname, $newparameters, $newReferenceType)
382 ->will($this->returnValue('http://test'))
385 $this->assertEquals('http://test', $router->generate($oldname, $oldparameters, $oldReferenceType));