UrlMatcherTest.php 20 KB

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