TraceableUrlMatcherTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\Component\Routing\Tests\Matcher;
  11. use Symfony\Component\Routing\Route;
  12. use Symfony\Component\Routing\RouteCollection;
  13. use Symfony\Component\Routing\RequestContext;
  14. use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
  15. class TraceableUrlMatcherTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function test()
  18. {
  19. $coll = new RouteCollection();
  20. $coll->add('foo', new Route('/foo', array(), array('_method' => 'POST')));
  21. $coll->add('bar', new Route('/bar/{id}', array(), array('id' => '\d+')));
  22. $coll->add('bar1', new Route('/bar/{name}', array(), array('id' => '\w+', '_method' => 'POST')));
  23. $coll->add('bar2', new Route('/foo', array(), array(), array(), 'baz'));
  24. $coll->add('bar3', new Route('/foo1', array(), array(), array(), 'baz'));
  25. $context = new RequestContext();
  26. $context->setHost('baz');
  27. $matcher = new TraceableUrlMatcher($coll, $context);
  28. $traces = $matcher->getTraces('/babar');
  29. $this->assertEquals(array(0, 0, 0, 0, 0), $this->getLevels($traces));
  30. $traces = $matcher->getTraces('/foo');
  31. $this->assertEquals(array(1, 0, 0, 2), $this->getLevels($traces));
  32. $traces = $matcher->getTraces('/bar/12');
  33. $this->assertEquals(array(0, 2), $this->getLevels($traces));
  34. $traces = $matcher->getTraces('/bar/dd');
  35. $this->assertEquals(array(0, 1, 1, 0, 0), $this->getLevels($traces));
  36. $traces = $matcher->getTraces('/foo1');
  37. $this->assertEquals(array(0, 0, 0, 0, 2), $this->getLevels($traces));
  38. $context->setMethod('POST');
  39. $traces = $matcher->getTraces('/foo');
  40. $this->assertEquals(array(2), $this->getLevels($traces));
  41. $traces = $matcher->getTraces('/bar/dd');
  42. $this->assertEquals(array(0, 1, 2), $this->getLevels($traces));
  43. }
  44. public function getLevels($traces)
  45. {
  46. $levels = array();
  47. foreach ($traces as $trace) {
  48. $levels[] = $trace['level'];
  49. }
  50. return $levels;
  51. }
  52. }