Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / routing / Tests / Matcher / TraceableUrlMatcherTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Routing\Tests\Matcher;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
17 use Symfony\Component\Routing\RequestContext;
18 use Symfony\Component\Routing\Route;
19 use Symfony\Component\Routing\RouteCollection;
20
21 class TraceableUrlMatcherTest extends TestCase
22 {
23     public function test()
24     {
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"'));
32
33         $context = new RequestContext();
34         $context->setHost('baz');
35
36         $matcher = new TraceableUrlMatcher($coll, $context);
37         $traces = $matcher->getTraces('/babar');
38         $this->assertSame(array(0, 0, 0, 0, 0, 0), $this->getLevels($traces));
39
40         $traces = $matcher->getTraces('/foo');
41         $this->assertSame(array(1, 0, 0, 2), $this->getLevels($traces));
42
43         $traces = $matcher->getTraces('/bar/12');
44         $this->assertSame(array(0, 2), $this->getLevels($traces));
45
46         $traces = $matcher->getTraces('/bar/dd');
47         $this->assertSame(array(0, 1, 1, 0, 0, 0), $this->getLevels($traces));
48
49         $traces = $matcher->getTraces('/foo1');
50         $this->assertSame(array(0, 0, 0, 0, 2), $this->getLevels($traces));
51
52         $context->setMethod('POST');
53         $traces = $matcher->getTraces('/foo');
54         $this->assertSame(array(2), $this->getLevels($traces));
55
56         $traces = $matcher->getTraces('/bar/dd');
57         $this->assertSame(array(0, 1, 2), $this->getLevels($traces));
58
59         $traces = $matcher->getTraces('/foo2');
60         $this->assertSame(array(0, 0, 0, 0, 0, 1), $this->getLevels($traces));
61     }
62
63     public function testMatchRouteOnMultipleHosts()
64     {
65         $routes = new RouteCollection();
66         $routes->add('first', new Route(
67             '/mypath/',
68             array('_controller' => 'MainBundle:Info:first'),
69             array(),
70             array(),
71             'some.example.com'
72         ));
73
74         $routes->add('second', new Route(
75             '/mypath/',
76             array('_controller' => 'MainBundle:Info:second'),
77             array(),
78             array(),
79             'another.example.com'
80         ));
81
82         $context = new RequestContext();
83         $context->setHost('baz');
84
85         $matcher = new TraceableUrlMatcher($routes, $context);
86
87         $traces = $matcher->getTraces('/mypath/');
88         $this->assertSame(
89             array(TraceableUrlMatcher::ROUTE_ALMOST_MATCHES, TraceableUrlMatcher::ROUTE_ALMOST_MATCHES),
90             $this->getLevels($traces)
91         );
92     }
93
94     public function getLevels($traces)
95     {
96         $levels = array();
97         foreach ($traces as $trace) {
98             $levels[] = $trace['level'];
99         }
100
101         return $levels;
102     }
103
104     public function testRoutesWithConditions()
105     {
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'"));
108
109         $context = new RequestContext();
110         $context->setHost('baz');
111
112         $matcher = new TraceableUrlMatcher($routes, $context);
113
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']);
117
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']);
121     }
122 }