Yaffs site version 1.1
[yaffs-website] / vendor / symfony-cmf / routing / Tests / Routing / DynamicRouterTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony CMF package.
5  *
6  * (c) 2011-2015 Symfony CMF
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Cmf\Component\Routing\Tests\Routing;
13
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;
20
21 class DynamicRouterTest extends CmfUnitTestCase
22 {
23     protected $routeDocument;
24     protected $matcher;
25     protected $generator;
26     protected $enhancer;
27     /** @var DynamicRouter */
28     protected $router;
29     protected $context;
30     public $request;
31
32     const URL = '/foo/bar';
33
34     public function setUp()
35     {
36         $this->routeDocument = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults'));
37
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'));
41
42         $this->context = $this->buildMock('Symfony\Component\Routing\RequestContext');
43         $this->request = Request::create(self::URL);
44
45         $this->router = new DynamicRouter($this->context, $this->matcher, $this->generator);
46         $this->router->addRouteEnhancer($this->enhancer);
47     }
48
49     /**
50      * rather trivial, but we want 100% coverage.
51      */
52     public function testContext()
53     {
54         $this->router->setContext($this->context);
55         $this->assertSame($this->context, $this->router->getContext());
56     }
57
58     public function testRouteCollectionEmpty()
59     {
60         $collection = $this->router->getRouteCollection();
61         $this->assertInstanceOf('Symfony\Component\Routing\RouteCollection', $collection);
62     }
63
64     public function testRouteCollectionLazy()
65     {
66         $provider = $this->getMock('Symfony\Cmf\Component\Routing\RouteProviderInterface');
67         $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', null, $provider);
68
69         $collection = $router->getRouteCollection();
70         $this->assertInstanceOf('Symfony\Cmf\Component\Routing\LazyRouteCollection', $collection);
71     }
72
73     /// generator tests ///
74
75     public function testGetGenerator()
76     {
77         $this->generator->expects($this->once())
78             ->method('setContext')
79             ->with($this->equalTo($this->context));
80
81         $generator = $this->router->getGenerator();
82         $this->assertInstanceOf('Symfony\Component\Routing\Generator\UrlGeneratorInterface', $generator);
83         $this->assertSame($this->generator, $generator);
84     }
85
86     public function testGenerate()
87     {
88         $name = 'my_route_name';
89         $parameters = array('foo' => 'bar');
90         $absolute = UrlGeneratorInterface::ABSOLUTE_PATH;
91
92         $this->generator->expects($this->once())
93             ->method('generate')
94             ->with($name, $parameters, $absolute)
95             ->will($this->returnValue('http://test'))
96         ;
97
98         $url = $this->router->generate($name, $parameters, $absolute);
99         $this->assertEquals('http://test', $url);
100     }
101
102     public function testSupports()
103     {
104         $name = 'foo/bar';
105         $this->generator->expects($this->once())
106             ->method('supports')
107             ->with($this->equalTo($name))
108             ->will($this->returnValue(true))
109         ;
110
111         $this->assertTrue($this->router->supports($name));
112     }
113
114     public function testSupportsNonversatile()
115     {
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'));
119
120         $this->assertTrue($router->supports('some string'));
121         $this->assertFalse($router->supports($this));
122     }
123
124     /// match tests ///
125
126     public function testGetMatcher()
127     {
128         $this->matcher->expects($this->once())
129             ->method('setContext')
130             ->with($this->equalTo($this->context));
131
132         $matcher = $this->router->getMatcher();
133         $this->assertInstanceOf('Symfony\Component\Routing\Matcher\UrlMatcherInterface', $matcher);
134         $this->assertSame($this->matcher, $matcher);
135     }
136
137     /**
138      * @group legacy
139      */
140     public function testMatchUrl()
141     {
142         $routeDefaults = array('foo' => 'bar');
143         $this->matcher->expects($this->once())
144             ->method('match')
145             ->with(self::URL)
146             ->will($this->returnValue($routeDefaults))
147         ;
148
149         $expected = array('this' => 'that');
150         $test = $this;
151         $this->enhancer->expects($this->once())
152             ->method('enhance')
153             ->with($this->equalTo($routeDefaults), $this->callback(function (Request $request) use ($test) {
154                 return DynamicRouterTest::URL === $request->server->get('REQUEST_URI');
155             }))
156             ->will($this->returnValue($expected))
157         ;
158
159         $results = $this->router->match(self::URL);
160
161         $this->assertEquals($expected, $results);
162     }
163
164     public function testMatchRequestWithUrlMatcher()
165     {
166         $routeDefaults = array('foo' => 'bar');
167
168         $this->matcher->expects($this->once())
169             ->method('match')
170             ->with(self::URL)
171             ->will($this->returnValue($routeDefaults))
172         ;
173
174         $expected = array('this' => 'that');
175         $test = $this;
176         $this->enhancer->expects($this->once())
177             ->method('enhance')
178             ->with($this->equalTo($routeDefaults), $this->callback(function (Request $request) use ($test) {
179                 return DynamicRouterTest::URL === $request->server->get('REQUEST_URI');
180             }))
181             ->will($this->returnValue($expected))
182         ;
183
184         $results = $this->router->matchRequest($this->request);
185
186         $this->assertEquals($expected, $results);
187     }
188
189     public function testMatchRequest()
190     {
191         $routeDefaults = array('foo' => 'bar');
192
193         $matcher = $this->buildMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface', array('matchRequest', 'setContext', 'getContext'));
194         $router = new DynamicRouter($this->context, $matcher, $this->generator);
195
196         $matcher->expects($this->once())
197             ->method('matchRequest')
198             ->with($this->request)
199             ->will($this->returnValue($routeDefaults))
200         ;
201
202         $expected = array('this' => 'that');
203         $test = $this;
204         $this->enhancer->expects($this->once())
205             ->method('enhance')
206             ->with($this->equalTo($routeDefaults), $this->callback(function (Request $request) use ($test) {
207                 return DynamicRouterTest::URL === $request->server->get('REQUEST_URI');
208             }))
209             ->will($this->returnValue($expected))
210         ;
211
212         $router->addRouteEnhancer($this->enhancer);
213
214         $this->assertEquals($expected, $router->matchRequest($this->request));
215     }
216
217     /**
218      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
219      * @group legacy
220      */
221     public function testMatchFilter()
222     {
223         $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '#/different/prefix.*#');
224         $router->addRouteEnhancer($this->enhancer);
225
226         $this->matcher->expects($this->never())
227             ->method('match')
228         ;
229
230         $this->enhancer->expects($this->never())
231             ->method('enhance')
232         ;
233
234         $router->match(self::URL);
235     }
236
237     /**
238      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
239      */
240     public function testMatchRequestFilter()
241     {
242         $matcher = $this->buildMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface', array('matchRequest', 'setContext', 'getContext'));
243
244         $router = new DynamicRouter($this->context, $matcher, $this->generator, '#/different/prefix.*#');
245         $router->addRouteEnhancer($this->enhancer);
246
247         $matcher->expects($this->never())
248             ->method('matchRequest')
249         ;
250
251         $this->enhancer->expects($this->never())
252             ->method('enhance')
253         ;
254
255         $router->matchRequest($this->request);
256     }
257
258     /**
259      * @expectedException \InvalidArgumentException
260      * @group legacy
261      */
262     public function testMatchUrlWithRequestMatcher()
263     {
264         $matcher = $this->buildMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface', array('matchRequest', 'setContext', 'getContext'));
265         $router = new DynamicRouter($this->context, $matcher, $this->generator);
266
267         $router->match(self::URL);
268     }
269
270     /**
271      * @expectedException \InvalidArgumentException
272      */
273     public function testInvalidMatcher()
274     {
275         new DynamicRouter($this->context, $this, $this->generator);
276     }
277
278     public function testRouteDebugMessage()
279     {
280         $this->generator->expects($this->once())
281             ->method('getRouteDebugMessage')
282             ->with($this->equalTo('test'), $this->equalTo(array()))
283             ->will($this->returnValue('debug message'))
284         ;
285
286         $this->assertEquals('debug message', $this->router->getRouteDebugMessage('test'));
287     }
288
289     public function testRouteDebugMessageNonversatile()
290     {
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'));
294     }
295
296     /**
297      * @group legacy
298      */
299     public function testEventHandler()
300     {
301         $eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
302         $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $eventDispatcher);
303
304         $eventDispatcher->expects($this->once())
305             ->method('dispatch')
306             ->with(Events::PRE_DYNAMIC_MATCH, $this->equalTo(new RouterMatchEvent()))
307         ;
308
309         $routeDefaults = array('foo' => 'bar');
310         $this->matcher->expects($this->once())
311             ->method('match')
312             ->with(self::URL)
313             ->will($this->returnValue($routeDefaults))
314         ;
315
316         $this->assertEquals($routeDefaults, $router->match(self::URL));
317     }
318
319     public function testEventHandlerRequest()
320     {
321         $eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
322         $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $eventDispatcher);
323
324         $that = $this;
325         $eventDispatcher->expects($this->once())
326             ->method('dispatch')
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());
330
331                 return true;
332             }))
333         ;
334
335         $routeDefaults = array('foo' => 'bar');
336         $this->matcher->expects($this->once())
337             ->method('match')
338             ->with(self::URL)
339             ->will($this->returnValue($routeDefaults))
340         ;
341
342         $this->assertEquals($routeDefaults, $router->matchRequest($this->request));
343     }
344
345     public function testEventHandlerGenerate()
346     {
347         $eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
348         $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $eventDispatcher);
349
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;
356
357         $that = $this;
358         $eventDispatcher->expects($this->once())
359             ->method('dispatch')
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
364                     $that->seen = true;
365                 } else {
366                     return true;
367                 }
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);
374
375                 return true;
376             }))
377         ;
378
379         $this->generator->expects($this->once())
380             ->method('generate')
381             ->with($newname, $newparameters, $newReferenceType)
382             ->will($this->returnValue('http://test'))
383         ;
384
385         $this->assertEquals('http://test', $router->generate($oldname, $oldparameters, $oldReferenceType));
386     }
387 }