TraceableUrlMatcherTest.php 4.3 KB

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