UrlMatcherTest.php 16 KB

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