-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
TraceableUrlMatcherTest.php
126 lines (100 loc) · 4.31 KB
/
TraceableUrlMatcherTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Matcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class TraceableUrlMatcherTest extends UrlMatcherTest
{
public function test()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo', [], [], [], '', [], ['POST']));
$coll->add('bar', new Route('/bar/{id}', [], ['id' => '\d+']));
$coll->add('bar1', new Route('/bar/{name}', [], ['id' => '\w+'], [], '', [], ['POST']));
$coll->add('bar2', new Route('/foo', [], [], [], 'baz'));
$coll->add('bar3', new Route('/foo1', [], [], [], 'baz'));
$coll->add('bar4', new Route('/foo2', [], [], [], 'baz', [], [], 'context.getMethod() == "GET"'));
$context = new RequestContext();
$context->setHost('baz');
$matcher = new TraceableUrlMatcher($coll, $context);
$traces = $matcher->getTraces('/babar');
$this->assertSame([0, 0, 0, 0, 0, 0], $this->getLevels($traces));
$traces = $matcher->getTraces('/foo');
$this->assertSame([1, 0, 0, 2], $this->getLevels($traces));
$traces = $matcher->getTraces('/bar/12');
$this->assertSame([0, 2], $this->getLevels($traces));
$traces = $matcher->getTraces('/bar/dd');
$this->assertSame([0, 1, 1, 0, 0, 0], $this->getLevels($traces));
$traces = $matcher->getTraces('/foo1');
$this->assertSame([0, 0, 0, 0, 2], $this->getLevels($traces));
$context->setMethod('POST');
$traces = $matcher->getTraces('/foo');
$this->assertSame([2], $this->getLevels($traces));
$traces = $matcher->getTraces('/bar/dd');
$this->assertSame([0, 1, 2], $this->getLevels($traces));
$traces = $matcher->getTraces('/foo2');
$this->assertSame([0, 0, 0, 0, 0, 1], $this->getLevels($traces));
}
public function testMatchRouteOnMultipleHosts()
{
$routes = new RouteCollection();
$routes->add('first', new Route(
'/mypath/',
['_controller' => 'MainBundle:Info:first'],
[],
[],
'some.example.com'
));
$routes->add('second', new Route(
'/mypath/',
['_controller' => 'MainBundle:Info:second'],
[],
[],
'another.example.com'
));
$context = new RequestContext();
$context->setHost('baz');
$matcher = new TraceableUrlMatcher($routes, $context);
$traces = $matcher->getTraces('/mypath/');
$this->assertSame(
[TraceableUrlMatcher::ROUTE_ALMOST_MATCHES, TraceableUrlMatcher::ROUTE_ALMOST_MATCHES],
$this->getLevels($traces)
);
}
public function getLevels($traces)
{
$levels = [];
foreach ($traces as $trace) {
$levels[] = $trace['level'];
}
return $levels;
}
public function testRoutesWithConditions()
{
$routes = new RouteCollection();
$routes->add('foo', new Route('/foo', [], [], [], 'baz', [], [], "request.headers.get('User-Agent') matches '/firefox/i'"));
$context = new RequestContext();
$context->setHost('baz');
$matcher = new TraceableUrlMatcher($routes, $context);
$notMatchingRequest = Request::create('/foo', 'GET');
$traces = $matcher->getTracesForRequest($notMatchingRequest);
$this->assertEquals("Condition \"request.headers.get('User-Agent') matches '/firefox/i'\" does not evaluate to \"true\"", $traces[0]['log']);
$matchingRequest = Request::create('/foo', 'GET', [], [], [], ['HTTP_USER_AGENT' => 'Firefox']);
$traces = $matcher->getTracesForRequest($matchingRequest);
$this->assertEquals('Route matches!', $traces[0]['log']);
}
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
{
return new TraceableUrlMatcher($routes, $context ?: new RequestContext());
}
}