TraceableUrlMatcherTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Route;
  14. use Symfony\Component\Routing\RouteCollection;
  15. use Symfony\Component\Routing\RequestContext;
  16. use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
  17. class TraceableUrlMatcherTest extends TestCase
  18. {
  19. public function test()
  20. {
  21. $coll = new RouteCollection();
  22. $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('POST')));
  23. $coll->add('bar', new Route('/bar/{id}', array(), array('id' => '\d+')));
  24. $coll->add('bar1', new Route('/bar/{name}', array(), array('id' => '\w+'), array(), '', array(), array('POST')));
  25. $coll->add('bar2', new Route('/foo', array(), array(), array(), 'baz'));
  26. $coll->add('bar3', new Route('/foo1', array(), array(), array(), 'baz'));
  27. $coll->add('bar4', new Route('/foo2', array(), array(), array(), 'baz', array(), array(), 'context.getMethod() == "GET"'));
  28. $context = new RequestContext();
  29. $context->setHost('baz');
  30. $matcher = new TraceableUrlMatcher($coll, $context);
  31. $traces = $matcher->getTraces('/babar');
  32. $this->assertSame(array(0, 0, 0, 0, 0, 0), $this->getLevels($traces));
  33. $traces = $matcher->getTraces('/foo');
  34. $this->assertSame(array(1, 0, 0, 2), $this->getLevels($traces));
  35. $traces = $matcher->getTraces('/bar/12');
  36. $this->assertSame(array(0, 2), $this->getLevels($traces));
  37. $traces = $matcher->getTraces('/bar/dd');
  38. $this->assertSame(array(0, 1, 1, 0, 0, 0), $this->getLevels($traces));
  39. $traces = $matcher->getTraces('/foo1');
  40. $this->assertSame(array(0, 0, 0, 0, 2), $this->getLevels($traces));
  41. $context->setMethod('POST');
  42. $traces = $matcher->getTraces('/foo');
  43. $this->assertSame(array(2), $this->getLevels($traces));
  44. $traces = $matcher->getTraces('/bar/dd');
  45. $this->assertSame(array(0, 1, 2), $this->getLevels($traces));
  46. $traces = $matcher->getTraces('/foo2');
  47. $this->assertSame(array(0, 0, 0, 0, 0, 1), $this->getLevels($traces));
  48. }
  49. public function testMatchRouteOnMultipleHosts()
  50. {
  51. $routes = new RouteCollection();
  52. $routes->add('first', new Route(
  53. '/mypath/',
  54. array('_controller' => 'MainBundle:Info:first'),
  55. array(),
  56. array(),
  57. 'some.example.com'
  58. ));
  59. $routes->add('second', new Route(
  60. '/mypath/',
  61. array('_controller' => 'MainBundle:Info:second'),
  62. array(),
  63. array(),
  64. 'another.example.com'
  65. ));
  66. $context = new RequestContext();
  67. $context->setHost('baz');
  68. $matcher = new TraceableUrlMatcher($routes, $context);
  69. $traces = $matcher->getTraces('/mypath/');
  70. $this->assertSame(
  71. array(TraceableUrlMatcher::ROUTE_ALMOST_MATCHES, TraceableUrlMatcher::ROUTE_ALMOST_MATCHES),
  72. $this->getLevels($traces)
  73. );
  74. }
  75. public function getLevels($traces)
  76. {
  77. $levels = array();
  78. foreach ($traces as $trace) {
  79. $levels[] = $trace['level'];
  80. }
  81. return $levels;
  82. }
  83. public function testRoutesWithConditions()
  84. {
  85. $routes = new RouteCollection();
  86. $routes->add('foo', new Route('/foo', array(), array(), array(), 'baz', array(), array(), "request.headers.get('User-Agent') matches '/firefox/i'"));
  87. $context = new RequestContext();
  88. $context->setHost('baz');
  89. $matcher = new TraceableUrlMatcher($routes, $context);
  90. $notMatchingRequest = Request::create('/foo', 'GET');
  91. $traces = $matcher->getTracesForRequest($notMatchingRequest);
  92. $this->assertEquals("Condition \"request.headers.get('User-Agent') matches '/firefox/i'\" does not evaluate to \"true\"", $traces[0]['log']);
  93. $matchingRequest = Request::create('/foo', 'GET', array(), array(), array(), array('HTTP_USER_AGENT' => 'Firefox'));
  94. $traces = $matcher->getTracesForRequest($matchingRequest);
  95. $this->assertEquals('Route matches!', $traces[0]['log']);
  96. }
  97. }