UrlMatcherTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. class UrlMatcherTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function testNoMethodSoAllowed()
  19. {
  20. $coll = new RouteCollection();
  21. $coll->add('foo', new Route('/foo'));
  22. $matcher = new UrlMatcher($coll, array('method' => 'get'));
  23. $matcher->match('/foo');
  24. }
  25. public function testMethodNotAllowed()
  26. {
  27. $coll = new RouteCollection();
  28. $coll->add('foo', new Route('/foo', array(), array('_method' => 'post')));
  29. $matcher = new UrlMatcher($coll, array('method' => 'get'));
  30. try {
  31. $matcher->match('/foo');
  32. $this->fail();
  33. } catch (MethodNotAllowedException $e) {
  34. $this->assertEquals(array('post'), $e->getAllowedMethods());
  35. }
  36. }
  37. public function testMethodNotAllowedAggregatesAllowedMethods()
  38. {
  39. $coll = new RouteCollection();
  40. $coll->add('foo1', new Route('/foo', array(), array('_method' => 'post')));
  41. $coll->add('foo2', new Route('/foo', array(), array('_method' => 'put|delete')));
  42. $matcher = new UrlMatcher($coll, array('method' => 'get'));
  43. try {
  44. $matcher->match('/foo');
  45. $this->fail();
  46. } catch (MethodNotAllowedException $e) {
  47. $this->assertEquals(array('post', 'put', 'delete'), $e->getAllowedMethods());
  48. }
  49. }
  50. public function testMatch()
  51. {
  52. // test the patterns are matched are parameters are returned
  53. $collection = new RouteCollection();
  54. $collection->add('foo', new Route('/foo/{bar}'));
  55. $matcher = new UrlMatcher($collection, array(), array());
  56. try {
  57. $matcher->match('/no-match');
  58. $this->fail();
  59. } catch (NotFoundException $e) {}
  60. $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));
  61. // test that defaults are merged
  62. $collection = new RouteCollection();
  63. $collection->add('foo', new Route('/foo/{bar}', array('def' => 'test')));
  64. $matcher = new UrlMatcher($collection, array(), array());
  65. $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz'));
  66. // test that route "method" is ignored if no method is given in the context
  67. $collection = new RouteCollection();
  68. $collection->add('foo', new Route('/foo', array(), array('_method' => 'GET|head')));
  69. $matcher = new UrlMatcher($collection, array(), array());
  70. $this->assertInternalType('array', $matcher->match('/foo'));
  71. // route does not match with POST method context
  72. $matcher = new UrlMatcher($collection, array('method' => 'POST'), array());
  73. try {
  74. $matcher->match('/foo');
  75. $this->fail();
  76. } catch (MethodNotAllowedException $e) {}
  77. // route does match with GET or HEAD method context
  78. $matcher = new UrlMatcher($collection, array('method' => 'GET'), array());
  79. $this->assertInternalType('array', $matcher->match('/foo'));
  80. $matcher = new UrlMatcher($collection, array('method' => 'HEAD'), array());
  81. $this->assertInternalType('array', $matcher->match('/foo'));
  82. }
  83. }