4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Tests\Matcher;
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;
22 class UrlMatcherTest extends TestCase
24 public function testNoMethodSoAllowed()
26 $coll = new RouteCollection();
27 $coll->add('foo', new Route('/foo'));
29 $matcher = $this->getUrlMatcher($coll);
30 $this->assertInternalType('array', $matcher->match('/foo'));
33 public function testMethodNotAllowed()
35 $coll = new RouteCollection();
36 $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('post')));
38 $matcher = $this->getUrlMatcher($coll);
41 $matcher->match('/foo');
43 } catch (MethodNotAllowedException $e) {
44 $this->assertEquals(array('POST'), $e->getAllowedMethods());
48 public function testMethodNotAllowedOnRoot()
50 $coll = new RouteCollection();
51 $coll->add('foo', new Route('/', array(), array(), array(), '', array(), array('GET')));
53 $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
58 } catch (MethodNotAllowedException $e) {
59 $this->assertEquals(array('GET'), $e->getAllowedMethods());
63 public function testHeadAllowedWhenRequirementContainsGet()
65 $coll = new RouteCollection();
66 $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('get')));
68 $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'head'));
69 $this->assertInternalType('array', $matcher->match('/foo'));
72 public function testMethodNotAllowedAggregatesAllowedMethods()
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')));
78 $matcher = $this->getUrlMatcher($coll);
81 $matcher->match('/foo');
83 } catch (MethodNotAllowedException $e) {
84 $this->assertEquals(array('POST', 'PUT', 'DELETE'), $e->getAllowedMethods());
88 public function testMatch()
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);
95 $matcher->match('/no-match');
97 } catch (ResourceNotFoundException $e) {
99 $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));
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'));
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'));
113 // route does not match with POST method context
114 $matcher = $this->getUrlMatcher($collection, new RequestContext('', 'post'));
116 $matcher->match('/foo');
118 } catch (MethodNotAllowedException $e) {
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'));
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'));
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('/'));
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'));
149 public function testMatchWithPrefixes()
151 $collection = new RouteCollection();
152 $collection->add('foo', new Route('/{foo}'));
153 $collection->addPrefix('/b');
154 $collection->addPrefix('/a');
156 $matcher = $this->getUrlMatcher($collection);
157 $this->assertEquals(array('_route' => 'foo', 'foo' => 'foo'), $matcher->match('/a/b/foo'));
160 public function testMatchWithDynamicPrefix()
162 $collection = new RouteCollection();
163 $collection->add('foo', new Route('/{foo}'));
164 $collection->addPrefix('/b');
165 $collection->addPrefix('/{_locale}');
167 $matcher = $this->getUrlMatcher($collection);
168 $this->assertEquals(array('_locale' => 'fr', '_route' => 'foo', 'foo' => 'foo'), $matcher->match('/fr/b/foo'));
171 public function testMatchSpecialRouteName()
173 $collection = new RouteCollection();
174 $collection->add('$péß^a|', new Route('/bar'));
176 $matcher = $this->getUrlMatcher($collection);
177 $this->assertEquals(array('_route' => '$péß^a|'), $matcher->match('/bar'));
181 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
183 public function testTrailingEncodedNewlineIsNotOverlooked()
185 $collection = new RouteCollection();
186 $collection->add('foo', new Route('/foo'));
188 $matcher = $this->getUrlMatcher($collection);
189 $matcher->match('/foo%0a');
192 public function testMatchNonAlpha()
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)));
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'));
203 public function testMatchWithDotMetacharacterInRequirements()
205 $collection = new RouteCollection();
206 $collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '.+')));
208 $matcher = $this->getUrlMatcher($collection);
209 $this->assertEquals(array('_route' => 'foo', 'foo' => "\n"), $matcher->match('/'.urlencode("\n").'/bar'), 'linefeed character is matched');
212 public function testMatchOverriddenRoute()
214 $collection = new RouteCollection();
215 $collection->add('foo', new Route('/foo'));
217 $collection1 = new RouteCollection();
218 $collection1->add('foo', new Route('/foo1'));
220 $collection->addCollection($collection1);
222 $matcher = $this->getUrlMatcher($collection);
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'));
229 public function testMatchRegression()
231 $coll = new RouteCollection();
232 $coll->add('foo', new Route('/foo/{foo}'));
233 $coll->add('bar', new Route('/foo/bar/{foo}'));
235 $matcher = $this->getUrlMatcher($coll);
236 $this->assertEquals(array('foo' => 'bar', '_route' => 'bar'), $matcher->match('/foo/bar/bar'));
238 $collection = new RouteCollection();
239 $collection->add('foo', new Route('/{bar}'));
240 $matcher = $this->getUrlMatcher($collection);
242 $matcher->match('/');
244 } catch (ResourceNotFoundException $e) {
248 public function testDefaultRequirementForOptionalVariables()
250 $coll = new RouteCollection();
251 $coll->add('test', new Route('/{page}.{_format}', array('page' => 'index', '_format' => 'html')));
253 $matcher = $this->getUrlMatcher($coll);
254 $this->assertEquals(array('page' => 'my-page', '_format' => 'xml', '_route' => 'test'), $matcher->match('/my-page.xml'));
257 public function testMatchingIsEager()
259 $coll = new RouteCollection();
260 $coll->add('test', new Route('/{foo}-{bar}-', array(), array('foo' => '.+', 'bar' => '.+')));
262 $matcher = $this->getUrlMatcher($coll);
263 $this->assertEquals(array('foo' => 'text1-text2-text3', 'bar' => 'text4', '_route' => 'test'), $matcher->match('/text1-text2-text3-text4-'));
266 public function testAdjacentVariables()
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')));
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'));
282 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Routing\Exception\ResourceNotFoundException');
283 $matcher->match('/wxy.html');
286 public function testOptionalVariableWithNoRealSeparator()
288 $coll = new RouteCollection();
289 $coll->add('test', new Route('/get{what}', array('what' => 'All')));
290 $matcher = $this->getUrlMatcher($coll);
292 $this->assertEquals(array('what' => 'All', '_route' => 'test'), $matcher->match('/get'));
293 $this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSites'));
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');
301 public function testRequiredVariableWithNoRealSeparator()
303 $coll = new RouteCollection();
304 $coll->add('test', new Route('/get{what}Suffix'));
305 $matcher = $this->getUrlMatcher($coll);
307 $this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSitesSuffix'));
310 public function testDefaultRequirementOfVariable()
312 $coll = new RouteCollection();
313 $coll->add('test', new Route('/{page}.{_format}'));
314 $matcher = $this->getUrlMatcher($coll);
316 $this->assertEquals(array('page' => 'index', '_format' => 'mobile.html', '_route' => 'test'), $matcher->match('/index.mobile.html'));
320 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
322 public function testDefaultRequirementOfVariableDisallowsSlash()
324 $coll = new RouteCollection();
325 $coll->add('test', new Route('/{page}.{_format}'));
326 $matcher = $this->getUrlMatcher($coll);
328 $matcher->match('/index.sl/ash');
332 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
334 public function testDefaultRequirementOfVariableDisallowsNextSeparator()
336 $coll = new RouteCollection();
337 $coll->add('test', new Route('/{page}.{_format}', array(), array('_format' => 'html|xml')));
338 $matcher = $this->getUrlMatcher($coll);
340 $matcher->match('/do.t.html');
344 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
346 public function testSchemeRequirement()
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');
355 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
357 public function testCondition()
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');
367 public function testRequestCondition()
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'));
377 public function testDecodeOnce()
379 $coll = new RouteCollection();
380 $coll->add('foo', new Route('/foo/{foo}'));
382 $matcher = $this->getUrlMatcher($coll);
383 $this->assertEquals(array('foo' => 'bar%23', '_route' => 'foo'), $matcher->match('/foo/bar%2523'));
386 public function testCannotRelyOnPrefix()
388 $coll = new RouteCollection();
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');
396 $coll->addCollection($subColl);
398 $matcher = $this->getUrlMatcher($coll);
399 $this->assertEquals(array('_route' => 'bar'), $matcher->match('/new'));
402 public function testWithHost()
404 $coll = new RouteCollection();
405 $coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
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'));
411 public function testWithHostOnRouteCollection()
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');
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'));
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'));
426 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
428 public function testWithOutHostHostDoesNotMatch()
430 $coll = new RouteCollection();
431 $coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
433 $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'example.com'));
434 $matcher->match('/foo/bar');
438 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
440 public function testPathIsCaseSensitive()
442 $coll = new RouteCollection();
443 $coll->add('foo', new Route('/locale', array(), array('locale' => 'EN|FR|DE')));
445 $matcher = $this->getUrlMatcher($coll);
446 $matcher->match('/en');
449 public function testHostIsCaseInsensitive()
451 $coll = new RouteCollection();
452 $coll->add('foo', new Route('/', array(), array('locale' => 'EN|FR|DE'), array(), '{locale}.example.com'));
454 $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
455 $this->assertEquals(array('_route' => 'foo', 'locale' => 'en'), $matcher->match('/'));
459 * @expectedException \Symfony\Component\Routing\Exception\NoConfigurationException
461 public function testNoConfiguration()
463 $coll = new RouteCollection();
465 $matcher = $this->getUrlMatcher($coll);
466 $matcher->match('/');
469 public function testNestedCollections()
471 $coll = new RouteCollection();
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);
480 $coll->add('baz', new Route('/{baz}'));
482 $subColl = new RouteCollection();
483 $subColl->add('buz', new Route('/buz'));
484 $subColl->addPrefix('/prefix');
485 $coll->addCollection($subColl);
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'));
494 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
496 public function testSchemeAndMethodMismatch()
498 $coll = new RouteCollection();
499 $coll->add('foo', new Route('/', array(), array(), array(), null, array('https'), array('POST')));
501 $matcher = $this->getUrlMatcher($coll);
502 $matcher->match('/');
505 protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
507 return new UrlMatcher($routes, $context ?: new RequestContext());