UrlMatcherTest.php 2.6 KB

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