UrlMatcherTest.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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\NoConfigurationException;
  14. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  15. use Symfony\Component\Routing\Matcher\UrlMatcher;
  16. use Symfony\Component\Routing\RequestContext;
  17. use Symfony\Component\Routing\Route;
  18. use Symfony\Component\Routing\RouteCollection;
  19. class UrlMatcherTest extends TestCase
  20. {
  21. public function testNoMethodSoAllowed()
  22. {
  23. $coll = new RouteCollection();
  24. $coll->add('foo', new Route('/foo'));
  25. $matcher = $this->getUrlMatcher($coll);
  26. $this->assertIsArray($matcher->match('/foo'));
  27. }
  28. public function testMethodNotAllowed()
  29. {
  30. $coll = new RouteCollection();
  31. $coll->add('foo', new Route('/foo', [], [], [], '', [], ['post']));
  32. $matcher = $this->getUrlMatcher($coll);
  33. try {
  34. $matcher->match('/foo');
  35. $this->fail();
  36. } catch (MethodNotAllowedException $e) {
  37. $this->assertEquals(['POST'], $e->getAllowedMethods());
  38. }
  39. }
  40. public function testMethodNotAllowedOnRoot()
  41. {
  42. $coll = new RouteCollection();
  43. $coll->add('foo', new Route('/', [], [], [], '', [], ['GET']));
  44. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
  45. try {
  46. $matcher->match('/');
  47. $this->fail();
  48. } catch (MethodNotAllowedException $e) {
  49. $this->assertEquals(['GET'], $e->getAllowedMethods());
  50. }
  51. }
  52. public function testHeadAllowedWhenRequirementContainsGet()
  53. {
  54. $coll = new RouteCollection();
  55. $coll->add('foo', new Route('/foo', [], [], [], '', [], ['get']));
  56. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'head'));
  57. $this->assertIsArray($matcher->match('/foo'));
  58. }
  59. public function testMethodNotAllowedAggregatesAllowedMethods()
  60. {
  61. $coll = new RouteCollection();
  62. $coll->add('foo1', new Route('/foo', [], [], [], '', [], ['post']));
  63. $coll->add('foo2', new Route('/foo', [], [], [], '', [], ['put', 'delete']));
  64. $matcher = $this->getUrlMatcher($coll);
  65. try {
  66. $matcher->match('/foo');
  67. $this->fail();
  68. } catch (MethodNotAllowedException $e) {
  69. $this->assertEquals(['POST', 'PUT', 'DELETE'], $e->getAllowedMethods());
  70. }
  71. }
  72. public function testPatternMatchAndParameterReturn()
  73. {
  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(['_route' => 'foo', 'bar' => 'baz'], $matcher->match('/foo/baz'));
  83. }
  84. public function testDefaultsAreMerged()
  85. {
  86. // test that defaults are merged
  87. $collection = new RouteCollection();
  88. $collection->add('foo', new Route('/foo/{bar}', ['def' => 'test']));
  89. $matcher = $this->getUrlMatcher($collection);
  90. $this->assertEquals(['_route' => 'foo', 'bar' => 'baz', 'def' => 'test'], $matcher->match('/foo/baz'));
  91. }
  92. public function testMethodIsIgnoredIfNoMethodGiven()
  93. {
  94. // test that route "method" is ignored if no method is given in the context
  95. $collection = new RouteCollection();
  96. $collection->add('foo', new Route('/foo', [], [], [], '', [], ['get', 'head']));
  97. $matcher = $this->getUrlMatcher($collection);
  98. $this->assertIsArray($matcher->match('/foo'));
  99. // route does not match with POST method context
  100. $matcher = $this->getUrlMatcher($collection, new RequestContext('', 'post'));
  101. try {
  102. $matcher->match('/foo');
  103. $this->fail();
  104. } catch (MethodNotAllowedException $e) {
  105. }
  106. // route does match with GET or HEAD method context
  107. $matcher = $this->getUrlMatcher($collection);
  108. $this->assertIsArray($matcher->match('/foo'));
  109. $matcher = $this->getUrlMatcher($collection, new RequestContext('', 'head'));
  110. $this->assertIsArray($matcher->match('/foo'));
  111. }
  112. public function testRouteWithOptionalVariableAsFirstSegment()
  113. {
  114. $collection = new RouteCollection();
  115. $collection->add('bar', new Route('/{bar}/foo', ['bar' => 'bar'], ['bar' => 'foo|bar']));
  116. $matcher = $this->getUrlMatcher($collection);
  117. $this->assertEquals(['_route' => 'bar', 'bar' => 'bar'], $matcher->match('/bar/foo'));
  118. $this->assertEquals(['_route' => 'bar', 'bar' => 'foo'], $matcher->match('/foo/foo'));
  119. $collection = new RouteCollection();
  120. $collection->add('bar', new Route('/{bar}', ['bar' => 'bar'], ['bar' => 'foo|bar']));
  121. $matcher = $this->getUrlMatcher($collection);
  122. $this->assertEquals(['_route' => 'bar', 'bar' => 'foo'], $matcher->match('/foo'));
  123. $this->assertEquals(['_route' => 'bar', 'bar' => 'bar'], $matcher->match('/'));
  124. }
  125. public function testRouteWithOnlyOptionalVariables()
  126. {
  127. $collection = new RouteCollection();
  128. $collection->add('bar', new Route('/{foo}/{bar}', ['foo' => 'foo', 'bar' => 'bar'], []));
  129. $matcher = $this->getUrlMatcher($collection);
  130. $this->assertEquals(['_route' => 'bar', 'foo' => 'foo', 'bar' => 'bar'], $matcher->match('/'));
  131. $this->assertEquals(['_route' => 'bar', 'foo' => 'a', 'bar' => 'bar'], $matcher->match('/a'));
  132. $this->assertEquals(['_route' => 'bar', 'foo' => 'a', 'bar' => 'b'], $matcher->match('/a/b'));
  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. $matcher = $this->getUrlMatcher($collection);
  141. $this->assertEquals(['_route' => 'foo', 'foo' => 'foo'], $matcher->match('/a/b/foo'));
  142. }
  143. public function testMatchWithDynamicPrefix()
  144. {
  145. $collection = new RouteCollection();
  146. $collection->add('foo', new Route('/{foo}'));
  147. $collection->addPrefix('/b');
  148. $collection->addPrefix('/{_locale}');
  149. $matcher = $this->getUrlMatcher($collection);
  150. $this->assertEquals(['_locale' => 'fr', '_route' => 'foo', 'foo' => 'foo'], $matcher->match('/fr/b/foo'));
  151. }
  152. public function testMatchSpecialRouteName()
  153. {
  154. $collection = new RouteCollection();
  155. $collection->add('$péß^a|', new Route('/bar'));
  156. $matcher = $this->getUrlMatcher($collection);
  157. $this->assertEquals(['_route' => '$péß^a|'], $matcher->match('/bar'));
  158. }
  159. public function testMatchImportantVariable()
  160. {
  161. $collection = new RouteCollection();
  162. $collection->add('index', new Route('/index.{!_format}', ['_format' => 'xml']));
  163. $matcher = $this->getUrlMatcher($collection);
  164. $this->assertEquals(['_route' => 'index', '_format' => 'xml'], $matcher->match('/index.xml'));
  165. }
  166. public function testShortPathDoesNotMatchImportantVariable()
  167. {
  168. $this->expectException(ResourceNotFoundException::class);
  169. $collection = new RouteCollection();
  170. $collection->add('index', new Route('/index.{!_format}', ['_format' => 'xml']));
  171. $this->getUrlMatcher($collection)->match('/index');
  172. }
  173. public function testTrailingEncodedNewlineIsNotOverlooked()
  174. {
  175. $this->expectException(ResourceNotFoundException::class);
  176. $collection = new RouteCollection();
  177. $collection->add('foo', new Route('/foo'));
  178. $matcher = $this->getUrlMatcher($collection);
  179. $matcher->match('/foo%0a');
  180. }
  181. public function testMatchNonAlpha()
  182. {
  183. $collection = new RouteCollection();
  184. $chars = '!"$%éà &\'()*+,./:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[]^_`abcdefghijklmnopqrstuvwxyz{|}~-';
  185. $collection->add('foo', new Route('/{foo}/bar', [], ['foo' => '['.preg_quote($chars).']+'], ['utf8' => true]));
  186. $matcher = $this->getUrlMatcher($collection);
  187. $this->assertEquals(['_route' => 'foo', 'foo' => $chars], $matcher->match('/'.rawurlencode($chars).'/bar'));
  188. $this->assertEquals(['_route' => 'foo', 'foo' => $chars], $matcher->match('/'.strtr($chars, ['%' => '%25']).'/bar'));
  189. }
  190. public function testMatchWithDotMetacharacterInRequirements()
  191. {
  192. $collection = new RouteCollection();
  193. $collection->add('foo', new Route('/{foo}/bar', [], ['foo' => '.+']));
  194. $matcher = $this->getUrlMatcher($collection);
  195. $this->assertEquals(['_route' => 'foo', 'foo' => "\n"], $matcher->match('/'.urlencode("\n").'/bar'), 'linefeed character is matched');
  196. }
  197. public function testMatchOverriddenRoute()
  198. {
  199. $collection = new RouteCollection();
  200. $collection->add('foo', new Route('/foo'));
  201. $collection1 = new RouteCollection();
  202. $collection1->add('foo', new Route('/foo1'));
  203. $collection->addCollection($collection1);
  204. $matcher = $this->getUrlMatcher($collection);
  205. $this->assertEquals(['_route' => 'foo'], $matcher->match('/foo1'));
  206. $this->expectException(ResourceNotFoundException::class);
  207. $this->assertEquals([], $matcher->match('/foo'));
  208. }
  209. public function testMatchRegression()
  210. {
  211. $coll = new RouteCollection();
  212. $coll->add('foo', new Route('/foo/{foo}'));
  213. $coll->add('bar', new Route('/foo/bar/{foo}'));
  214. $matcher = $this->getUrlMatcher($coll);
  215. $this->assertEquals(['foo' => 'bar', '_route' => 'bar'], $matcher->match('/foo/bar/bar'));
  216. $collection = new RouteCollection();
  217. $collection->add('foo', new Route('/{bar}'));
  218. $matcher = $this->getUrlMatcher($collection);
  219. try {
  220. $matcher->match('/');
  221. $this->fail();
  222. } catch (ResourceNotFoundException $e) {
  223. }
  224. }
  225. public function testMultipleParams()
  226. {
  227. $coll = new RouteCollection();
  228. $coll->add('foo1', new Route('/foo/{a}/{b}'));
  229. $coll->add('foo2', new Route('/foo/{a}/test/test/{b}'));
  230. $coll->add('foo3', new Route('/foo/{a}/{b}/{c}/{d}'));
  231. $route = $this->getUrlMatcher($coll)->match('/foo/test/test/test/bar')['_route'];
  232. $this->assertEquals('foo2', $route);
  233. }
  234. public function testDefaultRequirementForOptionalVariables()
  235. {
  236. $coll = new RouteCollection();
  237. $coll->add('test', new Route('/{page}.{_format}', ['page' => 'index', '_format' => 'html']));
  238. $matcher = $this->getUrlMatcher($coll);
  239. $this->assertEquals(['page' => 'my-page', '_format' => 'xml', '_route' => 'test'], $matcher->match('/my-page.xml'));
  240. }
  241. public function testMatchingIsEager()
  242. {
  243. $coll = new RouteCollection();
  244. $coll->add('test', new Route('/{foo}-{bar}-', [], ['foo' => '.+', 'bar' => '.+']));
  245. $matcher = $this->getUrlMatcher($coll);
  246. $this->assertEquals(['foo' => 'text1-text2-text3', 'bar' => 'text4', '_route' => 'test'], $matcher->match('/text1-text2-text3-text4-'));
  247. }
  248. public function testAdjacentVariables()
  249. {
  250. $coll = new RouteCollection();
  251. $coll->add('test', new Route('/{w}{x}{y}{z}.{_format}', ['z' => 'default-z', '_format' => 'html'], ['y' => 'y|Y']));
  252. $matcher = $this->getUrlMatcher($coll);
  253. // 'w' eagerly matches as much as possible and the other variables match the remaining chars.
  254. // This also shows that the variables w-z must all exclude the separating char (the dot '.' in this case) by default requirement.
  255. // Otherwise they would also consume '.xml' and _format would never match as it's an optional variable.
  256. $this->assertEquals(['w' => 'wwwww', 'x' => 'x', 'y' => 'Y', 'z' => 'Z', '_format' => 'xml', '_route' => 'test'], $matcher->match('/wwwwwxYZ.xml'));
  257. // As 'y' has custom requirement and can only be of value 'y|Y', it will leave 'ZZZ' to variable z.
  258. // So with carefully chosen requirements adjacent variables, can be useful.
  259. $this->assertEquals(['w' => 'wwwww', 'x' => 'x', 'y' => 'y', 'z' => 'ZZZ', '_format' => 'html', '_route' => 'test'], $matcher->match('/wwwwwxyZZZ'));
  260. // z and _format are optional.
  261. $this->assertEquals(['w' => 'wwwww', 'x' => 'x', 'y' => 'y', 'z' => 'default-z', '_format' => 'html', '_route' => 'test'], $matcher->match('/wwwwwxy'));
  262. $this->expectException(ResourceNotFoundException::class);
  263. $matcher->match('/wxy.html');
  264. }
  265. public function testOptionalVariableWithNoRealSeparator()
  266. {
  267. $coll = new RouteCollection();
  268. $coll->add('test', new Route('/get{what}', ['what' => 'All']));
  269. $matcher = $this->getUrlMatcher($coll);
  270. $this->assertEquals(['what' => 'All', '_route' => 'test'], $matcher->match('/get'));
  271. $this->assertEquals(['what' => 'Sites', '_route' => 'test'], $matcher->match('/getSites'));
  272. // Usually the character in front of an optional parameter can be left out, e.g. with pattern '/get/{what}' just '/get' would match.
  273. // But here the 't' in 'get' is not a separating character, so it makes no sense to match without it.
  274. $this->expectException(ResourceNotFoundException::class);
  275. $matcher->match('/ge');
  276. }
  277. public function testRequiredVariableWithNoRealSeparator()
  278. {
  279. $coll = new RouteCollection();
  280. $coll->add('test', new Route('/get{what}Suffix'));
  281. $matcher = $this->getUrlMatcher($coll);
  282. $this->assertEquals(['what' => 'Sites', '_route' => 'test'], $matcher->match('/getSitesSuffix'));
  283. }
  284. public function testDefaultRequirementOfVariable()
  285. {
  286. $coll = new RouteCollection();
  287. $coll->add('test', new Route('/{page}.{_format}'));
  288. $matcher = $this->getUrlMatcher($coll);
  289. $this->assertEquals(['page' => 'index', '_format' => 'mobile.html', '_route' => 'test'], $matcher->match('/index.mobile.html'));
  290. }
  291. public function testDefaultRequirementOfVariableDisallowsSlash()
  292. {
  293. $this->expectException(ResourceNotFoundException::class);
  294. $coll = new RouteCollection();
  295. $coll->add('test', new Route('/{page}.{_format}'));
  296. $matcher = $this->getUrlMatcher($coll);
  297. $matcher->match('/index.sl/ash');
  298. }
  299. public function testDefaultRequirementOfVariableDisallowsNextSeparator()
  300. {
  301. $this->expectException(ResourceNotFoundException::class);
  302. $coll = new RouteCollection();
  303. $coll->add('test', new Route('/{page}.{_format}', [], ['_format' => 'html|xml']));
  304. $matcher = $this->getUrlMatcher($coll);
  305. $matcher->match('/do.t.html');
  306. }
  307. public function testMissingTrailingSlash()
  308. {
  309. $this->expectException(ResourceNotFoundException::class);
  310. $coll = new RouteCollection();
  311. $coll->add('foo', new Route('/foo/'));
  312. $matcher = $this->getUrlMatcher($coll);
  313. $matcher->match('/foo');
  314. }
  315. public function testExtraTrailingSlash()
  316. {
  317. $this->getExpectedException() ?: $this->expectException(ResourceNotFoundException::class);
  318. $coll = new RouteCollection();
  319. $coll->add('foo', new Route('/foo'));
  320. $matcher = $this->getUrlMatcher($coll);
  321. $matcher->match('/foo/');
  322. }
  323. public function testMissingTrailingSlashForNonSafeMethod()
  324. {
  325. $this->getExpectedException() ?: $this->expectException(ResourceNotFoundException::class);
  326. $coll = new RouteCollection();
  327. $coll->add('foo', new Route('/foo/'));
  328. $context = new RequestContext();
  329. $context->setMethod('POST');
  330. $matcher = $this->getUrlMatcher($coll, $context);
  331. $matcher->match('/foo');
  332. }
  333. public function testExtraTrailingSlashForNonSafeMethod()
  334. {
  335. $this->getExpectedException() ?: $this->expectException(ResourceNotFoundException::class);
  336. $coll = new RouteCollection();
  337. $coll->add('foo', new Route('/foo'));
  338. $context = new RequestContext();
  339. $context->setMethod('POST');
  340. $matcher = $this->getUrlMatcher($coll, $context);
  341. $matcher->match('/foo/');
  342. }
  343. public function testSchemeRequirement()
  344. {
  345. $this->getExpectedException() ?: $this->expectException(ResourceNotFoundException::class);
  346. $coll = new RouteCollection();
  347. $coll->add('foo', new Route('/foo', [], [], [], '', ['https']));
  348. $matcher = $this->getUrlMatcher($coll);
  349. $matcher->match('/foo');
  350. }
  351. public function testSchemeRequirementForNonSafeMethod()
  352. {
  353. $this->getExpectedException() ?: $this->expectException(ResourceNotFoundException::class);
  354. $coll = new RouteCollection();
  355. $coll->add('foo', new Route('/foo', [], [], [], '', ['https']));
  356. $context = new RequestContext();
  357. $context->setMethod('POST');
  358. $matcher = $this->getUrlMatcher($coll, $context);
  359. $matcher->match('/foo');
  360. }
  361. public function testSamePathWithDifferentScheme()
  362. {
  363. $coll = new RouteCollection();
  364. $coll->add('https_route', new Route('/', [], [], [], '', ['https']));
  365. $coll->add('http_route', new Route('/', [], [], [], '', ['http']));
  366. $matcher = $this->getUrlMatcher($coll);
  367. $this->assertEquals(['_route' => 'http_route'], $matcher->match('/'));
  368. }
  369. public function testCondition()
  370. {
  371. $this->expectException(ResourceNotFoundException::class);
  372. $coll = new RouteCollection();
  373. $route = new Route('/foo');
  374. $route->setCondition('context.getMethod() == "POST"');
  375. $coll->add('foo', $route);
  376. $matcher = $this->getUrlMatcher($coll);
  377. $matcher->match('/foo');
  378. }
  379. public function testRequestCondition()
  380. {
  381. $coll = new RouteCollection();
  382. $route = new Route('/foo/{bar}');
  383. $route->setCondition('request.getBaseUrl() == "/bar"');
  384. $coll->add('bar', $route);
  385. $route = new Route('/foo/{bar}');
  386. $route->setCondition('request.getBaseUrl() == "/sub/front.php" and request.getPathInfo() == "/foo/bar"');
  387. $coll->add('foo', $route);
  388. $matcher = $this->getUrlMatcher($coll, new RequestContext('/sub/front.php'));
  389. $this->assertEquals(['bar' => 'bar', '_route' => 'foo'], $matcher->match('/foo/bar'));
  390. }
  391. public function testDecodeOnce()
  392. {
  393. $coll = new RouteCollection();
  394. $coll->add('foo', new Route('/foo/{foo}'));
  395. $matcher = $this->getUrlMatcher($coll);
  396. $this->assertEquals(['foo' => 'bar%23', '_route' => 'foo'], $matcher->match('/foo/bar%2523'));
  397. }
  398. public function testCannotRelyOnPrefix()
  399. {
  400. $coll = new RouteCollection();
  401. $subColl = new RouteCollection();
  402. $subColl->add('bar', new Route('/bar'));
  403. $subColl->addPrefix('/prefix');
  404. // overwrite the pattern, so the prefix is not valid anymore for this route in the collection
  405. $subColl->get('bar')->setPath('/new');
  406. $coll->addCollection($subColl);
  407. $matcher = $this->getUrlMatcher($coll);
  408. $this->assertEquals(['_route' => 'bar'], $matcher->match('/new'));
  409. }
  410. public function testWithHost()
  411. {
  412. $coll = new RouteCollection();
  413. $coll->add('foo', new Route('/foo/{foo}', [], [], [], '{locale}.example.com'));
  414. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
  415. $this->assertEquals(['foo' => 'bar', '_route' => 'foo', 'locale' => 'en'], $matcher->match('/foo/bar'));
  416. }
  417. public function testWithHostOnRouteCollection()
  418. {
  419. $coll = new RouteCollection();
  420. $coll->add('foo', new Route('/foo/{foo}'));
  421. $coll->add('bar', new Route('/bar/{foo}', [], [], [], '{locale}.example.net'));
  422. $coll->setHost('{locale}.example.com');
  423. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
  424. $this->assertEquals(['foo' => 'bar', '_route' => 'foo', 'locale' => 'en'], $matcher->match('/foo/bar'));
  425. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
  426. $this->assertEquals(['foo' => 'bar', '_route' => 'bar', 'locale' => 'en'], $matcher->match('/bar/bar'));
  427. }
  428. public function testVariationInTrailingSlashWithHosts()
  429. {
  430. $coll = new RouteCollection();
  431. $coll->add('foo', new Route('/foo/', [], [], [], 'foo.example.com'));
  432. $coll->add('bar', new Route('/foo', [], [], [], 'bar.example.com'));
  433. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'foo.example.com'));
  434. $this->assertEquals(['_route' => 'foo'], $matcher->match('/foo/'));
  435. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'bar.example.com'));
  436. $this->assertEquals(['_route' => 'bar'], $matcher->match('/foo'));
  437. }
  438. public function testVariationInTrailingSlashWithHostsInReverse()
  439. {
  440. // The order should not matter
  441. $coll = new RouteCollection();
  442. $coll->add('bar', new Route('/foo', [], [], [], 'bar.example.com'));
  443. $coll->add('foo', new Route('/foo/', [], [], [], 'foo.example.com'));
  444. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'foo.example.com'));
  445. $this->assertEquals(['_route' => 'foo'], $matcher->match('/foo/'));
  446. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'bar.example.com'));
  447. $this->assertEquals(['_route' => 'bar'], $matcher->match('/foo'));
  448. }
  449. public function testVariationInTrailingSlashWithHostsAndVariable()
  450. {
  451. $coll = new RouteCollection();
  452. $coll->add('foo', new Route('/{foo}/', [], [], [], 'foo.example.com'));
  453. $coll->add('bar', new Route('/{foo}', [], [], [], 'bar.example.com'));
  454. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'foo.example.com'));
  455. $this->assertEquals(['foo' => 'bar', '_route' => 'foo'], $matcher->match('/bar/'));
  456. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'bar.example.com'));
  457. $this->assertEquals(['foo' => 'bar', '_route' => 'bar'], $matcher->match('/bar'));
  458. }
  459. public function testVariationInTrailingSlashWithHostsAndVariableInReverse()
  460. {
  461. // The order should not matter
  462. $coll = new RouteCollection();
  463. $coll->add('bar', new Route('/{foo}', [], [], [], 'bar.example.com'));
  464. $coll->add('foo', new Route('/{foo}/', [], [], [], 'foo.example.com'));
  465. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'foo.example.com'));
  466. $this->assertEquals(['foo' => 'bar', '_route' => 'foo'], $matcher->match('/bar/'));
  467. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'bar.example.com'));
  468. $this->assertEquals(['foo' => 'bar', '_route' => 'bar'], $matcher->match('/bar'));
  469. }
  470. public function testVariationInTrailingSlashWithMethods()
  471. {
  472. $coll = new RouteCollection();
  473. $coll->add('foo', new Route('/foo/', [], [], [], '', [], ['POST']));
  474. $coll->add('bar', new Route('/foo', [], [], [], '', [], ['GET']));
  475. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
  476. $this->assertEquals(['_route' => 'foo'], $matcher->match('/foo/'));
  477. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET'));
  478. $this->assertEquals(['_route' => 'bar'], $matcher->match('/foo'));
  479. }
  480. public function testVariationInTrailingSlashWithMethodsInReverse()
  481. {
  482. // The order should not matter
  483. $coll = new RouteCollection();
  484. $coll->add('bar', new Route('/foo', [], [], [], '', [], ['GET']));
  485. $coll->add('foo', new Route('/foo/', [], [], [], '', [], ['POST']));
  486. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
  487. $this->assertEquals(['_route' => 'foo'], $matcher->match('/foo/'));
  488. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET'));
  489. $this->assertEquals(['_route' => 'bar'], $matcher->match('/foo'));
  490. }
  491. public function testVariableVariationInTrailingSlashWithMethods()
  492. {
  493. $coll = new RouteCollection();
  494. $coll->add('foo', new Route('/{foo}/', [], [], [], '', [], ['POST']));
  495. $coll->add('bar', new Route('/{foo}', [], [], [], '', [], ['GET']));
  496. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
  497. $this->assertEquals(['foo' => 'bar', '_route' => 'foo'], $matcher->match('/bar/'));
  498. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET'));
  499. $this->assertEquals(['foo' => 'bar', '_route' => 'bar'], $matcher->match('/bar'));
  500. }
  501. public function testVariableVariationInTrailingSlashWithMethodsInReverse()
  502. {
  503. // The order should not matter
  504. $coll = new RouteCollection();
  505. $coll->add('bar', new Route('/{foo}', [], [], [], '', [], ['GET']));
  506. $coll->add('foo', new Route('/{foo}/', [], [], [], '', [], ['POST']));
  507. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
  508. $this->assertEquals(['foo' => 'bar', '_route' => 'foo'], $matcher->match('/bar/'));
  509. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET'));
  510. $this->assertEquals(['foo' => 'bar', '_route' => 'bar'], $matcher->match('/bar'));
  511. }
  512. public function testMixOfStaticAndVariableVariationInTrailingSlashWithHosts()
  513. {
  514. $coll = new RouteCollection();
  515. $coll->add('foo', new Route('/foo/', [], [], [], 'foo.example.com'));
  516. $coll->add('bar', new Route('/{foo}', [], [], [], 'bar.example.com'));
  517. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'foo.example.com'));
  518. $this->assertEquals(['_route' => 'foo'], $matcher->match('/foo/'));
  519. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'bar.example.com'));
  520. $this->assertEquals(['foo' => 'bar', '_route' => 'bar'], $matcher->match('/bar'));
  521. }
  522. public function testMixOfStaticAndVariableVariationInTrailingSlashWithMethods()
  523. {
  524. $coll = new RouteCollection();
  525. $coll->add('foo', new Route('/foo/', [], [], [], '', [], ['POST']));
  526. $coll->add('bar', new Route('/{foo}', [], [], [], '', [], ['GET']));
  527. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
  528. $this->assertEquals(['_route' => 'foo'], $matcher->match('/foo/'));
  529. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET'));
  530. $this->assertEquals(['foo' => 'bar', '_route' => 'bar'], $matcher->match('/bar'));
  531. $this->assertEquals(['foo' => 'foo', '_route' => 'bar'], $matcher->match('/foo'));
  532. }
  533. public function testWithOutHostHostDoesNotMatch()
  534. {
  535. $this->expectException(ResourceNotFoundException::class);
  536. $coll = new RouteCollection();
  537. $coll->add('foo', new Route('/foo/{foo}', [], [], [], '{locale}.example.com'));
  538. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'example.com'));
  539. $matcher->match('/foo/bar');
  540. }
  541. public function testPathIsCaseSensitive()
  542. {
  543. $this->expectException(ResourceNotFoundException::class);
  544. $coll = new RouteCollection();
  545. $coll->add('foo', new Route('/locale', [], ['locale' => 'EN|FR|DE']));
  546. $matcher = $this->getUrlMatcher($coll);
  547. $matcher->match('/en');
  548. }
  549. public function testHostIsCaseInsensitive()
  550. {
  551. $coll = new RouteCollection();
  552. $coll->add('foo', new Route('/', [], ['locale' => 'EN|FR|DE'], [], '{locale}.example.com'));
  553. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
  554. $this->assertEquals(['_route' => 'foo', 'locale' => 'en'], $matcher->match('/'));
  555. }
  556. public function testNoConfiguration()
  557. {
  558. $this->expectException(NoConfigurationException::class);
  559. $coll = new RouteCollection();
  560. $matcher = $this->getUrlMatcher($coll);
  561. $matcher->match('/');
  562. }
  563. public function testNestedCollections()
  564. {
  565. $coll = new RouteCollection();
  566. $subColl = new RouteCollection();
  567. $subColl->add('a', new Route('/a'));
  568. $subColl->add('b', new Route('/b'));
  569. $subColl->add('c', new Route('/c'));
  570. $subColl->addPrefix('/p');
  571. $coll->addCollection($subColl);
  572. $coll->add('baz', new Route('/{baz}'));
  573. $subColl = new RouteCollection();
  574. $subColl->add('buz', new Route('/buz'));
  575. $subColl->addPrefix('/prefix');
  576. $coll->addCollection($subColl);
  577. $matcher = $this->getUrlMatcher($coll);
  578. $this->assertEquals(['_route' => 'a'], $matcher->match('/p/a'));
  579. $this->assertEquals(['_route' => 'baz', 'baz' => 'p'], $matcher->match('/p'));
  580. $this->assertEquals(['_route' => 'buz'], $matcher->match('/prefix/buz'));
  581. }
  582. public function testSchemeAndMethodMismatch()
  583. {
  584. $this->expectException(ResourceNotFoundException::class);
  585. $this->expectExceptionMessage('No routes found for "/".');
  586. $coll = new RouteCollection();
  587. $coll->add('foo', new Route('/', [], [], [], null, ['https'], ['POST']));
  588. $matcher = $this->getUrlMatcher($coll);
  589. $matcher->match('/');
  590. }
  591. public function testSiblingRoutes()
  592. {
  593. $coll = new RouteCollection();
  594. $coll->add('a', (new Route('/a{a}'))->setMethods('POST'));
  595. $coll->add('b', (new Route('/a{a}'))->setMethods('PUT'));
  596. $coll->add('c', new Route('/a{a}'));
  597. $coll->add('d', (new Route('/b{a}'))->setCondition('false'));
  598. $coll->add('e', (new Route('/{b}{a}'))->setCondition('false'));
  599. $coll->add('f', (new Route('/{b}{a}'))->setRequirements(['b' => 'b']));
  600. $matcher = $this->getUrlMatcher($coll);
  601. $this->assertEquals(['_route' => 'c', 'a' => 'a'], $matcher->match('/aa'));
  602. $this->assertEquals(['_route' => 'f', 'b' => 'b', 'a' => 'a'], $matcher->match('/ba'));
  603. }
  604. public function testUnicodeRoute()
  605. {
  606. $coll = new RouteCollection();
  607. $coll->add('a', new Route('/{a}', [], ['a' => '.'], ['utf8' => false]));
  608. $coll->add('b', new Route('/{a}', [], ['a' => '.'], ['utf8' => true]));
  609. $matcher = $this->getUrlMatcher($coll);
  610. $this->assertEquals(['_route' => 'b', 'a' => 'é'], $matcher->match('/é'));
  611. }
  612. public function testRequirementWithCapturingGroup()
  613. {
  614. $coll = new RouteCollection();
  615. $coll->add('a', new Route('/{a}/{b}', [], ['a' => '(a|b)']));
  616. $matcher = $this->getUrlMatcher($coll);
  617. $this->assertEquals(['_route' => 'a', 'a' => 'a', 'b' => 'b'], $matcher->match('/a/b'));
  618. }
  619. public function testDotAllWithCatchAll()
  620. {
  621. $coll = new RouteCollection();
  622. $coll->add('a', new Route('/{id}.html', [], ['id' => '.+']));
  623. $coll->add('b', new Route('/{all}', [], ['all' => '.+']));
  624. $matcher = $this->getUrlMatcher($coll);
  625. $this->assertEquals(['_route' => 'a', 'id' => 'foo/bar'], $matcher->match('/foo/bar.html'));
  626. }
  627. public function testHostPattern()
  628. {
  629. $coll = new RouteCollection();
  630. $coll->add('a', new Route('/{app}/{action}/{unused}', [], [], [], '{host}'));
  631. $expected = [
  632. '_route' => 'a',
  633. 'app' => 'an_app',
  634. 'action' => 'an_action',
  635. 'unused' => 'unused',
  636. 'host' => 'foo',
  637. ];
  638. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'foo'));
  639. $this->assertEquals($expected, $matcher->match('/an_app/an_action/unused'));
  640. }
  641. public function testUtf8Prefix()
  642. {
  643. $coll = new RouteCollection();
  644. $coll->add('a', new Route('/é{foo}', [], [], ['utf8' => true]));
  645. $coll->add('b', new Route('/è{bar}', [], [], ['utf8' => true]));
  646. $matcher = $this->getUrlMatcher($coll);
  647. $this->assertEquals('a', $matcher->match('/éo')['_route']);
  648. }
  649. public function testUtf8AndMethodMatching()
  650. {
  651. $coll = new RouteCollection();
  652. $coll->add('a', new Route('/admin/api/list/{shortClassName}/{id}.{_format}', [], [], ['utf8' => true], '', [], ['PUT']));
  653. $coll->add('b', new Route('/admin/api/package.{_format}', [], [], [], '', [], ['POST']));
  654. $coll->add('c', new Route('/admin/api/package.{_format}', ['_format' => 'json'], [], [], '', [], ['GET']));
  655. $matcher = $this->getUrlMatcher($coll);
  656. $this->assertEquals('c', $matcher->match('/admin/api/package.json')['_route']);
  657. }
  658. public function testHostWithDot()
  659. {
  660. $coll = new RouteCollection();
  661. $coll->add('a', new Route('/foo', [], [], [], 'foo.example.com'));
  662. $coll->add('b', new Route('/bar/{baz}'));
  663. $matcher = $this->getUrlMatcher($coll);
  664. $this->assertEquals('b', $matcher->match('/bar/abc.123')['_route']);
  665. }
  666. public function testSlashVariant()
  667. {
  668. $coll = new RouteCollection();
  669. $coll->add('a', new Route('/foo/{bar}', [], ['bar' => '.*']));
  670. $matcher = $this->getUrlMatcher($coll);
  671. $this->assertEquals('a', $matcher->match('/foo/')['_route']);
  672. }
  673. public function testSlashVariant2()
  674. {
  675. $coll = new RouteCollection();
  676. $coll->add('a', new Route('/foo/{bar}/', [], ['bar' => '.*']));
  677. $matcher = $this->getUrlMatcher($coll);
  678. $this->assertEquals(['_route' => 'a', 'bar' => 'bar'], $matcher->match('/foo/bar/'));
  679. }
  680. public function testSlashWithVerb()
  681. {
  682. $coll = new RouteCollection();
  683. $coll->add('a', new Route('/{foo}', [], [], [], '', [], ['put', 'delete']));
  684. $coll->add('b', new Route('/bar/'));
  685. $matcher = $this->getUrlMatcher($coll);
  686. $this->assertSame(['_route' => 'b'], $matcher->match('/bar/'));
  687. $coll = new RouteCollection();
  688. $coll->add('a', new Route('/dav/{foo}', [], ['foo' => '.*'], [], '', [], ['GET', 'OPTIONS']));
  689. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'OPTIONS'));
  690. $expected = [
  691. '_route' => 'a',
  692. 'foo' => 'files/bar/',
  693. ];
  694. $this->assertEquals($expected, $matcher->match('/dav/files/bar/'));
  695. }
  696. public function testSlashAndVerbPrecedence()
  697. {
  698. $coll = new RouteCollection();
  699. $coll->add('a', new Route('/api/customers/{customerId}/contactpersons/', [], [], [], '', [], ['post']));
  700. $coll->add('b', new Route('/api/customers/{customerId}/contactpersons', [], [], [], '', [], ['get']));
  701. $matcher = $this->getUrlMatcher($coll);
  702. $expected = [
  703. '_route' => 'b',
  704. 'customerId' => '123',
  705. ];
  706. $this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
  707. $coll = new RouteCollection();
  708. $coll->add('a', new Route('/api/customers/{customerId}/contactpersons/', [], [], [], '', [], ['get']));
  709. $coll->add('b', new Route('/api/customers/{customerId}/contactpersons', [], [], [], '', [], ['post']));
  710. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
  711. $expected = [
  712. '_route' => 'b',
  713. 'customerId' => '123',
  714. ];
  715. $this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
  716. }
  717. public function testGreedyTrailingRequirement()
  718. {
  719. $coll = new RouteCollection();
  720. $coll->add('a', new Route('/{a}', [], ['a' => '.+']));
  721. $matcher = $this->getUrlMatcher($coll);
  722. $this->assertEquals(['_route' => 'a', 'a' => 'foo'], $matcher->match('/foo'));
  723. $this->assertEquals(['_route' => 'a', 'a' => 'foo/'], $matcher->match('/foo/'));
  724. }
  725. public function testTrailingRequirementWithDefault()
  726. {
  727. $coll = new RouteCollection();
  728. $coll->add('a', new Route('/fr-fr/{a}', ['a' => 'aaa'], ['a' => '.+']));
  729. $coll->add('b', new Route('/en-en/{b}', ['b' => 'bbb'], ['b' => '.*']));
  730. $matcher = $this->getUrlMatcher($coll);
  731. $this->assertEquals(['_route' => 'a', 'a' => 'aaa'], $matcher->match('/fr-fr'));
  732. $this->assertEquals(['_route' => 'a', 'a' => 'AAA'], $matcher->match('/fr-fr/AAA'));
  733. $this->assertEquals(['_route' => 'b', 'b' => 'bbb'], $matcher->match('/en-en'));
  734. $this->assertEquals(['_route' => 'b', 'b' => 'BBB'], $matcher->match('/en-en/BBB'));
  735. }
  736. public function testTrailingRequirementWithDefaultA()
  737. {
  738. $coll = new RouteCollection();
  739. $coll->add('a', new Route('/fr-fr/{a}', ['a' => 'aaa'], ['a' => '.+']));
  740. $matcher = $this->getUrlMatcher($coll);
  741. $this->expectException(ResourceNotFoundException::class);
  742. $matcher->match('/fr-fr/');
  743. }
  744. public function testTrailingRequirementWithDefaultB()
  745. {
  746. $coll = new RouteCollection();
  747. $coll->add('b', new Route('/en-en/{b}', ['b' => 'bbb'], ['b' => '.*']));
  748. $matcher = $this->getUrlMatcher($coll);
  749. $this->assertEquals(['_route' => 'b', 'b' => ''], $matcher->match('/en-en/'));
  750. }
  751. public function testRestrictiveTrailingRequirementWithStaticRouteAfter()
  752. {
  753. $coll = new RouteCollection();
  754. $coll->add('a', new Route('/hello{_}', [], ['_' => '/(?!/)']));
  755. $coll->add('b', new Route('/hello'));
  756. $matcher = $this->getUrlMatcher($coll);
  757. $this->assertEquals(['_route' => 'a', '_' => '/'], $matcher->match('/hello/'));
  758. }
  759. protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
  760. {
  761. return new UrlMatcher($routes, $context ?? new RequestContext());
  762. }
  763. }