Nenhuma Descrição

TraceableUrlMatcherTest.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\Route;
  13. use Symfony\Component\Routing\RouteCollection;
  14. use Symfony\Component\Routing\RequestContext;
  15. use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
  16. class TraceableUrlMatcherTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function test()
  19. {
  20. $coll = new RouteCollection();
  21. $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('POST')));
  22. $coll->add('bar', new Route('/bar/{id}', array(), array('id' => '\d+')));
  23. $coll->add('bar1', new Route('/bar/{name}', array(), array('id' => '\w+'), array(), '', array(), array('POST')));
  24. $coll->add('bar2', new Route('/foo', array(), array(), array(), 'baz'));
  25. $coll->add('bar3', new Route('/foo1', array(), array(), array(), 'baz'));
  26. $coll->add('bar4', new Route('/foo2', array(), array(), array(), 'baz', array(), array(), '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(array(0, 0, 0, 0, 0, 0), $this->getLevels($traces));
  32. $traces = $matcher->getTraces('/foo');
  33. $this->assertSame(array(1, 0, 0, 2), $this->getLevels($traces));
  34. $traces = $matcher->getTraces('/bar/12');
  35. $this->assertSame(array(0, 2), $this->getLevels($traces));
  36. $traces = $matcher->getTraces('/bar/dd');
  37. $this->assertSame(array(0, 1, 1, 0, 0, 0), $this->getLevels($traces));
  38. $traces = $matcher->getTraces('/foo1');
  39. $this->assertSame(array(0, 0, 0, 0, 2), $this->getLevels($traces));
  40. $context->setMethod('POST');
  41. $traces = $matcher->getTraces('/foo');
  42. $this->assertSame(array(2), $this->getLevels($traces));
  43. $traces = $matcher->getTraces('/bar/dd');
  44. $this->assertSame(array(0, 1, 2), $this->getLevels($traces));
  45. $traces = $matcher->getTraces('/foo2');
  46. $this->assertSame(array(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. array('_controller' => 'MainBundle:Info:first'),
  54. array(),
  55. array(),
  56. 'some.example.com'
  57. ));
  58. $routes->add('second', new Route(
  59. '/mypath/',
  60. array('_controller' => 'MainBundle:Info:second'),
  61. array(),
  62. array(),
  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. array(TraceableUrlMatcher::ROUTE_ALMOST_MATCHES, TraceableUrlMatcher::ROUTE_ALMOST_MATCHES),
  71. $this->getLevels($traces)
  72. );
  73. }
  74. public function getLevels($traces)
  75. {
  76. $levels = array();
  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', array(), array(), array(), 'baz', array(), array(), "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', array(), array(), array(), array('HTTP_USER_AGENT' => 'Firefox'));
  93. $traces = $matcher->getTracesForRequest($matchingRequest);
  94. $this->assertEquals('Route matches!', $traces[0]['log']);
  95. }
  96. }