4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Tests\Matcher;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\Routing\Route;
17 use Symfony\Component\Routing\RouteCollection;
18 use Symfony\Component\Routing\RequestContext;
19 use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
21 class TraceableUrlMatcherTest extends TestCase
23 public function test()
25 $coll = new RouteCollection();
26 $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('POST')));
27 $coll->add('bar', new Route('/bar/{id}', array(), array('id' => '\d+')));
28 $coll->add('bar1', new Route('/bar/{name}', array(), array('id' => '\w+'), array(), '', array(), array('POST')));
29 $coll->add('bar2', new Route('/foo', array(), array(), array(), 'baz'));
30 $coll->add('bar3', new Route('/foo1', array(), array(), array(), 'baz'));
31 $coll->add('bar4', new Route('/foo2', array(), array(), array(), 'baz', array(), array(), 'context.getMethod() == "GET"'));
33 $context = new RequestContext();
34 $context->setHost('baz');
36 $matcher = new TraceableUrlMatcher($coll, $context);
37 $traces = $matcher->getTraces('/babar');
38 $this->assertSame(array(0, 0, 0, 0, 0, 0), $this->getLevels($traces));
40 $traces = $matcher->getTraces('/foo');
41 $this->assertSame(array(1, 0, 0, 2), $this->getLevels($traces));
43 $traces = $matcher->getTraces('/bar/12');
44 $this->assertSame(array(0, 2), $this->getLevels($traces));
46 $traces = $matcher->getTraces('/bar/dd');
47 $this->assertSame(array(0, 1, 1, 0, 0, 0), $this->getLevels($traces));
49 $traces = $matcher->getTraces('/foo1');
50 $this->assertSame(array(0, 0, 0, 0, 2), $this->getLevels($traces));
52 $context->setMethod('POST');
53 $traces = $matcher->getTraces('/foo');
54 $this->assertSame(array(2), $this->getLevels($traces));
56 $traces = $matcher->getTraces('/bar/dd');
57 $this->assertSame(array(0, 1, 2), $this->getLevels($traces));
59 $traces = $matcher->getTraces('/foo2');
60 $this->assertSame(array(0, 0, 0, 0, 0, 1), $this->getLevels($traces));
63 public function testMatchRouteOnMultipleHosts()
65 $routes = new RouteCollection();
66 $routes->add('first', new Route(
68 array('_controller' => 'MainBundle:Info:first'),
74 $routes->add('second', new Route(
76 array('_controller' => 'MainBundle:Info:second'),
82 $context = new RequestContext();
83 $context->setHost('baz');
85 $matcher = new TraceableUrlMatcher($routes, $context);
87 $traces = $matcher->getTraces('/mypath/');
89 array(TraceableUrlMatcher::ROUTE_ALMOST_MATCHES, TraceableUrlMatcher::ROUTE_ALMOST_MATCHES),
90 $this->getLevels($traces)
94 public function getLevels($traces)
97 foreach ($traces as $trace) {
98 $levels[] = $trace['level'];
104 public function testRoutesWithConditions()
106 $routes = new RouteCollection();
107 $routes->add('foo', new Route('/foo', array(), array(), array(), 'baz', array(), array(), "request.headers.get('User-Agent') matches '/firefox/i'"));
109 $context = new RequestContext();
110 $context->setHost('baz');
112 $matcher = new TraceableUrlMatcher($routes, $context);
114 $notMatchingRequest = Request::create('/foo', 'GET');
115 $traces = $matcher->getTracesForRequest($notMatchingRequest);
116 $this->assertEquals("Condition \"request.headers.get('User-Agent') matches '/firefox/i'\" does not evaluate to \"true\"", $traces[0]['log']);
118 $matchingRequest = Request::create('/foo', 'GET', array(), array(), array(), array('HTTP_USER_AGENT' => 'Firefox'));
119 $traces = $matcher->getTracesForRequest($matchingRequest);
120 $this->assertEquals('Route matches!', $traces[0]['log']);