PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php

https://bitbucket.org/hill2steve/mobileroom
PHP | 245 lines | 181 code | 46 blank | 18 comment | 0 complexity | 3fb33b012ede771166f9033cde44915b MD5 | raw file
  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. $collection1 = new RouteCollection();
  112. $collection1->add('foo', new Route('/{foo}'));
  113. $collection2 = new RouteCollection();
  114. $collection2->addCollection($collection1, '/b');
  115. $collection = new RouteCollection();
  116. $collection->addCollection($collection2, '/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. $collection1 = new RouteCollection();
  123. $collection1->add('foo', new Route('/{foo}'));
  124. $collection2 = new RouteCollection();
  125. $collection2->addCollection($collection1, '/b');
  126. $collection = new RouteCollection();
  127. $collection->addCollection($collection2, '/{_locale}');
  128. $matcher = new UrlMatcher($collection, new RequestContext());
  129. $this->assertEquals(array('_locale' => 'fr', '_route' => 'foo', 'foo' => 'foo'), $matcher->match('/fr/b/foo'));
  130. }
  131. public function testMatchNonAlpha()
  132. {
  133. $collection = new RouteCollection();
  134. $chars = '!"$%éà &\'()*+,./:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[]^_`abcdefghijklmnopqrstuvwxyz{|}~-';
  135. $collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '['.preg_quote($chars).']+')));
  136. $matcher = new UrlMatcher($collection, new RequestContext());
  137. $this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.rawurlencode($chars).'/bar'));
  138. $this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.strtr($chars, array('%' => '%25')).'/bar'));
  139. }
  140. public function testMatchWithDotMetacharacterInRequirements()
  141. {
  142. $collection = new RouteCollection();
  143. $collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '.+')));
  144. $matcher = new UrlMatcher($collection, new RequestContext());
  145. $this->assertEquals(array('_route' => 'foo', 'foo' => "\n"), $matcher->match('/'.urlencode("\n").'/bar'), 'linefeed character is matched');
  146. }
  147. public function testMatchOverriddenRoute()
  148. {
  149. $collection = new RouteCollection();
  150. $collection->add('foo', new Route('/foo'));
  151. $collection1 = new RouteCollection();
  152. $collection1->add('foo', new Route('/foo1'));
  153. $collection->addCollection($collection1);
  154. $matcher = new UrlMatcher($collection, new RequestContext());
  155. $this->assertEquals(array('_route' => 'foo'), $matcher->match('/foo1'));
  156. $this->setExpectedException('Symfony\Component\Routing\Exception\ResourceNotFoundException');
  157. $this->assertEquals(array(), $matcher->match('/foo'));
  158. }
  159. public function testMatchRegression()
  160. {
  161. $coll = new RouteCollection();
  162. $coll->add('foo', new Route('/foo/{foo}'));
  163. $coll->add('bar', new Route('/foo/bar/{foo}'));
  164. $matcher = new UrlMatcher($coll, new RequestContext());
  165. $this->assertEquals(array('foo' => 'bar', '_route' => 'bar'), $matcher->match('/foo/bar/bar'));
  166. $collection = new RouteCollection();
  167. $collection->add('foo', new Route('/{bar}'));
  168. $matcher = new UrlMatcher($collection, new RequestContext());
  169. try {
  170. $matcher->match('/');
  171. $this->fail();
  172. } catch (ResourceNotFoundException $e) {
  173. }
  174. }
  175. public function testMatchingIsEager()
  176. {
  177. $coll = new RouteCollection();
  178. $coll->add('test', new Route('/{foo}-{bar}-', array(), array('foo' => '.+', 'bar' => '.+')));
  179. $matcher = new UrlMatcher($coll, new RequestContext());
  180. $this->assertEquals(array('foo' => 'text1-text2-text3', 'bar' => 'text4', '_route' => 'test'), $matcher->match('/text1-text2-text3-text4-'));
  181. }
  182. /**
  183. * @expectedException Symfony\Component\Routing\Exception\ResourceNotFoundException
  184. */
  185. public function testSchemeRequirement()
  186. {
  187. $coll = new RouteCollection();
  188. $coll->add('foo', new Route('/foo', array(), array('_scheme' => 'https')));
  189. $matcher = new UrlMatcher($coll, new RequestContext());
  190. $matcher->match('/foo');
  191. }
  192. public function testDecodeOnce()
  193. {
  194. $coll = new RouteCollection();
  195. $coll->add('foo', new Route('/foo/{foo}'));
  196. $matcher = new UrlMatcher($coll, new RequestContext());
  197. $this->assertEquals(array('foo' => 'bar%23', '_route' => 'foo'), $matcher->match('/foo/bar%2523'));
  198. }
  199. }