UrlMatcherTest.php 18 KB

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