UrlMatcherTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\Matcher\Exception\MethodNotAllowedException;
  12. use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
  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 testMethodNotAllowedAggregatesAllowedMethods()
  39. {
  40. $coll = new RouteCollection();
  41. $coll->add('foo1', new Route('/foo', array(), array('_method' => 'post')));
  42. $coll->add('foo2', new Route('/foo', array(), array('_method' => 'put|delete')));
  43. $matcher = new UrlMatcher($coll, new RequestContext());
  44. try {
  45. $matcher->match('/foo');
  46. $this->fail();
  47. } catch (MethodNotAllowedException $e) {
  48. $this->assertEquals(array('post', 'put', 'delete'), $e->getAllowedMethods());
  49. }
  50. }
  51. public function testMatch()
  52. {
  53. // test the patterns are matched are parameters are returned
  54. $collection = new RouteCollection();
  55. $collection->add('foo', new Route('/foo/{bar}'));
  56. $matcher = new UrlMatcher($collection, new RequestContext(), array());
  57. try {
  58. $matcher->match('/no-match');
  59. $this->fail();
  60. } catch (NotFoundException $e) {}
  61. $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));
  62. // test that defaults are merged
  63. $collection = new RouteCollection();
  64. $collection->add('foo', new Route('/foo/{bar}', array('def' => 'test')));
  65. $matcher = new UrlMatcher($collection, new RequestContext(), array());
  66. $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz'));
  67. // test that route "method" is ignored if no method is given in the context
  68. $collection = new RouteCollection();
  69. $collection->add('foo', new Route('/foo', array(), array('_method' => 'GET|head')));
  70. $matcher = new UrlMatcher($collection, new RequestContext(), array());
  71. $this->assertInternalType('array', $matcher->match('/foo'));
  72. // route does not match with POST method context
  73. $matcher = new UrlMatcher($collection, new RequestContext('', 'post'), array());
  74. try {
  75. $matcher->match('/foo');
  76. $this->fail();
  77. } catch (MethodNotAllowedException $e) {}
  78. // route does match with GET or HEAD method context
  79. $matcher = new UrlMatcher($collection, new RequestContext(), array());
  80. $this->assertInternalType('array', $matcher->match('/foo'));
  81. $matcher = new UrlMatcher($collection, new RequestContext('', 'head'), array());
  82. $this->assertInternalType('array', $matcher->match('/foo'));
  83. // route with an optional variable as the first segment
  84. $collection = new RouteCollection();
  85. $collection->add('bar', new Route('/{bar}/foo', array('bar' => 'bar'), array('bar' => 'foo|bar')));
  86. $matcher = new UrlMatcher($collection, new RequestContext(), array());
  87. $this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/bar/foo'));
  88. $this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo/foo'));
  89. $collection = new RouteCollection();
  90. $collection->add('bar', new Route('/{bar}', array('bar' => 'bar'), array('bar' => 'foo|bar')));
  91. $matcher = new UrlMatcher($collection, new RequestContext(), array());
  92. $this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo'));
  93. $this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/'));
  94. }
  95. public function testMatchRegression()
  96. {
  97. $coll = new RouteCollection();
  98. $coll->add('foo', new Route('/foo/{foo}'));
  99. $coll->add('bar', new Route('/foo/bar/{foo}'));
  100. $matcher = new UrlMatcher($coll, new RequestContext());
  101. $this->assertEquals(array('foo' => 'bar', '_route' => 'bar'), $matcher->match('/foo/bar/bar'));
  102. $collection = new RouteCollection();
  103. $collection->add('foo', new Route('/{bar}'));
  104. $matcher = new UrlMatcher($collection, new RequestContext(), array());
  105. try {
  106. $matcher->match('/');
  107. $this->fail();
  108. } catch (NotFoundException $e) {
  109. }
  110. }
  111. }