UrlMatcherTest.php 3.0 KB

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