6d59855d2d8f39c6f236a93c3b698eefcf109d7c
[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\Route;
19 use Symfony\Component\Routing\RouteCollection;
20 use Symfony\Component\Routing\RequestContext;
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 = new UrlMatcher($coll, new RequestContext());
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 = new UrlMatcher($coll, new RequestContext());
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 testHeadAllowedWhenRequirementContainsGet()
49     {
50         $coll = new RouteCollection();
51         $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('get')));
52
53         $matcher = new UrlMatcher($coll, new RequestContext('', 'head'));
54         $this->assertInternalType('array', $matcher->match('/foo'));
55     }
56
57     public function testMethodNotAllowedAggregatesAllowedMethods()
58     {
59         $coll = new RouteCollection();
60         $coll->add('foo1', new Route('/foo', array(), array(), array(), '', array(), array('post')));
61         $coll->add('foo2', new Route('/foo', array(), array(), array(), '', array(), array('put', 'delete')));
62
63         $matcher = new UrlMatcher($coll, new RequestContext());
64
65         try {
66             $matcher->match('/foo');
67             $this->fail();
68         } catch (MethodNotAllowedException $e) {
69             $this->assertEquals(array('POST', 'PUT', 'DELETE'), $e->getAllowedMethods());
70         }
71     }
72
73     public function testMatch()
74     {
75         // test the patterns are matched and parameters are returned
76         $collection = new RouteCollection();
77         $collection->add('foo', new Route('/foo/{bar}'));
78         $matcher = new UrlMatcher($collection, new RequestContext());
79         try {
80             $matcher->match('/no-match');
81             $this->fail();
82         } catch (ResourceNotFoundException $e) {
83         }
84         $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));
85
86         // test that defaults are merged
87         $collection = new RouteCollection();
88         $collection->add('foo', new Route('/foo/{bar}', array('def' => 'test')));
89         $matcher = new UrlMatcher($collection, new RequestContext());
90         $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz'));
91
92         // test that route "method" is ignored if no method is given in the context
93         $collection = new RouteCollection();
94         $collection->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('get', 'head')));
95         $matcher = new UrlMatcher($collection, new RequestContext());
96         $this->assertInternalType('array', $matcher->match('/foo'));
97
98         // route does not match with POST method context
99         $matcher = new UrlMatcher($collection, new RequestContext('', 'post'));
100         try {
101             $matcher->match('/foo');
102             $this->fail();
103         } catch (MethodNotAllowedException $e) {
104         }
105
106         // route does match with GET or HEAD method context
107         $matcher = new UrlMatcher($collection, new RequestContext());
108         $this->assertInternalType('array', $matcher->match('/foo'));
109         $matcher = new UrlMatcher($collection, new RequestContext('', 'head'));
110         $this->assertInternalType('array', $matcher->match('/foo'));
111
112         // route with an optional variable as the first segment
113         $collection = new RouteCollection();
114         $collection->add('bar', new Route('/{bar}/foo', array('bar' => 'bar'), array('bar' => 'foo|bar')));
115         $matcher = new UrlMatcher($collection, new RequestContext());
116         $this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/bar/foo'));
117         $this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo/foo'));
118
119         $collection = new RouteCollection();
120         $collection->add('bar', new Route('/{bar}', array('bar' => 'bar'), array('bar' => 'foo|bar')));
121         $matcher = new UrlMatcher($collection, new RequestContext());
122         $this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo'));
123         $this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/'));
124
125         // route with only optional variables
126         $collection = new RouteCollection();
127         $collection->add('bar', new Route('/{foo}/{bar}', array('foo' => 'foo', 'bar' => 'bar'), array()));
128         $matcher = new UrlMatcher($collection, new RequestContext());
129         $this->assertEquals(array('_route' => 'bar', 'foo' => 'foo', 'bar' => 'bar'), $matcher->match('/'));
130         $this->assertEquals(array('_route' => 'bar', 'foo' => 'a', 'bar' => 'bar'), $matcher->match('/a'));
131         $this->assertEquals(array('_route' => 'bar', 'foo' => 'a', 'bar' => 'b'), $matcher->match('/a/b'));
132     }
133
134     public function testMatchWithPrefixes()
135     {
136         $collection = new RouteCollection();
137         $collection->add('foo', new Route('/{foo}'));
138         $collection->addPrefix('/b');
139         $collection->addPrefix('/a');
140
141         $matcher = new UrlMatcher($collection, new RequestContext());
142         $this->assertEquals(array('_route' => 'foo', 'foo' => 'foo'), $matcher->match('/a/b/foo'));
143     }
144
145     public function testMatchWithDynamicPrefix()
146     {
147         $collection = new RouteCollection();
148         $collection->add('foo', new Route('/{foo}'));
149         $collection->addPrefix('/b');
150         $collection->addPrefix('/{_locale}');
151
152         $matcher = new UrlMatcher($collection, new RequestContext());
153         $this->assertEquals(array('_locale' => 'fr', '_route' => 'foo', 'foo' => 'foo'), $matcher->match('/fr/b/foo'));
154     }
155
156     public function testMatchSpecialRouteName()
157     {
158         $collection = new RouteCollection();
159         $collection->add('$péß^a|', new Route('/bar'));
160
161         $matcher = new UrlMatcher($collection, new RequestContext());
162         $this->assertEquals(array('_route' => '$péß^a|'), $matcher->match('/bar'));
163     }
164
165     public function testMatchNonAlpha()
166     {
167         $collection = new RouteCollection();
168         $chars = '!"$%éà &\'()*+,./:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[]^_`abcdefghijklmnopqrstuvwxyz{|}~-';
169         $collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '['.preg_quote($chars).']+')));
170
171         $matcher = new UrlMatcher($collection, new RequestContext());
172         $this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.rawurlencode($chars).'/bar'));
173         $this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.strtr($chars, array('%' => '%25')).'/bar'));
174     }
175
176     public function testMatchWithDotMetacharacterInRequirements()
177     {
178         $collection = new RouteCollection();
179         $collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '.+')));
180
181         $matcher = new UrlMatcher($collection, new RequestContext());
182         $this->assertEquals(array('_route' => 'foo', 'foo' => "\n"), $matcher->match('/'.urlencode("\n").'/bar'), 'linefeed character is matched');
183     }
184
185     public function testMatchOverriddenRoute()
186     {
187         $collection = new RouteCollection();
188         $collection->add('foo', new Route('/foo'));
189
190         $collection1 = new RouteCollection();
191         $collection1->add('foo', new Route('/foo1'));
192
193         $collection->addCollection($collection1);
194
195         $matcher = new UrlMatcher($collection, new RequestContext());
196
197         $this->assertEquals(array('_route' => 'foo'), $matcher->match('/foo1'));
198         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Routing\Exception\ResourceNotFoundException');
199         $this->assertEquals(array(), $matcher->match('/foo'));
200     }
201
202     public function testMatchRegression()
203     {
204         $coll = new RouteCollection();
205         $coll->add('foo', new Route('/foo/{foo}'));
206         $coll->add('bar', new Route('/foo/bar/{foo}'));
207
208         $matcher = new UrlMatcher($coll, new RequestContext());
209         $this->assertEquals(array('foo' => 'bar', '_route' => 'bar'), $matcher->match('/foo/bar/bar'));
210
211         $collection = new RouteCollection();
212         $collection->add('foo', new Route('/{bar}'));
213         $matcher = new UrlMatcher($collection, new RequestContext());
214         try {
215             $matcher->match('/');
216             $this->fail();
217         } catch (ResourceNotFoundException $e) {
218         }
219     }
220
221     public function testDefaultRequirementForOptionalVariables()
222     {
223         $coll = new RouteCollection();
224         $coll->add('test', new Route('/{page}.{_format}', array('page' => 'index', '_format' => 'html')));
225
226         $matcher = new UrlMatcher($coll, new RequestContext());
227         $this->assertEquals(array('page' => 'my-page', '_format' => 'xml', '_route' => 'test'), $matcher->match('/my-page.xml'));
228     }
229
230     public function testMatchingIsEager()
231     {
232         $coll = new RouteCollection();
233         $coll->add('test', new Route('/{foo}-{bar}-', array(), array('foo' => '.+', 'bar' => '.+')));
234
235         $matcher = new UrlMatcher($coll, new RequestContext());
236         $this->assertEquals(array('foo' => 'text1-text2-text3', 'bar' => 'text4', '_route' => 'test'), $matcher->match('/text1-text2-text3-text4-'));
237     }
238
239     public function testAdjacentVariables()
240     {
241         $coll = new RouteCollection();
242         $coll->add('test', new Route('/{w}{x}{y}{z}.{_format}', array('z' => 'default-z', '_format' => 'html'), array('y' => 'y|Y')));
243
244         $matcher = new UrlMatcher($coll, new RequestContext());
245         // 'w' eagerly matches as much as possible and the other variables match the remaining chars.
246         // This also shows that the variables w-z must all exclude the separating char (the dot '.' in this case) by default requirement.
247         // Otherwise they would also consume '.xml' and _format would never match as it's an optional variable.
248         $this->assertEquals(array('w' => 'wwwww', 'x' => 'x', 'y' => 'Y', 'z' => 'Z', '_format' => 'xml', '_route' => 'test'), $matcher->match('/wwwwwxYZ.xml'));
249         // As 'y' has custom requirement and can only be of value 'y|Y', it will leave  'ZZZ' to variable z.
250         // So with carefully chosen requirements adjacent variables, can be useful.
251         $this->assertEquals(array('w' => 'wwwww', 'x' => 'x', 'y' => 'y', 'z' => 'ZZZ', '_format' => 'html', '_route' => 'test'), $matcher->match('/wwwwwxyZZZ'));
252         // z and _format are optional.
253         $this->assertEquals(array('w' => 'wwwww', 'x' => 'x', 'y' => 'y', 'z' => 'default-z', '_format' => 'html', '_route' => 'test'), $matcher->match('/wwwwwxy'));
254
255         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Routing\Exception\ResourceNotFoundException');
256         $matcher->match('/wxy.html');
257     }
258
259     public function testOptionalVariableWithNoRealSeparator()
260     {
261         $coll = new RouteCollection();
262         $coll->add('test', new Route('/get{what}', array('what' => 'All')));
263         $matcher = new UrlMatcher($coll, new RequestContext());
264
265         $this->assertEquals(array('what' => 'All', '_route' => 'test'), $matcher->match('/get'));
266         $this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSites'));
267
268         // Usually the character in front of an optional parameter can be left out, e.g. with pattern '/get/{what}' just '/get' would match.
269         // But here the 't' in 'get' is not a separating character, so it makes no sense to match without it.
270         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Routing\Exception\ResourceNotFoundException');
271         $matcher->match('/ge');
272     }
273
274     public function testRequiredVariableWithNoRealSeparator()
275     {
276         $coll = new RouteCollection();
277         $coll->add('test', new Route('/get{what}Suffix'));
278         $matcher = new UrlMatcher($coll, new RequestContext());
279
280         $this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSitesSuffix'));
281     }
282
283     public function testDefaultRequirementOfVariable()
284     {
285         $coll = new RouteCollection();
286         $coll->add('test', new Route('/{page}.{_format}'));
287         $matcher = new UrlMatcher($coll, new RequestContext());
288
289         $this->assertEquals(array('page' => 'index', '_format' => 'mobile.html', '_route' => 'test'), $matcher->match('/index.mobile.html'));
290     }
291
292     /**
293      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
294      */
295     public function testDefaultRequirementOfVariableDisallowsSlash()
296     {
297         $coll = new RouteCollection();
298         $coll->add('test', new Route('/{page}.{_format}'));
299         $matcher = new UrlMatcher($coll, new RequestContext());
300
301         $matcher->match('/index.sl/ash');
302     }
303
304     /**
305      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
306      */
307     public function testDefaultRequirementOfVariableDisallowsNextSeparator()
308     {
309         $coll = new RouteCollection();
310         $coll->add('test', new Route('/{page}.{_format}', array(), array('_format' => 'html|xml')));
311         $matcher = new UrlMatcher($coll, new RequestContext());
312
313         $matcher->match('/do.t.html');
314     }
315
316     /**
317      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
318      */
319     public function testSchemeRequirement()
320     {
321         $coll = new RouteCollection();
322         $coll->add('foo', new Route('/foo', array(), array(), array(), '', array('https')));
323         $matcher = new UrlMatcher($coll, new RequestContext());
324         $matcher->match('/foo');
325     }
326
327     /**
328      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
329      */
330     public function testCondition()
331     {
332         $coll = new RouteCollection();
333         $route = new Route('/foo');
334         $route->setCondition('context.getMethod() == "POST"');
335         $coll->add('foo', $route);
336         $matcher = new UrlMatcher($coll, new RequestContext());
337         $matcher->match('/foo');
338     }
339
340     public function testDecodeOnce()
341     {
342         $coll = new RouteCollection();
343         $coll->add('foo', new Route('/foo/{foo}'));
344
345         $matcher = new UrlMatcher($coll, new RequestContext());
346         $this->assertEquals(array('foo' => 'bar%23', '_route' => 'foo'), $matcher->match('/foo/bar%2523'));
347     }
348
349     public function testCannotRelyOnPrefix()
350     {
351         $coll = new RouteCollection();
352
353         $subColl = new RouteCollection();
354         $subColl->add('bar', new Route('/bar'));
355         $subColl->addPrefix('/prefix');
356         // overwrite the pattern, so the prefix is not valid anymore for this route in the collection
357         $subColl->get('bar')->setPath('/new');
358
359         $coll->addCollection($subColl);
360
361         $matcher = new UrlMatcher($coll, new RequestContext());
362         $this->assertEquals(array('_route' => 'bar'), $matcher->match('/new'));
363     }
364
365     public function testWithHost()
366     {
367         $coll = new RouteCollection();
368         $coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
369
370         $matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
371         $this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
372     }
373
374     public function testWithHostOnRouteCollection()
375     {
376         $coll = new RouteCollection();
377         $coll->add('foo', new Route('/foo/{foo}'));
378         $coll->add('bar', new Route('/bar/{foo}', array(), array(), array(), '{locale}.example.net'));
379         $coll->setHost('{locale}.example.com');
380
381         $matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
382         $this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
383
384         $matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
385         $this->assertEquals(array('foo' => 'bar', '_route' => 'bar', 'locale' => 'en'), $matcher->match('/bar/bar'));
386     }
387
388     /**
389      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
390      */
391     public function testWithOutHostHostDoesNotMatch()
392     {
393         $coll = new RouteCollection();
394         $coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
395
396         $matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'example.com'));
397         $matcher->match('/foo/bar');
398     }
399
400     /**
401      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
402      */
403     public function testPathIsCaseSensitive()
404     {
405         $coll = new RouteCollection();
406         $coll->add('foo', new Route('/locale', array(), array('locale' => 'EN|FR|DE')));
407
408         $matcher = new UrlMatcher($coll, new RequestContext());
409         $matcher->match('/en');
410     }
411
412     public function testHostIsCaseInsensitive()
413     {
414         $coll = new RouteCollection();
415         $coll->add('foo', new Route('/', array(), array('locale' => 'EN|FR|DE'), array(), '{locale}.example.com'));
416
417         $matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
418         $this->assertEquals(array('_route' => 'foo', 'locale' => 'en'), $matcher->match('/'));
419     }
420 }