UrlMatcherTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. public function testMatch()
  25. {
  26. // test the patterns are matched are parameters are returned
  27. $collection = new RouteCollection();
  28. $collection->add('foo', new Route('/foo/{bar}'));
  29. $matcher = new UrlMatcher($collection, array(), array());
  30. $this->assertEquals(false, $matcher->match('/no-match'));
  31. $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));
  32. // test that defaults are merged
  33. $collection = new RouteCollection();
  34. $collection->add('foo', new Route('/foo/{bar}', array('def' => 'test')));
  35. $matcher = new UrlMatcher($collection, array(), array());
  36. $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz'));
  37. // test that route "method" is ignore if no method is given in the context
  38. $collection = new RouteCollection();
  39. $collection->add('foo', new Route('/foo', array(), array('_method' => 'GET|head')));
  40. // route matches with no context
  41. $matcher = new UrlMatcher($collection, array(), array());
  42. $this->assertNotEquals(false, $matcher->match('/foo'));
  43. // route does not match with POST method context
  44. $matcher = new UrlMatcher($collection, array('method' => 'POST'), array());
  45. $this->assertEquals(false, $matcher->match('/foo'));
  46. // route does match with GET or HEAD method context
  47. $matcher = new UrlMatcher($collection, array('method' => 'GET'), array());
  48. $this->assertNotEquals(false, $matcher->match('/foo'));
  49. $matcher = new UrlMatcher($collection, array('method' => 'HEAD'), array());
  50. $this->assertNotEquals(false, $matcher->match('/foo'));
  51. }
  52. }
  53. class UrlMatcherForTests extends UrlMatcher
  54. {
  55. public function normalizeUrl($url)
  56. {
  57. return parent::normalizeUrl($url);
  58. }
  59. }