No Description

UrlMatcherTest.php 17KB

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