Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UrlMatcherTest.php 37KB

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