UrlMatcherTest.php 20 KB

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