UrlMatcherTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\UrlMatcher;
  12. use Symfony\Component\Routing\RouteCollection;
  13. use Symfony\Component\Routing\Route;
  14. class UrlMatcherTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testNormalizeUrl()
  17. {
  18. $collection = new RouteCollection();
  19. $collection->add('foo', new Route('/{foo}'));
  20. $matcher = new UrlMatcherForTests($collection, array(), array());
  21. $this->assertEquals('/foo', $matcher->normalizeUrl('/foo?foo=bar'), '->normalizeUrl() removes the query string');
  22. $this->assertEquals('/foo/bar', $matcher->normalizeUrl('/foo//bar'), '->normalizeUrl() removes duplicated /');
  23. }
  24. /**
  25. * @expectedException \InvalidArgumentException
  26. */
  27. public function testNormalizeUrlThrowsAnExceptionIfTheUrlIsInvalid()
  28. {
  29. $matcher = new UrlMatcherForTests(new RouteCollection(), array(), array());
  30. $matcher->normalizeUrl('');
  31. }
  32. public function testMatch()
  33. {
  34. // test the patterns are matched are parameters are returned
  35. $collection = new RouteCollection();
  36. $collection->add('foo', new Route('/foo/{bar}'));
  37. $matcher = new UrlMatcher($collection, array(), array());
  38. $this->assertEquals(false, $matcher->match('/no-match'));
  39. $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));
  40. // test that defaults are merged
  41. $collection = new RouteCollection();
  42. $collection->add('foo', new Route('/foo/{bar}', array('def' => 'test')));
  43. $matcher = new UrlMatcher($collection, array(), array());
  44. $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz'));
  45. // test that route "metod" is ignore if no method is given in the context
  46. $collection = new RouteCollection();
  47. $collection->add('foo', new Route('/foo', array(), array('_method' => 'GET|head')));
  48. // route matches with no context
  49. $matcher = new UrlMatcher($collection, array(), array());
  50. $this->assertNotEquals(false, $matcher->match('/foo'));
  51. // route does not match with POST method context
  52. $matcher = new UrlMatcher($collection, array('method' => 'POST'), array());
  53. $this->assertEquals(false, $matcher->match('/foo'));
  54. // route does match with GET or HEAD method context
  55. $matcher = new UrlMatcher($collection, array('method' => 'GET'), array());
  56. $this->assertNotEquals(false, $matcher->match('/foo'));
  57. $matcher = new UrlMatcher($collection, array('method' => 'HEAD'), array());
  58. $this->assertNotEquals(false, $matcher->match('/foo'));
  59. }
  60. }
  61. class UrlMatcherForTests extends UrlMatcher
  62. {
  63. public function normalizeUrl($url)
  64. {
  65. return parent::normalizeUrl($url);
  66. }
  67. }