/tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php

https://github.com/Faianca/symfony · PHP · 208 lines · 154 code · 40 blank · 14 comment · 0 complexity · 5801492b76a16970558ee18a1a208ef2 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\Tests\Component\Routing\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 are parameters are returned
  61. $collection = new RouteCollection();
  62. $collection->add('foo', new Route('/foo/{bar}'));
  63. $matcher = new UrlMatcher($collection, new RequestContext(), array());
  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(), array());
  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(), array());
  78. $this->assertInternalType('array', $matcher->match('/foo'));
  79. // route does not match with POST method context
  80. $matcher = new UrlMatcher($collection, new RequestContext('', 'post'), array());
  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(), array());
  87. $this->assertInternalType('array', $matcher->match('/foo'));
  88. $matcher = new UrlMatcher($collection, new RequestContext('', 'head'), array());
  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(), array());
  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(), array());
  99. $this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo'));
  100. $this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/'));
  101. }
  102. public function testMatchWithPrefixes()
  103. {
  104. $collection1 = new RouteCollection();
  105. $collection1->add('foo', new Route('/{foo}'));
  106. $collection2 = new RouteCollection();
  107. $collection2->addCollection($collection1, '/b');
  108. $collection = new RouteCollection();
  109. $collection->addCollection($collection2, '/a');
  110. $matcher = new UrlMatcher($collection, new RequestContext(), array());
  111. $this->assertEquals(array('_route' => 'foo', 'foo' => 'foo'), $matcher->match('/a/b/foo'));
  112. }
  113. public function testMatchWithDynamicPrefix()
  114. {
  115. $collection1 = new RouteCollection();
  116. $collection1->add('foo', new Route('/{foo}'));
  117. $collection2 = new RouteCollection();
  118. $collection2->addCollection($collection1, '/b');
  119. $collection = new RouteCollection();
  120. $collection->addCollection($collection2, '/{_locale}');
  121. $matcher = new UrlMatcher($collection, new RequestContext(), array());
  122. $this->assertEquals(array('_locale' => 'fr', '_route' => 'foo', 'foo' => 'foo'), $matcher->match('/fr/b/foo'));
  123. }
  124. public function testMatchNonAlpha()
  125. {
  126. $collection = new RouteCollection();
  127. $chars = '!"$%éà &\'()*+,./:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[]^_`abcdefghijklmnopqrstuvwxyz{|}~-';
  128. $collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '['.preg_quote($chars).']+')));
  129. $matcher = new UrlMatcher($collection, new RequestContext(), array());
  130. $this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.urlencode($chars).'/bar'));
  131. $this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.strtr($chars, array('%' => '%25', '+' => '%2B')).'/bar'));
  132. }
  133. public function testMatchWithDotMetacharacterInRequirements()
  134. {
  135. $collection = new RouteCollection();
  136. $collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '.+')));
  137. $matcher = new UrlMatcher($collection, new RequestContext(), array());
  138. $this->assertEquals(array('_route' => 'foo', 'foo' => "\n"), $matcher->match('/'.urlencode("\n").'/bar'), 'linefeed character is matched');
  139. }
  140. public function testMatchOverridenRoute()
  141. {
  142. $collection = new RouteCollection();
  143. $collection->add('foo', new Route('/foo'));
  144. $collection1 = new RouteCollection();
  145. $collection1->add('foo', new Route('/foo1'));
  146. $collection->addCollection($collection1);
  147. $matcher = new UrlMatcher($collection, new RequestContext(), array());
  148. $this->assertEquals(array('_route' => 'foo'), $matcher->match('/foo1'));
  149. $this->setExpectedException('Symfony\Component\Routing\Exception\ResourceNotFoundException');
  150. $this->assertEquals(array(), $matcher->match('/foo'));
  151. }
  152. public function testMatchRegression()
  153. {
  154. $coll = new RouteCollection();
  155. $coll->add('foo', new Route('/foo/{foo}'));
  156. $coll->add('bar', new Route('/foo/bar/{foo}'));
  157. $matcher = new UrlMatcher($coll, new RequestContext());
  158. $this->assertEquals(array('foo' => 'bar', '_route' => 'bar'), $matcher->match('/foo/bar/bar'));
  159. $collection = new RouteCollection();
  160. $collection->add('foo', new Route('/{bar}'));
  161. $matcher = new UrlMatcher($collection, new RequestContext(), array());
  162. try {
  163. $matcher->match('/');
  164. $this->fail();
  165. } catch (ResourceNotFoundException $e) {
  166. }
  167. }
  168. }