UrlMatcherTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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', [], [], [], '', [], ['post']));
  31. $matcher = $this->getUrlMatcher($coll);
  32. try {
  33. $matcher->match('/foo');
  34. $this->fail();
  35. } catch (MethodNotAllowedException $e) {
  36. $this->assertEquals(['POST'], $e->getAllowedMethods());
  37. }
  38. }
  39. public function testMethodNotAllowedOnRoot()
  40. {
  41. $coll = new RouteCollection();
  42. $coll->add('foo', new Route('/', [], [], [], '', [], ['GET']));
  43. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
  44. try {
  45. $matcher->match('/');
  46. $this->fail();
  47. } catch (MethodNotAllowedException $e) {
  48. $this->assertEquals(['GET'], $e->getAllowedMethods());
  49. }
  50. }
  51. public function testHeadAllowedWhenRequirementContainsGet()
  52. {
  53. $coll = new RouteCollection();
  54. $coll->add('foo', new Route('/foo', [], [], [], '', [], ['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', [], [], [], '', [], ['post']));
  62. $coll->add('foo2', new Route('/foo', [], [], [], '', [], ['put', 'delete']));
  63. $matcher = $this->getUrlMatcher($coll);
  64. try {
  65. $matcher->match('/foo');
  66. $this->fail();
  67. } catch (MethodNotAllowedException $e) {
  68. $this->assertEquals(['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(['_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}', ['def' => 'test']));
  86. $matcher = $this->getUrlMatcher($collection);
  87. $this->assertEquals(['_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', [], [], [], '', [], ['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', ['bar' => 'bar'], ['bar' => 'foo|bar']));
  108. $matcher = $this->getUrlMatcher($collection);
  109. $this->assertEquals(['_route' => 'bar', 'bar' => 'bar'], $matcher->match('/bar/foo'));
  110. $this->assertEquals(['_route' => 'bar', 'bar' => 'foo'], $matcher->match('/foo/foo'));
  111. $collection = new RouteCollection();
  112. $collection->add('bar', new Route('/{bar}', ['bar' => 'bar'], ['bar' => 'foo|bar']));
  113. $matcher = $this->getUrlMatcher($collection);
  114. $this->assertEquals(['_route' => 'bar', 'bar' => 'foo'], $matcher->match('/foo'));
  115. $this->assertEquals(['_route' => 'bar', 'bar' => 'bar'], $matcher->match('/'));
  116. // route with only optional variables
  117. $collection = new RouteCollection();
  118. $collection->add('bar', new Route('/{foo}/{bar}', ['foo' => 'foo', 'bar' => 'bar'], []));
  119. $matcher = $this->getUrlMatcher($collection);
  120. $this->assertEquals(['_route' => 'bar', 'foo' => 'foo', 'bar' => 'bar'], $matcher->match('/'));
  121. $this->assertEquals(['_route' => 'bar', 'foo' => 'a', 'bar' => 'bar'], $matcher->match('/a'));
  122. $this->assertEquals(['_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(['_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(['_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(['_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', [], ['foo' => '['.preg_quote($chars).']+'], ['utf8' => true]));
  164. $matcher = $this->getUrlMatcher($collection);
  165. $this->assertEquals(['_route' => 'foo', 'foo' => $chars], $matcher->match('/'.rawurlencode($chars).'/bar'));
  166. $this->assertEquals(['_route' => 'foo', 'foo' => $chars], $matcher->match('/'.strtr($chars, ['%' => '%25']).'/bar'));
  167. }
  168. public function testMatchWithDotMetacharacterInRequirements()
  169. {
  170. $collection = new RouteCollection();
  171. $collection->add('foo', new Route('/{foo}/bar', [], ['foo' => '.+']));
  172. $matcher = $this->getUrlMatcher($collection);
  173. $this->assertEquals(['_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(['_route' => 'foo'], $matcher->match('/foo1'));
  184. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Routing\Exception\ResourceNotFoundException');
  185. $this->assertEquals([], $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(['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 testMultipleParams()
  204. {
  205. $coll = new RouteCollection();
  206. $coll->add('foo1', new Route('/foo/{a}/{b}'));
  207. $coll->add('foo2', new Route('/foo/{a}/test/test/{b}'));
  208. $coll->add('foo3', new Route('/foo/{a}/{b}/{c}/{d}'));
  209. $route = $this->getUrlMatcher($coll)->match('/foo/test/test/test/bar')['_route'];
  210. $this->assertEquals('foo2', $route);
  211. }
  212. public function testDefaultRequirementForOptionalVariables()
  213. {
  214. $coll = new RouteCollection();
  215. $coll->add('test', new Route('/{page}.{_format}', ['page' => 'index', '_format' => 'html']));
  216. $matcher = $this->getUrlMatcher($coll);
  217. $this->assertEquals(['page' => 'my-page', '_format' => 'xml', '_route' => 'test'], $matcher->match('/my-page.xml'));
  218. }
  219. public function testMatchingIsEager()
  220. {
  221. $coll = new RouteCollection();
  222. $coll->add('test', new Route('/{foo}-{bar}-', [], ['foo' => '.+', 'bar' => '.+']));
  223. $matcher = $this->getUrlMatcher($coll);
  224. $this->assertEquals(['foo' => 'text1-text2-text3', 'bar' => 'text4', '_route' => 'test'], $matcher->match('/text1-text2-text3-text4-'));
  225. }
  226. public function testAdjacentVariables()
  227. {
  228. $coll = new RouteCollection();
  229. $coll->add('test', new Route('/{w}{x}{y}{z}.{_format}', ['z' => 'default-z', '_format' => 'html'], ['y' => 'y|Y']));
  230. $matcher = $this->getUrlMatcher($coll);
  231. // 'w' eagerly matches as much as possible and the other variables match the remaining chars.
  232. // This also shows that the variables w-z must all exclude the separating char (the dot '.' in this case) by default requirement.
  233. // Otherwise they would also consume '.xml' and _format would never match as it's an optional variable.
  234. $this->assertEquals(['w' => 'wwwww', 'x' => 'x', 'y' => 'Y', 'z' => 'Z', '_format' => 'xml', '_route' => 'test'], $matcher->match('/wwwwwxYZ.xml'));
  235. // As 'y' has custom requirement and can only be of value 'y|Y', it will leave 'ZZZ' to variable z.
  236. // So with carefully chosen requirements adjacent variables, can be useful.
  237. $this->assertEquals(['w' => 'wwwww', 'x' => 'x', 'y' => 'y', 'z' => 'ZZZ', '_format' => 'html', '_route' => 'test'], $matcher->match('/wwwwwxyZZZ'));
  238. // z and _format are optional.
  239. $this->assertEquals(['w' => 'wwwww', 'x' => 'x', 'y' => 'y', 'z' => 'default-z', '_format' => 'html', '_route' => 'test'], $matcher->match('/wwwwwxy'));
  240. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Routing\Exception\ResourceNotFoundException');
  241. $matcher->match('/wxy.html');
  242. }
  243. public function testOptionalVariableWithNoRealSeparator()
  244. {
  245. $coll = new RouteCollection();
  246. $coll->add('test', new Route('/get{what}', ['what' => 'All']));
  247. $matcher = $this->getUrlMatcher($coll);
  248. $this->assertEquals(['what' => 'All', '_route' => 'test'], $matcher->match('/get'));
  249. $this->assertEquals(['what' => 'Sites', '_route' => 'test'], $matcher->match('/getSites'));
  250. // Usually the character in front of an optional parameter can be left out, e.g. with pattern '/get/{what}' just '/get' would match.
  251. // But here the 't' in 'get' is not a separating character, so it makes no sense to match without it.
  252. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Routing\Exception\ResourceNotFoundException');
  253. $matcher->match('/ge');
  254. }
  255. public function testRequiredVariableWithNoRealSeparator()
  256. {
  257. $coll = new RouteCollection();
  258. $coll->add('test', new Route('/get{what}Suffix'));
  259. $matcher = $this->getUrlMatcher($coll);
  260. $this->assertEquals(['what' => 'Sites', '_route' => 'test'], $matcher->match('/getSitesSuffix'));
  261. }
  262. public function testDefaultRequirementOfVariable()
  263. {
  264. $coll = new RouteCollection();
  265. $coll->add('test', new Route('/{page}.{_format}'));
  266. $matcher = $this->getUrlMatcher($coll);
  267. $this->assertEquals(['page' => 'index', '_format' => 'mobile.html', '_route' => 'test'], $matcher->match('/index.mobile.html'));
  268. }
  269. /**
  270. * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
  271. */
  272. public function testDefaultRequirementOfVariableDisallowsSlash()
  273. {
  274. $coll = new RouteCollection();
  275. $coll->add('test', new Route('/{page}.{_format}'));
  276. $matcher = $this->getUrlMatcher($coll);
  277. $matcher->match('/index.sl/ash');
  278. }
  279. /**
  280. * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
  281. */
  282. public function testDefaultRequirementOfVariableDisallowsNextSeparator()
  283. {
  284. $coll = new RouteCollection();
  285. $coll->add('test', new Route('/{page}.{_format}', [], ['_format' => 'html|xml']));
  286. $matcher = $this->getUrlMatcher($coll);
  287. $matcher->match('/do.t.html');
  288. }
  289. /**
  290. * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
  291. */
  292. public function testMissingTrailingSlash()
  293. {
  294. $coll = new RouteCollection();
  295. $coll->add('foo', new Route('/foo/'));
  296. $matcher = $this->getUrlMatcher($coll);
  297. $matcher->match('/foo');
  298. }
  299. /**
  300. * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
  301. */
  302. public function testExtraTrailingSlash()
  303. {
  304. $coll = new RouteCollection();
  305. $coll->add('foo', new Route('/foo'));
  306. $matcher = $this->getUrlMatcher($coll);
  307. $matcher->match('/foo/');
  308. }
  309. /**
  310. * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
  311. */
  312. public function testMissingTrailingSlashForNonSafeMethod()
  313. {
  314. $coll = new RouteCollection();
  315. $coll->add('foo', new Route('/foo/'));
  316. $context = new RequestContext();
  317. $context->setMethod('POST');
  318. $matcher = $this->getUrlMatcher($coll, $context);
  319. $matcher->match('/foo');
  320. }
  321. /**
  322. * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
  323. */
  324. public function testExtraTrailingSlashForNonSafeMethod()
  325. {
  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. /**
  334. * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
  335. */
  336. public function testSchemeRequirement()
  337. {
  338. $coll = new RouteCollection();
  339. $coll->add('foo', new Route('/foo', [], [], [], '', ['https']));
  340. $matcher = $this->getUrlMatcher($coll);
  341. $matcher->match('/foo');
  342. }
  343. /**
  344. * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
  345. */
  346. public function testSchemeRequirementForNonSafeMethod()
  347. {
  348. $coll = new RouteCollection();
  349. $coll->add('foo', new Route('/foo', [], [], [], '', ['https']));
  350. $context = new RequestContext();
  351. $context->setMethod('POST');
  352. $matcher = $this->getUrlMatcher($coll, $context);
  353. $matcher->match('/foo');
  354. }
  355. public function testSamePathWithDifferentScheme()
  356. {
  357. $coll = new RouteCollection();
  358. $coll->add('https_route', new Route('/', [], [], [], '', ['https']));
  359. $coll->add('http_route', new Route('/', [], [], [], '', ['http']));
  360. $matcher = $this->getUrlMatcher($coll);
  361. $this->assertEquals(['_route' => 'http_route'], $matcher->match('/'));
  362. }
  363. /**
  364. * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
  365. */
  366. public function testCondition()
  367. {
  368. $coll = new RouteCollection();
  369. $route = new Route('/foo');
  370. $route->setCondition('context.getMethod() == "POST"');
  371. $coll->add('foo', $route);
  372. $matcher = $this->getUrlMatcher($coll);
  373. $matcher->match('/foo');
  374. }
  375. public function testRequestCondition()
  376. {
  377. $coll = new RouteCollection();
  378. $route = new Route('/foo/{bar}');
  379. $route->setCondition('request.getBaseUrl() == "/bar"');
  380. $coll->add('bar', $route);
  381. $route = new Route('/foo/{bar}');
  382. $route->setCondition('request.getBaseUrl() == "/sub/front.php" and request.getPathInfo() == "/foo/bar"');
  383. $coll->add('foo', $route);
  384. $matcher = $this->getUrlMatcher($coll, new RequestContext('/sub/front.php'));
  385. $this->assertEquals(['bar' => 'bar', '_route' => 'foo'], $matcher->match('/foo/bar'));
  386. }
  387. public function testDecodeOnce()
  388. {
  389. $coll = new RouteCollection();
  390. $coll->add('foo', new Route('/foo/{foo}'));
  391. $matcher = $this->getUrlMatcher($coll);
  392. $this->assertEquals(['foo' => 'bar%23', '_route' => 'foo'], $matcher->match('/foo/bar%2523'));
  393. }
  394. public function testCannotRelyOnPrefix()
  395. {
  396. $coll = new RouteCollection();
  397. $subColl = new RouteCollection();
  398. $subColl->add('bar', new Route('/bar'));
  399. $subColl->addPrefix('/prefix');
  400. // overwrite the pattern, so the prefix is not valid anymore for this route in the collection
  401. $subColl->get('bar')->setPath('/new');
  402. $coll->addCollection($subColl);
  403. $matcher = $this->getUrlMatcher($coll);
  404. $this->assertEquals(['_route' => 'bar'], $matcher->match('/new'));
  405. }
  406. public function testWithHost()
  407. {
  408. $coll = new RouteCollection();
  409. $coll->add('foo', new Route('/foo/{foo}', [], [], [], '{locale}.example.com'));
  410. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
  411. $this->assertEquals(['foo' => 'bar', '_route' => 'foo', 'locale' => 'en'], $matcher->match('/foo/bar'));
  412. }
  413. public function testWithHostOnRouteCollection()
  414. {
  415. $coll = new RouteCollection();
  416. $coll->add('foo', new Route('/foo/{foo}'));
  417. $coll->add('bar', new Route('/bar/{foo}', [], [], [], '{locale}.example.net'));
  418. $coll->setHost('{locale}.example.com');
  419. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
  420. $this->assertEquals(['foo' => 'bar', '_route' => 'foo', 'locale' => 'en'], $matcher->match('/foo/bar'));
  421. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
  422. $this->assertEquals(['foo' => 'bar', '_route' => 'bar', 'locale' => 'en'], $matcher->match('/bar/bar'));
  423. }
  424. /**
  425. * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
  426. */
  427. public function testWithOutHostHostDoesNotMatch()
  428. {
  429. $coll = new RouteCollection();
  430. $coll->add('foo', new Route('/foo/{foo}', [], [], [], '{locale}.example.com'));
  431. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'example.com'));
  432. $matcher->match('/foo/bar');
  433. }
  434. /**
  435. * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
  436. */
  437. public function testPathIsCaseSensitive()
  438. {
  439. $coll = new RouteCollection();
  440. $coll->add('foo', new Route('/locale', [], ['locale' => 'EN|FR|DE']));
  441. $matcher = $this->getUrlMatcher($coll);
  442. $matcher->match('/en');
  443. }
  444. public function testHostIsCaseInsensitive()
  445. {
  446. $coll = new RouteCollection();
  447. $coll->add('foo', new Route('/', [], ['locale' => 'EN|FR|DE'], [], '{locale}.example.com'));
  448. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
  449. $this->assertEquals(['_route' => 'foo', 'locale' => 'en'], $matcher->match('/'));
  450. }
  451. /**
  452. * @expectedException \Symfony\Component\Routing\Exception\NoConfigurationException
  453. */
  454. public function testNoConfiguration()
  455. {
  456. $coll = new RouteCollection();
  457. $matcher = $this->getUrlMatcher($coll);
  458. $matcher->match('/');
  459. }
  460. public function testNestedCollections()
  461. {
  462. $coll = new RouteCollection();
  463. $subColl = new RouteCollection();
  464. $subColl->add('a', new Route('/a'));
  465. $subColl->add('b', new Route('/b'));
  466. $subColl->add('c', new Route('/c'));
  467. $subColl->addPrefix('/p');
  468. $coll->addCollection($subColl);
  469. $coll->add('baz', new Route('/{baz}'));
  470. $subColl = new RouteCollection();
  471. $subColl->add('buz', new Route('/buz'));
  472. $subColl->addPrefix('/prefix');
  473. $coll->addCollection($subColl);
  474. $matcher = $this->getUrlMatcher($coll);
  475. $this->assertEquals(['_route' => 'a'], $matcher->match('/p/a'));
  476. $this->assertEquals(['_route' => 'baz', 'baz' => 'p'], $matcher->match('/p'));
  477. $this->assertEquals(['_route' => 'buz'], $matcher->match('/prefix/buz'));
  478. }
  479. /**
  480. * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
  481. */
  482. public function testSchemeAndMethodMismatch()
  483. {
  484. $coll = new RouteCollection();
  485. $coll->add('foo', new Route('/', [], [], [], null, ['https'], ['POST']));
  486. $matcher = $this->getUrlMatcher($coll);
  487. $matcher->match('/');
  488. }
  489. public function testSiblingRoutes()
  490. {
  491. $coll = new RouteCollection();
  492. $coll->add('a', (new Route('/a{a}'))->setMethods('POST'));
  493. $coll->add('b', (new Route('/a{a}'))->setMethods('PUT'));
  494. $coll->add('c', new Route('/a{a}'));
  495. $coll->add('d', (new Route('/b{a}'))->setCondition('false'));
  496. $coll->add('e', (new Route('/{b}{a}'))->setCondition('false'));
  497. $coll->add('f', (new Route('/{b}{a}'))->setRequirements(['b' => 'b']));
  498. $matcher = $this->getUrlMatcher($coll);
  499. $this->assertEquals(['_route' => 'c', 'a' => 'a'], $matcher->match('/aa'));
  500. $this->assertEquals(['_route' => 'f', 'b' => 'b', 'a' => 'a'], $matcher->match('/ba'));
  501. }
  502. public function testUnicodeRoute()
  503. {
  504. $coll = new RouteCollection();
  505. $coll->add('a', new Route('/{a}', [], ['a' => '.'], ['utf8' => false]));
  506. $coll->add('b', new Route('/{a}', [], ['a' => '.'], ['utf8' => true]));
  507. $matcher = $this->getUrlMatcher($coll);
  508. $this->assertEquals(['_route' => 'b', 'a' => 'é'], $matcher->match('/é'));
  509. }
  510. public function testRequirementWithCapturingGroup()
  511. {
  512. $coll = new RouteCollection();
  513. $coll->add('a', new Route('/{a}/{b}', [], ['a' => '(a|b)']));
  514. $matcher = $this->getUrlMatcher($coll);
  515. $this->assertEquals(['_route' => 'a', 'a' => 'a', 'b' => 'b'], $matcher->match('/a/b'));
  516. }
  517. public function testDotAllWithCatchAll()
  518. {
  519. $coll = new RouteCollection();
  520. $coll->add('a', new Route('/{id}.html', [], ['id' => '.+']));
  521. $coll->add('b', new Route('/{all}', [], ['all' => '.+']));
  522. $matcher = $this->getUrlMatcher($coll);
  523. $this->assertEquals(['_route' => 'a', 'id' => 'foo/bar'], $matcher->match('/foo/bar.html'));
  524. }
  525. public function testHostPattern()
  526. {
  527. $coll = new RouteCollection();
  528. $coll->add('a', new Route('/{app}/{action}/{unused}', [], [], [], '{host}'));
  529. $expected = [
  530. '_route' => 'a',
  531. 'app' => 'an_app',
  532. 'action' => 'an_action',
  533. 'unused' => 'unused',
  534. 'host' => 'foo',
  535. ];
  536. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'foo'));
  537. $this->assertEquals($expected, $matcher->match('/an_app/an_action/unused'));
  538. }
  539. public function testUtf8Prefix()
  540. {
  541. $coll = new RouteCollection();
  542. $coll->add('a', new Route('/é{foo}', [], [], ['utf8' => true]));
  543. $coll->add('b', new Route('/è{bar}', [], [], ['utf8' => true]));
  544. $matcher = $this->getUrlMatcher($coll);
  545. $this->assertEquals('a', $matcher->match('/éo')['_route']);
  546. }
  547. public function testUtf8AndMethodMatching()
  548. {
  549. $coll = new RouteCollection();
  550. $coll->add('a', new Route('/admin/api/list/{shortClassName}/{id}.{_format}', [], [], ['utf8' => true], '', [], ['PUT']));
  551. $coll->add('b', new Route('/admin/api/package.{_format}', [], [], [], '', [], ['POST']));
  552. $coll->add('c', new Route('/admin/api/package.{_format}', ['_format' => 'json'], [], [], '', [], ['GET']));
  553. $matcher = $this->getUrlMatcher($coll);
  554. $this->assertEquals('c', $matcher->match('/admin/api/package.json')['_route']);
  555. }
  556. public function testHostWithDot()
  557. {
  558. $coll = new RouteCollection();
  559. $coll->add('a', new Route('/foo', [], [], [], 'foo.example.com'));
  560. $coll->add('b', new Route('/bar/{baz}'));
  561. $matcher = $this->getUrlMatcher($coll);
  562. $this->assertEquals('b', $matcher->match('/bar/abc.123')['_route']);
  563. }
  564. public function testSlashVariant()
  565. {
  566. $coll = new RouteCollection();
  567. $coll->add('a', new Route('/foo/{bar}', [], ['bar' => '.*']));
  568. $matcher = $this->getUrlMatcher($coll);
  569. $this->assertEquals('a', $matcher->match('/foo/')['_route']);
  570. }
  571. public function testSlashVariant2()
  572. {
  573. $coll = new RouteCollection();
  574. $coll->add('a', new Route('/foo/{bar}/', [], ['bar' => '.*']));
  575. $matcher = $this->getUrlMatcher($coll);
  576. $this->assertEquals(['_route' => 'a', 'bar' => 'bar'], $matcher->match('/foo/bar/'));
  577. }
  578. public function testSlashWithVerb()
  579. {
  580. $coll = new RouteCollection();
  581. $coll->add('a', new Route('/{foo}', [], [], [], '', [], ['put', 'delete']));
  582. $coll->add('b', new Route('/bar/'));
  583. $matcher = $this->getUrlMatcher($coll);
  584. $this->assertSame(['_route' => 'b'], $matcher->match('/bar/'));
  585. $coll = new RouteCollection();
  586. $coll->add('a', new Route('/dav/{foo}', [], ['foo' => '.*'], [], '', [], ['GET', 'OPTIONS']));
  587. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'OPTIONS'));
  588. $expected = [
  589. '_route' => 'a',
  590. 'foo' => 'files/bar/',
  591. ];
  592. $this->assertEquals($expected, $matcher->match('/dav/files/bar/'));
  593. }
  594. public function testSlashAndVerbPrecedence()
  595. {
  596. $coll = new RouteCollection();
  597. $coll->add('a', new Route('/api/customers/{customerId}/contactpersons/', [], [], [], '', [], ['post']));
  598. $coll->add('b', new Route('/api/customers/{customerId}/contactpersons', [], [], [], '', [], ['get']));
  599. $matcher = $this->getUrlMatcher($coll);
  600. $expected = [
  601. '_route' => 'b',
  602. 'customerId' => '123',
  603. ];
  604. $this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
  605. $coll = new RouteCollection();
  606. $coll->add('a', new Route('/api/customers/{customerId}/contactpersons/', [], [], [], '', [], ['get']));
  607. $coll->add('b', new Route('/api/customers/{customerId}/contactpersons', [], [], [], '', [], ['post']));
  608. $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'POST'));
  609. $expected = [
  610. '_route' => 'b',
  611. 'customerId' => '123',
  612. ];
  613. $this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
  614. }
  615. public function testGreedyTrailingRequirement()
  616. {
  617. $coll = new RouteCollection();
  618. $coll->add('a', new Route('/{a}', [], ['a' => '.+']));
  619. $matcher = $this->getUrlMatcher($coll);
  620. $this->assertEquals(['_route' => 'a', 'a' => 'foo'], $matcher->match('/foo'));
  621. $this->assertEquals(['_route' => 'a', 'a' => 'foo/'], $matcher->match('/foo/'));
  622. }
  623. protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
  624. {
  625. return new UrlMatcher($routes, $context ?: new RequestContext());
  626. }
  627. }