No Description

TraceableUrlMatcherTest.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. $coll->add('bar4', new Route('/foo2', array(), array(), array(), 'baz', array(), array(), 'context.getMethod() == "GET"'));
  26. $context = new RequestContext();
  27. $context->setHost('baz');
  28. $matcher = new TraceableUrlMatcher($coll, $context);
  29. $traces = $matcher->getTraces('/babar');
  30. $this->assertEquals(array(0, 0, 0, 0, 0, 0), $this->getLevels($traces));
  31. $traces = $matcher->getTraces('/foo');
  32. $this->assertEquals(array(1, 0, 0, 2), $this->getLevels($traces));
  33. $traces = $matcher->getTraces('/bar/12');
  34. $this->assertEquals(array(0, 2), $this->getLevels($traces));
  35. $traces = $matcher->getTraces('/bar/dd');
  36. $this->assertEquals(array(0, 1, 1, 0, 0, 0), $this->getLevels($traces));
  37. $traces = $matcher->getTraces('/foo1');
  38. $this->assertEquals(array(0, 0, 0, 0, 2), $this->getLevels($traces));
  39. $context->setMethod('POST');
  40. $traces = $matcher->getTraces('/foo');
  41. $this->assertEquals(array(2), $this->getLevels($traces));
  42. $traces = $matcher->getTraces('/bar/dd');
  43. $this->assertEquals(array(0, 1, 2), $this->getLevels($traces));
  44. $traces = $matcher->getTraces('/foo2');
  45. $this->assertEquals(array(0, 0, 0, 0, 0, 1), $this->getLevels($traces));
  46. }
  47. public function testMatchRouteOnMultipleHosts()
  48. {
  49. $routes = new RouteCollection();
  50. $routes->add('first', new Route(
  51. '/mypath/',
  52. array('_controller' => 'MainBundle:Info:first'),
  53. array(),
  54. array(),
  55. 'some.example.com'
  56. ));
  57. $routes->add('second', new Route(
  58. '/mypath/',
  59. array('_controller' => 'MainBundle:Info:second'),
  60. array(),
  61. array(),
  62. 'another.example.com'
  63. ));
  64. $context = new RequestContext();
  65. $context->setHost('baz');
  66. $matcher = new TraceableUrlMatcher($routes, $context);
  67. $traces = $matcher->getTraces('/mypath/');
  68. $this->assertEquals(
  69. array(TraceableUrlMatcher::ROUTE_ALMOST_MATCHES, TraceableUrlMatcher::ROUTE_ALMOST_MATCHES),
  70. $this->getLevels($traces)
  71. );
  72. }
  73. public function getLevels($traces)
  74. {
  75. $levels = array();
  76. foreach ($traces as $trace) {
  77. $levels[] = $trace['level'];
  78. }
  79. return $levels;
  80. }
  81. }