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\Route;
19 use Symfony\Component\Routing\RouteCollection;
20 use Symfony\Component\Routing\RequestContext;
22 class UrlMatcherTest extends TestCase
24 public function testNoMethodSoAllowed()
26 $coll = new RouteCollection();
27 $coll->add('foo', new Route('/foo'));
29 $matcher = new UrlMatcher($coll, new RequestContext());
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 = new UrlMatcher($coll, new RequestContext());
41 $matcher->match('/foo');
43 } catch (MethodNotAllowedException $e) {
44 $this->assertEquals(array('POST'), $e->getAllowedMethods());
48 public function testHeadAllowedWhenRequirementContainsGet()
50 $coll = new RouteCollection();
51 $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('get')));
53 $matcher = new UrlMatcher($coll, new RequestContext('', 'head'));
54 $this->assertInternalType('array', $matcher->match('/foo'));
57 public function testMethodNotAllowedAggregatesAllowedMethods()
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')));
63 $matcher = new UrlMatcher($coll, new RequestContext());
66 $matcher->match('/foo');
68 } catch (MethodNotAllowedException $e) {
69 $this->assertEquals(array('POST', 'PUT', 'DELETE'), $e->getAllowedMethods());
73 public function testMatch()
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());
80 $matcher->match('/no-match');
82 } catch (ResourceNotFoundException $e) {
84 $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));
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'));
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'));
98 // route does not match with POST method context
99 $matcher = new UrlMatcher($collection, new RequestContext('', 'post'));
101 $matcher->match('/foo');
103 } catch (MethodNotAllowedException $e) {
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'));
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'));
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('/'));
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'));
134 public function testMatchWithPrefixes()
136 $collection = new RouteCollection();
137 $collection->add('foo', new Route('/{foo}'));
138 $collection->addPrefix('/b');
139 $collection->addPrefix('/a');
141 $matcher = new UrlMatcher($collection, new RequestContext());
142 $this->assertEquals(array('_route' => 'foo', 'foo' => 'foo'), $matcher->match('/a/b/foo'));
145 public function testMatchWithDynamicPrefix()
147 $collection = new RouteCollection();
148 $collection->add('foo', new Route('/{foo}'));
149 $collection->addPrefix('/b');
150 $collection->addPrefix('/{_locale}');
152 $matcher = new UrlMatcher($collection, new RequestContext());
153 $this->assertEquals(array('_locale' => 'fr', '_route' => 'foo', 'foo' => 'foo'), $matcher->match('/fr/b/foo'));
156 public function testMatchSpecialRouteName()
158 $collection = new RouteCollection();
159 $collection->add('$péß^a|', new Route('/bar'));
161 $matcher = new UrlMatcher($collection, new RequestContext());
162 $this->assertEquals(array('_route' => '$péß^a|'), $matcher->match('/bar'));
165 public function testMatchNonAlpha()
167 $collection = new RouteCollection();
168 $chars = '!"$%éà &\'()*+,./:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[]^_`abcdefghijklmnopqrstuvwxyz{|}~-';
169 $collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '['.preg_quote($chars).']+')));
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'));
176 public function testMatchWithDotMetacharacterInRequirements()
178 $collection = new RouteCollection();
179 $collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '.+')));
181 $matcher = new UrlMatcher($collection, new RequestContext());
182 $this->assertEquals(array('_route' => 'foo', 'foo' => "\n"), $matcher->match('/'.urlencode("\n").'/bar'), 'linefeed character is matched');
185 public function testMatchOverriddenRoute()
187 $collection = new RouteCollection();
188 $collection->add('foo', new Route('/foo'));
190 $collection1 = new RouteCollection();
191 $collection1->add('foo', new Route('/foo1'));
193 $collection->addCollection($collection1);
195 $matcher = new UrlMatcher($collection, new RequestContext());
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'));
202 public function testMatchRegression()
204 $coll = new RouteCollection();
205 $coll->add('foo', new Route('/foo/{foo}'));
206 $coll->add('bar', new Route('/foo/bar/{foo}'));
208 $matcher = new UrlMatcher($coll, new RequestContext());
209 $this->assertEquals(array('foo' => 'bar', '_route' => 'bar'), $matcher->match('/foo/bar/bar'));
211 $collection = new RouteCollection();
212 $collection->add('foo', new Route('/{bar}'));
213 $matcher = new UrlMatcher($collection, new RequestContext());
215 $matcher->match('/');
217 } catch (ResourceNotFoundException $e) {
221 public function testDefaultRequirementForOptionalVariables()
223 $coll = new RouteCollection();
224 $coll->add('test', new Route('/{page}.{_format}', array('page' => 'index', '_format' => 'html')));
226 $matcher = new UrlMatcher($coll, new RequestContext());
227 $this->assertEquals(array('page' => 'my-page', '_format' => 'xml', '_route' => 'test'), $matcher->match('/my-page.xml'));
230 public function testMatchingIsEager()
232 $coll = new RouteCollection();
233 $coll->add('test', new Route('/{foo}-{bar}-', array(), array('foo' => '.+', 'bar' => '.+')));
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-'));
239 public function testAdjacentVariables()
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')));
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'));
255 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Routing\Exception\ResourceNotFoundException');
256 $matcher->match('/wxy.html');
259 public function testOptionalVariableWithNoRealSeparator()
261 $coll = new RouteCollection();
262 $coll->add('test', new Route('/get{what}', array('what' => 'All')));
263 $matcher = new UrlMatcher($coll, new RequestContext());
265 $this->assertEquals(array('what' => 'All', '_route' => 'test'), $matcher->match('/get'));
266 $this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSites'));
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');
274 public function testRequiredVariableWithNoRealSeparator()
276 $coll = new RouteCollection();
277 $coll->add('test', new Route('/get{what}Suffix'));
278 $matcher = new UrlMatcher($coll, new RequestContext());
280 $this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSitesSuffix'));
283 public function testDefaultRequirementOfVariable()
285 $coll = new RouteCollection();
286 $coll->add('test', new Route('/{page}.{_format}'));
287 $matcher = new UrlMatcher($coll, new RequestContext());
289 $this->assertEquals(array('page' => 'index', '_format' => 'mobile.html', '_route' => 'test'), $matcher->match('/index.mobile.html'));
293 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
295 public function testDefaultRequirementOfVariableDisallowsSlash()
297 $coll = new RouteCollection();
298 $coll->add('test', new Route('/{page}.{_format}'));
299 $matcher = new UrlMatcher($coll, new RequestContext());
301 $matcher->match('/index.sl/ash');
305 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
307 public function testDefaultRequirementOfVariableDisallowsNextSeparator()
309 $coll = new RouteCollection();
310 $coll->add('test', new Route('/{page}.{_format}', array(), array('_format' => 'html|xml')));
311 $matcher = new UrlMatcher($coll, new RequestContext());
313 $matcher->match('/do.t.html');
317 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
319 public function testSchemeRequirement()
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');
328 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
330 public function testCondition()
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');
340 public function testDecodeOnce()
342 $coll = new RouteCollection();
343 $coll->add('foo', new Route('/foo/{foo}'));
345 $matcher = new UrlMatcher($coll, new RequestContext());
346 $this->assertEquals(array('foo' => 'bar%23', '_route' => 'foo'), $matcher->match('/foo/bar%2523'));
349 public function testCannotRelyOnPrefix()
351 $coll = new RouteCollection();
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');
359 $coll->addCollection($subColl);
361 $matcher = new UrlMatcher($coll, new RequestContext());
362 $this->assertEquals(array('_route' => 'bar'), $matcher->match('/new'));
365 public function testWithHost()
367 $coll = new RouteCollection();
368 $coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
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'));
374 public function testWithHostOnRouteCollection()
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');
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'));
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'));
389 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
391 public function testWithOutHostHostDoesNotMatch()
393 $coll = new RouteCollection();
394 $coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
396 $matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'example.com'));
397 $matcher->match('/foo/bar');
401 * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
403 public function testPathIsCaseSensitive()
405 $coll = new RouteCollection();
406 $coll->add('foo', new Route('/locale', array(), array('locale' => 'EN|FR|DE')));
408 $matcher = new UrlMatcher($coll, new RequestContext());
409 $matcher->match('/en');
412 public function testHostIsCaseInsensitive()
414 $coll = new RouteCollection();
415 $coll->add('foo', new Route('/', array(), array('locale' => 'EN|FR|DE'), array(), '{locale}.example.com'));
417 $matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
418 $this->assertEquals(array('_route' => 'foo', 'locale' => 'en'), $matcher->match('/'));