0d4b44b09070490432f9d98c38429cdf036a1e47
[yaffs-website] / vendor / symfony / routing / Tests / Matcher / UrlMatcherTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
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\Component\Routing\Tests\Matcher;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
16 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
17 use Symfony\Component\Routing\Matcher\UrlMatcher;
18 use Symfony\Component\Routing\RequestContext;
19 use Symfony\Component\Routing\Route;
20 use Symfony\Component\Routing\RouteCollection;
21
22 class UrlMatcherTest extends TestCase
23 {
24     public function testNoMethodSoAllowed()
25     {
26         $coll = new RouteCollection();
27         $coll->add('foo', new Route('/foo'));
28
29         $matcher = $this->getUrlMatcher($coll);
30         $this->assertInternalType('array', $matcher->match('/foo'));
31     }
32
33     public function testMethodNotAllowed()
34     {
35         $coll = new RouteCollection();
36         $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('post')));
37
38         $matcher = $this->getUrlMatcher($coll);
39
40         try {
41             $matcher->match('/foo');
42             $this->fail();
43         } catch (MethodNotAllowedException $e) {
44             $this->assertEquals(array('POST'), $e->getAllowedMethods());
45         }
46     }
47
48     public function testMethodNotAllowedOnRoot()
49     {
50         $coll = new RouteCollection();
51         $coll->add('foo', new Route('/', array(), array(), array(), '', array(), array('GET')));
52
53         $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
54
55         try {
56             $matcher->match('/');
57             $this->fail();
58         } catch (MethodNotAllowedException $e) {
59             $this->assertEquals(array('GET'), $e->getAllowedMethods());
60         }
61     }
62
63     public function testHeadAllowedWhenRequirementContainsGet()
64     {
65         $coll = new RouteCollection();
66         $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('get')));
67
68         $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'head'));
69         $this->assertInternalType('array', $matcher->match('/foo'));
70     }
71
72     public function testMethodNotAllowedAggregatesAllowedMethods()
73     {
74         $coll = new RouteCollection();
75         $coll->add('foo1', new Route('/foo', array(), array(), array(), '', array(), array('post')));
76         $coll->add('foo2', new Route('/foo', array(), array(), array(), '', array(), array('put', 'delete')));
77
78         $matcher = $this->getUrlMatcher($coll);
79
80         try {
81             $matcher->match('/foo');
82             $this->fail();
83         } catch (MethodNotAllowedException $e) {
84             $this->assertEquals(array('POST', 'PUT', 'DELETE'), $e->getAllowedMethods());
85         }
86     }
87
88     public function testMatch()
89     {
90         // test the patterns are matched and parameters are returned
91         $collection = new RouteCollection();
92         $collection->add('foo', new Route('/foo/{bar}'));
93         $matcher = $this->getUrlMatcher($collection);
94         try {
95             $matcher->match('/no-match');
96             $this->fail();
97         } catch (ResourceNotFoundException $e) {
98         }
99         $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));
100
101         // test that defaults are merged
102         $collection = new RouteCollection();
103         $collection->add('foo', new Route('/foo/{bar}', array('def' => 'test')));
104         $matcher = $this->getUrlMatcher($collection);
105         $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz'));
106
107         // test that route "method" is ignored if no method is given in the context
108         $collection = new RouteCollection();
109         $collection->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('get', 'head')));
110         $matcher = $this->getUrlMatcher($collection);
111         $this->assertInternalType('array', $matcher->match('/foo'));
112
113         // route does not match with POST method context
114         $matcher = $this->getUrlMatcher($collection, new RequestContext('', 'post'));
115         try {
116             $matcher->match('/foo');
117             $this->fail();
118         } catch (MethodNotAllowedException $e) {
119         }
120
121         // route does match with GET or HEAD method context
122         $matcher = $this->getUrlMatcher($collection);
123         $this->assertInternalType('array', $matcher->match('/foo'));
124         $matcher = $this->getUrlMatcher($collection, new RequestContext('', 'head'));
125         $this->assertInternalType('array', $matcher->match('/foo'));
126
127         // route with an optional variable as the first segment
128         $collection = new RouteCollection();
129         $collection->add('bar', new Route('/{bar}/foo', array('bar' => 'bar'), array('bar' => 'foo|bar')));
130         $matcher = $this->getUrlMatcher($collection);
131         $this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/bar/foo'));
132         $this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo/foo'));
133
134         $collection = new RouteCollection();
135         $collection->add('bar', new Route('/{bar}', array('bar' => 'bar'), array('bar' => 'foo|bar')));
136         $matcher = $this->getUrlMatcher($collection);
137         $this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo'));
138         $this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/'));
139
140         // route with only optional variables
141         $collection = new RouteCollection();
142         $collection->add('bar', new Route('/{foo}/{bar}', array('foo' => 'foo', 'bar' => 'bar'), array()));
143         $matcher = $this->getUrlMatcher($collection);
144         $this->assertEquals(array('_route' => 'bar', 'foo' => 'foo', 'bar' => 'bar'), $matcher->match('/'));
145         $this->assertEquals(array('_route' => 'bar', 'foo' => 'a', 'bar' => 'bar'), $matcher->match('/a'));
146         $this->assertEquals(array('_route' => 'bar', 'foo' => 'a', 'bar' => 'b'), $matcher->match('/a/b'));
147     }
148
149     public function testMatchWithPrefixes()
150     {
151         $collection = new RouteCollection();
152         $collection->add('foo', new Route('/{foo}'));
153         $collection->addPrefix('/b');
154         $collection->addPrefix('/a');
155
156         $matcher = $this->getUrlMatcher($collection);
157         $this->assertEquals(array('_route' => 'foo', 'foo' => 'foo'), $matcher->match('/a/b/foo'));
158     }
159
160     public function testMatchWithDynamicPrefix()
161     {
162         $collection = new RouteCollection();
163         $collection->add('foo', new Route('/{foo}'));
164         $collection->addPrefix('/b');
165         $collection->addPrefix('/{_locale}');
166
167         $matcher = $this->getUrlMatcher($collection);
168         $this->assertEquals(array('_locale' => 'fr', '_route' => 'foo', 'foo' => 'foo'), $matcher->match('/fr/b/foo'));
169     }
170
171     public function testMatchSpecialRouteName()
172     {
173         $collection = new RouteCollection();
174         $collection->add('$péß^a|', new Route('/bar'));
175
176         $matcher = $this->getUrlMatcher($collection);
177         $this->assertEquals(array('_route' => '$péß^a|'), $matcher->match('/bar'));
178     }
179
180     /**
181      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
182      */
183     public function testTrailingEncodedNewlineIsNotOverlooked()
184     {
185         $collection = new RouteCollection();
186         $collection->add('foo', new Route('/foo'));
187
188         $matcher = $this->getUrlMatcher($collection);
189         $matcher->match('/foo%0a');
190     }
191
192     public function testMatchNonAlpha()
193     {
194         $collection = new RouteCollection();
195         $chars = '!"$%éà &\'()*+,./:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[]^_`abcdefghijklmnopqrstuvwxyz{|}~-';
196         $collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '['.preg_quote($chars).']+'), array('utf8' => true)));
197
198         $matcher = $this->getUrlMatcher($collection);
199         $this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.rawurlencode($chars).'/bar'));
200         $this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.strtr($chars, array('%' => '%25')).'/bar'));
201     }
202
203     public function testMatchWithDotMetacharacterInRequirements()
204     {
205         $collection = new RouteCollection();
206         $collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '.+')));
207
208         $matcher = $this->getUrlMatcher($collection);
209         $this->assertEquals(array('_route' => 'foo', 'foo' => "\n"), $matcher->match('/'.urlencode("\n").'/bar'), 'linefeed character is matched');
210     }
211
212     public function testMatchOverriddenRoute()
213     {
214         $collection = new RouteCollection();
215         $collection->add('foo', new Route('/foo'));
216
217         $collection1 = new RouteCollection();
218         $collection1->add('foo', new Route('/foo1'));
219
220         $collection->addCollection($collection1);
221
222         $matcher = $this->getUrlMatcher($collection);
223
224         $this->assertEquals(array('_route' => 'foo'), $matcher->match('/foo1'));
225         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Routing\Exception\ResourceNotFoundException');
226         $this->assertEquals(array(), $matcher->match('/foo'));
227     }
228
229     public function testMatchRegression()
230     {
231         $coll = new RouteCollection();
232         $coll->add('foo', new Route('/foo/{foo}'));
233         $coll->add('bar', new Route('/foo/bar/{foo}'));
234
235         $matcher = $this->getUrlMatcher($coll);
236         $this->assertEquals(array('foo' => 'bar', '_route' => 'bar'), $matcher->match('/foo/bar/bar'));
237
238         $collection = new RouteCollection();
239         $collection->add('foo', new Route('/{bar}'));
240         $matcher = $this->getUrlMatcher($collection);
241         try {
242             $matcher->match('/');
243             $this->fail();
244         } catch (ResourceNotFoundException $e) {
245         }
246     }
247
248     public function testDefaultRequirementForOptionalVariables()
249     {
250         $coll = new RouteCollection();
251         $coll->add('test', new Route('/{page}.{_format}', array('page' => 'index', '_format' => 'html')));
252
253         $matcher = $this->getUrlMatcher($coll);
254         $this->assertEquals(array('page' => 'my-page', '_format' => 'xml', '_route' => 'test'), $matcher->match('/my-page.xml'));
255     }
256
257     public function testMatchingIsEager()
258     {
259         $coll = new RouteCollection();
260         $coll->add('test', new Route('/{foo}-{bar}-', array(), array('foo' => '.+', 'bar' => '.+')));
261
262         $matcher = $this->getUrlMatcher($coll);
263         $this->assertEquals(array('foo' => 'text1-text2-text3', 'bar' => 'text4', '_route' => 'test'), $matcher->match('/text1-text2-text3-text4-'));
264     }
265
266     public function testAdjacentVariables()
267     {
268         $coll = new RouteCollection();
269         $coll->add('test', new Route('/{w}{x}{y}{z}.{_format}', array('z' => 'default-z', '_format' => 'html'), array('y' => 'y|Y')));
270
271         $matcher = $this->getUrlMatcher($coll);
272         // 'w' eagerly matches as much as possible and the other variables match the remaining chars.
273         // This also shows that the variables w-z must all exclude the separating char (the dot '.' in this case) by default requirement.
274         // Otherwise they would also consume '.xml' and _format would never match as it's an optional variable.
275         $this->assertEquals(array('w' => 'wwwww', 'x' => 'x', 'y' => 'Y', 'z' => 'Z', '_format' => 'xml', '_route' => 'test'), $matcher->match('/wwwwwxYZ.xml'));
276         // As 'y' has custom requirement and can only be of value 'y|Y', it will leave  'ZZZ' to variable z.
277         // So with carefully chosen requirements adjacent variables, can be useful.
278         $this->assertEquals(array('w' => 'wwwww', 'x' => 'x', 'y' => 'y', 'z' => 'ZZZ', '_format' => 'html', '_route' => 'test'), $matcher->match('/wwwwwxyZZZ'));
279         // z and _format are optional.
280         $this->assertEquals(array('w' => 'wwwww', 'x' => 'x', 'y' => 'y', 'z' => 'default-z', '_format' => 'html', '_route' => 'test'), $matcher->match('/wwwwwxy'));
281
282         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Routing\Exception\ResourceNotFoundException');
283         $matcher->match('/wxy.html');
284     }
285
286     public function testOptionalVariableWithNoRealSeparator()
287     {
288         $coll = new RouteCollection();
289         $coll->add('test', new Route('/get{what}', array('what' => 'All')));
290         $matcher = $this->getUrlMatcher($coll);
291
292         $this->assertEquals(array('what' => 'All', '_route' => 'test'), $matcher->match('/get'));
293         $this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSites'));
294
295         // Usually the character in front of an optional parameter can be left out, e.g. with pattern '/get/{what}' just '/get' would match.
296         // But here the 't' in 'get' is not a separating character, so it makes no sense to match without it.
297         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Routing\Exception\ResourceNotFoundException');
298         $matcher->match('/ge');
299     }
300
301     public function testRequiredVariableWithNoRealSeparator()
302     {
303         $coll = new RouteCollection();
304         $coll->add('test', new Route('/get{what}Suffix'));
305         $matcher = $this->getUrlMatcher($coll);
306
307         $this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSitesSuffix'));
308     }
309
310     public function testDefaultRequirementOfVariable()
311     {
312         $coll = new RouteCollection();
313         $coll->add('test', new Route('/{page}.{_format}'));
314         $matcher = $this->getUrlMatcher($coll);
315
316         $this->assertEquals(array('page' => 'index', '_format' => 'mobile.html', '_route' => 'test'), $matcher->match('/index.mobile.html'));
317     }
318
319     /**
320      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
321      */
322     public function testDefaultRequirementOfVariableDisallowsSlash()
323     {
324         $coll = new RouteCollection();
325         $coll->add('test', new Route('/{page}.{_format}'));
326         $matcher = $this->getUrlMatcher($coll);
327
328         $matcher->match('/index.sl/ash');
329     }
330
331     /**
332      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
333      */
334     public function testDefaultRequirementOfVariableDisallowsNextSeparator()
335     {
336         $coll = new RouteCollection();
337         $coll->add('test', new Route('/{page}.{_format}', array(), array('_format' => 'html|xml')));
338         $matcher = $this->getUrlMatcher($coll);
339
340         $matcher->match('/do.t.html');
341     }
342
343     /**
344      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
345      */
346     public function testSchemeRequirement()
347     {
348         $coll = new RouteCollection();
349         $coll->add('foo', new Route('/foo', array(), array(), array(), '', array('https')));
350         $matcher = $this->getUrlMatcher($coll);
351         $matcher->match('/foo');
352     }
353
354     /**
355      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
356      */
357     public function testCondition()
358     {
359         $coll = new RouteCollection();
360         $route = new Route('/foo');
361         $route->setCondition('context.getMethod() == "POST"');
362         $coll->add('foo', $route);
363         $matcher = $this->getUrlMatcher($coll);
364         $matcher->match('/foo');
365     }
366
367     public function testRequestCondition()
368     {
369         $coll = new RouteCollection();
370         $route = new Route('/foo/{bar}');
371         $route->setCondition('request.getBaseUrl() == "/sub/front.php" and request.getPathInfo() == "/foo/bar"');
372         $coll->add('foo', $route);
373         $matcher = $this->getUrlMatcher($coll, new RequestContext('/sub/front.php'));
374         $this->assertEquals(array('bar' => 'bar', '_route' => 'foo'), $matcher->match('/foo/bar'));
375     }
376
377     public function testDecodeOnce()
378     {
379         $coll = new RouteCollection();
380         $coll->add('foo', new Route('/foo/{foo}'));
381
382         $matcher = $this->getUrlMatcher($coll);
383         $this->assertEquals(array('foo' => 'bar%23', '_route' => 'foo'), $matcher->match('/foo/bar%2523'));
384     }
385
386     public function testCannotRelyOnPrefix()
387     {
388         $coll = new RouteCollection();
389
390         $subColl = new RouteCollection();
391         $subColl->add('bar', new Route('/bar'));
392         $subColl->addPrefix('/prefix');
393         // overwrite the pattern, so the prefix is not valid anymore for this route in the collection
394         $subColl->get('bar')->setPath('/new');
395
396         $coll->addCollection($subColl);
397
398         $matcher = $this->getUrlMatcher($coll);
399         $this->assertEquals(array('_route' => 'bar'), $matcher->match('/new'));
400     }
401
402     public function testWithHost()
403     {
404         $coll = new RouteCollection();
405         $coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
406
407         $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
408         $this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
409     }
410
411     public function testWithHostOnRouteCollection()
412     {
413         $coll = new RouteCollection();
414         $coll->add('foo', new Route('/foo/{foo}'));
415         $coll->add('bar', new Route('/bar/{foo}', array(), array(), array(), '{locale}.example.net'));
416         $coll->setHost('{locale}.example.com');
417
418         $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
419         $this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
420
421         $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
422         $this->assertEquals(array('foo' => 'bar', '_route' => 'bar', 'locale' => 'en'), $matcher->match('/bar/bar'));
423     }
424
425     /**
426      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
427      */
428     public function testWithOutHostHostDoesNotMatch()
429     {
430         $coll = new RouteCollection();
431         $coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
432
433         $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'example.com'));
434         $matcher->match('/foo/bar');
435     }
436
437     /**
438      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
439      */
440     public function testPathIsCaseSensitive()
441     {
442         $coll = new RouteCollection();
443         $coll->add('foo', new Route('/locale', array(), array('locale' => 'EN|FR|DE')));
444
445         $matcher = $this->getUrlMatcher($coll);
446         $matcher->match('/en');
447     }
448
449     public function testHostIsCaseInsensitive()
450     {
451         $coll = new RouteCollection();
452         $coll->add('foo', new Route('/', array(), array('locale' => 'EN|FR|DE'), array(), '{locale}.example.com'));
453
454         $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
455         $this->assertEquals(array('_route' => 'foo', 'locale' => 'en'), $matcher->match('/'));
456     }
457
458     /**
459      * @expectedException \Symfony\Component\Routing\Exception\NoConfigurationException
460      */
461     public function testNoConfiguration()
462     {
463         $coll = new RouteCollection();
464
465         $matcher = $this->getUrlMatcher($coll);
466         $matcher->match('/');
467     }
468
469     public function testNestedCollections()
470     {
471         $coll = new RouteCollection();
472
473         $subColl = new RouteCollection();
474         $subColl->add('a', new Route('/a'));
475         $subColl->add('b', new Route('/b'));
476         $subColl->add('c', new Route('/c'));
477         $subColl->addPrefix('/p');
478         $coll->addCollection($subColl);
479
480         $coll->add('baz', new Route('/{baz}'));
481
482         $subColl = new RouteCollection();
483         $subColl->add('buz', new Route('/buz'));
484         $subColl->addPrefix('/prefix');
485         $coll->addCollection($subColl);
486
487         $matcher = $this->getUrlMatcher($coll);
488         $this->assertEquals(array('_route' => 'a'), $matcher->match('/p/a'));
489         $this->assertEquals(array('_route' => 'baz', 'baz' => 'p'), $matcher->match('/p'));
490         $this->assertEquals(array('_route' => 'buz'), $matcher->match('/prefix/buz'));
491     }
492
493     /**
494      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
495      */
496     public function testSchemeAndMethodMismatch()
497     {
498         $coll = new RouteCollection();
499         $coll->add('foo', new Route('/', array(), array(), array(), null, array('https'), array('POST')));
500
501         $matcher = $this->getUrlMatcher($coll);
502         $matcher->match('/');
503     }
504
505     protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
506     {
507         return new UrlMatcher($routes, $context ?: new RequestContext());
508     }
509 }