UrlMatcherTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 testMatch()
  17. {
  18. // test the patterns are matched are parameters are returned
  19. $collection = new RouteCollection();
  20. $collection->add('foo', new Route('/foo/{bar}'));
  21. $matcher = new UrlMatcher($collection, array(), array());
  22. $this->assertEquals(false, $matcher->match('/no-match'));
  23. $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));
  24. // test that defaults are merged
  25. $collection = new RouteCollection();
  26. $collection->add('foo', new Route('/foo/{bar}', array('def' => 'test')));
  27. $matcher = new UrlMatcher($collection, array(), array());
  28. $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz'));
  29. // test that route "method" is ignore if no method is given in the context
  30. $collection = new RouteCollection();
  31. $collection->add('foo', new Route('/foo', array(), array('_method' => 'GET|head')));
  32. // route matches with no context
  33. $matcher = new UrlMatcher($collection, array(), array());
  34. $this->assertNotEquals(false, $matcher->match('/foo'));
  35. // route does not match with POST method context
  36. $matcher = new UrlMatcher($collection, array('method' => 'POST'), array());
  37. $this->assertEquals(false, $matcher->match('/foo'));
  38. // route does match with GET or HEAD method context
  39. $matcher = new UrlMatcher($collection, array('method' => 'GET'), array());
  40. $this->assertNotEquals(false, $matcher->match('/foo'));
  41. $matcher = new UrlMatcher($collection, array('method' => 'HEAD'), array());
  42. $this->assertNotEquals(false, $matcher->match('/foo'));
  43. }
  44. }