Version 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     protected $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($this->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($this->url)
146             ->will($this->returnValue($routeDefaults))
147         ;
148
149         $expected = array('this' => 'that');
150         $this->enhancer->expects($this->once())
151             ->method('enhance')
152             ->with($this->equalTo($routeDefaults), $this->equalTo($this->request))
153             ->will($this->returnValue($expected))
154         ;
155
156         $results = $this->router->match($this->url);
157
158         $this->assertEquals($expected, $results);
159     }
160
161     public function testMatchRequestWithUrlMatcher()
162     {
163         $routeDefaults = array('foo' => 'bar');
164
165         $this->matcher->expects($this->once())
166             ->method('match')
167             ->with($this->url)
168             ->will($this->returnValue($routeDefaults))
169         ;
170
171         $expected = array('this' => 'that');
172         $this->enhancer->expects($this->once())
173             ->method('enhance')
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))
177         ;
178
179         $results = $this->router->matchRequest($this->request);
180
181         $this->assertEquals($expected, $results);
182     }
183
184     public function testMatchRequest()
185     {
186         $routeDefaults = array('foo' => 'bar');
187
188         $matcher = $this->buildMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface', array('matchRequest', 'setContext', 'getContext'));
189         $router = new DynamicRouter($this->context, $matcher, $this->generator);
190
191         $matcher->expects($this->once())
192             ->method('matchRequest')
193             ->with($this->request)
194             ->will($this->returnValue($routeDefaults))
195         ;
196
197         $expected = array('this' => 'that');
198         $this->enhancer->expects($this->once())
199             ->method('enhance')
200             ->with($this->equalTo($routeDefaults), $this->equalTo($this->request))
201             ->will($this->returnValue($expected))
202         ;
203
204         $router->addRouteEnhancer($this->enhancer);
205
206         $this->assertEquals($expected, $router->matchRequest($this->request));
207     }
208
209     /**
210      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
211      * @group legacy
212      */
213     public function testMatchFilter()
214     {
215         $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '#/different/prefix.*#');
216         $router->addRouteEnhancer($this->enhancer);
217
218         $this->matcher->expects($this->never())
219             ->method('match')
220         ;
221
222         $this->enhancer->expects($this->never())
223             ->method('enhance')
224         ;
225
226         $router->match($this->url);
227     }
228
229     /**
230      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
231      */
232     public function testMatchRequestFilter()
233     {
234         $matcher = $this->buildMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface', array('matchRequest', 'setContext', 'getContext'));
235
236         $router = new DynamicRouter($this->context, $matcher, $this->generator, '#/different/prefix.*#');
237         $router->addRouteEnhancer($this->enhancer);
238
239         $matcher->expects($this->never())
240             ->method('matchRequest')
241         ;
242
243         $this->enhancer->expects($this->never())
244             ->method('enhance')
245         ;
246
247         $router->matchRequest($this->request);
248     }
249
250     /**
251      * @expectedException \InvalidArgumentException
252      * @group legacy
253      */
254     public function testMatchUrlWithRequestMatcher()
255     {
256         $matcher = $this->buildMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface', array('matchRequest', 'setContext', 'getContext'));
257         $router = new DynamicRouter($this->context, $matcher, $this->generator);
258
259         $router->match($this->url);
260     }
261
262     /**
263      * @expectedException \InvalidArgumentException
264      */
265     public function testInvalidMatcher()
266     {
267         new DynamicRouter($this->context, $this, $this->generator);
268     }
269
270     public function testRouteDebugMessage()
271     {
272         $this->generator->expects($this->once())
273             ->method('getRouteDebugMessage')
274             ->with($this->equalTo('test'), $this->equalTo(array()))
275             ->will($this->returnValue('debug message'))
276         ;
277
278         $this->assertEquals('debug message', $this->router->getRouteDebugMessage('test'));
279     }
280
281     public function testRouteDebugMessageNonversatile()
282     {
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'));
286     }
287
288     /**
289      * @group legacy
290      */
291     public function testEventHandler()
292     {
293         $eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
294         $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $eventDispatcher);
295
296         $eventDispatcher->expects($this->once())
297             ->method('dispatch')
298             ->with(Events::PRE_DYNAMIC_MATCH, $this->equalTo(new RouterMatchEvent()))
299         ;
300
301         $routeDefaults = array('foo' => 'bar');
302         $this->matcher->expects($this->once())
303             ->method('match')
304             ->with($this->url)
305             ->will($this->returnValue($routeDefaults))
306         ;
307
308         $this->assertEquals($routeDefaults, $router->match($this->url));
309     }
310
311     public function testEventHandlerRequest()
312     {
313         $eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
314         $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $eventDispatcher);
315
316         $that = $this;
317         $eventDispatcher->expects($this->once())
318             ->method('dispatch')
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());
322
323                 return true;
324             }))
325         ;
326
327         $routeDefaults = array('foo' => 'bar');
328         $this->matcher->expects($this->once())
329             ->method('match')
330             ->with($this->url)
331             ->will($this->returnValue($routeDefaults))
332         ;
333
334         $this->assertEquals($routeDefaults, $router->matchRequest($this->request));
335     }
336
337     public function testEventHandlerGenerate()
338     {
339         $eventDispatcher = $this->buildMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
340         $router = new DynamicRouter($this->context, $this->matcher, $this->generator, '', $eventDispatcher);
341
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;
348
349         $that = $this;
350         $eventDispatcher->expects($this->once())
351             ->method('dispatch')
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
356                     $that->seen = true;
357                 } else {
358                     return true;
359                 }
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);
366
367                 return true;
368             }))
369         ;
370
371         $this->generator->expects($this->once())
372             ->method('generate')
373             ->with($newname, $newparameters, $newReferenceType)
374             ->will($this->returnValue('http://test'))
375         ;
376
377         $this->assertEquals('http://test', $router->generate($oldname, $oldparameters, $oldReferenceType));
378     }
379 }