UrlMatcherTest.php 3.4 KB

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