RouteMatcherTest::testMatchesByPathPattern()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Tests\Security\Http;
12
13
use PHPUnit\Framework\TestCase;
14
use Symfony\Component\Routing\Route;
15
use Yarhon\RouteGuardBundle\Security\Http\RequestConstraint;
16
use Yarhon\RouteGuardBundle\Security\Http\RouteMatcher;
17
18
/**
19
 * @author Yaroslav Honcharuk <[email protected]>
20
 */
21
class RouteMatcherTest extends TestCase
22
{
23
    private $routeMatcher;
24
25
    public function setUp()
26
    {
27
        $this->routeMatcher = new RouteMatcher();
28
    }
29
30
    /**
31
     * @dataProvider matchesGeneralDataProvider
32
     */
33
    public function testMatchesGeneral($route, $constraint, $expected)
34
    {
35
        $result = $this->routeMatcher->matches($route, $constraint);
36
37
        if (is_bool($expected)) {
38
            $this->assertSame($expected, $result);
39
        } else {
40
            $this->assertEquals($expected, $result);
41
        }
42
    }
43
44
    public function matchesGeneralDataProvider()
45
    {
46
        return [
47
            [
48
                new Route('/blog', [], [], [], '', [], []),
49
                new RequestConstraint('^/blog', 'site\.com'),
50
                new RequestConstraint(null, 'site\.com'),
51
            ],
52
            [
53
                new Route('/blog', [], [], [], 'site.com', [], []),
54
                new RequestConstraint('^/blog', 'site\.com', ['GET']),
55
                new RequestConstraint(null, null, ['GET']),
56
            ],
57
            [
58
                new Route('/blog'),
59
                new RequestConstraint(null, null, null, ['127.0.0.1']),
60
                new RequestConstraint(null, null, null, ['127.0.0.1']),
61
            ],
62
            [
63
                new Route('/blog', [], [], []),
64
                new RequestConstraint('^/blog'),
65
                true,
66
            ],
67
            [
68
                new Route('/blog', [], [], []),
69
                new RequestConstraint('^/admin'),
70
                false,
71
            ],
72
            [
73
                new Route('/blog', [], [], []),
74
                new RequestConstraint(),
75
                true,
76
            ],
77
        ];
78
    }
79
80
    /**
81
     * @dataProvider matchesByPathPatternProvider
82
     */
83
    public function testMatchesByPathPattern($routePath, $pathPattern, $expected)
84
    {
85
        $route = new Route($routePath);
86
        $constraint = new RequestConstraint($pathPattern);
87
        $result = $this->routeMatcher->matches($route, $constraint);
88
89
        if (is_bool($expected)) {
90
            $this->assertSame($expected, $result);
91
        } else {
92
            $expected = new RequestConstraint($expected);
93
            $this->assertEquals($expected, $result);
94
        }
95
    }
96
97
    public function matchesByPathPatternProvider()
98
    {
99
        return [
100
            // static routes
101
            ['/', '/', true],
102
            ['/', '^/', true],
103
            ['/blog', '/blog', true],
104
            ['/blog', '/admin', false],
105
            ['/blog', '^/blog', true],
106
            ['/blog', '^/admin', false],
107
            // dynamic routes match to "wildcard" patterns
108
            ['/{name}', '^/', '^/'],
109
            ['/blog/{author}', '^/blog', true],
110
            ['/blog/{author}', '^/blog.*$', true],
111
            ['/blog/{author}', '^/blog/', '^/blog/'],
112
            ['/blog/{author}', '^/blog/.*$', '^/blog/.*$'],
113
            // dynamic routes match to patterns without "string start" assert
114
            ['/blog/{author}', '/blog', true],
115
            ['/new/blog/{author}', '/blog', true],
116
            ['/new/blog/{author}', '/blog/', '/blog/'],
117
            ['/new/blog/{author}', '/new2', '/new2'],
118
            // other dynamic routes
119
            ['/blog/{author}', '^/blow', false],
120
            ['/blog/{author}', '^/blog\\d+', '^/blog\\d+'],
121
            ['/blog/{author}', '\\d+', '\\d+'],
122
            ['/blog/{author}', '^\\d+', '^\\d+'],
123
        ];
124
    }
125
126
    /**
127
     * @dataProvider matchesByHostPatternProvider
128
     */
129
    public function testMatchesByHostPattern($routeHost, $hostPattern, $expected)
130
    {
131
        $route = new Route('/');
132
        $route->setHost($routeHost);
133
        $constraint = new RequestConstraint(null, $hostPattern);
134
        $result = $this->routeMatcher->matches($route, $constraint);
135
136
        if (is_bool($expected)) {
137
            $this->assertSame($expected, $result);
138
        } else {
139
            $expected = new RequestConstraint(null, $expected);
140
            $this->assertEquals($expected, $result);
141
        }
142
    }
143
144
    public function matchesByHostPatternProvider()
145
    {
146
        return [
147
            ['', 'site\.com', 'site\.com'],
148
            ['site.com', 'ite\.com', true],
149
            ['site.com', '^ite\.com', false],
150
            ['site.com', '^site\.com', true],
151
            ['site.com', 'SITE\.com', true],
152
            ['site.com.ua', 'site\.com', true],
153
            ['site.com.ua', 'site\.com$', false],
154
            ['site.com.{country}', 'site\.com', true],
155
            ['site.com.{country}', '^SITE\.com', true],
156
            ['{country}.site.com', 'site\.com', 'site\.com'],
157
            ['test.{country}.site.com', 'test\.', true],
158
        ];
159
    }
160
161
    /**
162
     * @dataProvider matchesByMethodsProvider
163
     */
164
    public function testMatchesByMethods($routeMethods, $methods, $expected)
165
    {
166
        $route = new Route('/');
167
        $route->setMethods($routeMethods);
168
        $constraint = new RequestConstraint(null, null, $methods);
169
        $result = $this->routeMatcher->matches($route, $constraint);
170
171
        if (is_bool($expected)) {
172
            $this->assertSame($expected, $result);
173
        } else {
174
            $expected = new RequestConstraint(null, null, $expected);
175
            $this->assertEquals($expected, $result);
176
        }
177
    }
178
179
    public function matchesByMethodsProvider()
180
    {
181
        return [
182
            [[], ['GET'], ['GET']],
183
            [['GET', 'POST'], ['PUT'], false],
184
            [['GET', 'POST'], ['POST'], ['POST']],
185
            [['GET', 'POST'], ['GET', 'POST'], true],
186
            [['GET', 'POST'], ['POST', 'GET'], true],
187
        ];
188
    }
189
}
190