RequestConstraintTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 71
dl 0
loc 162
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A matchPathPatternDataProvider() 0 17 1
A testMatchPathPattern() 0 10 1
A testMatchIps() 0 10 1
A testConstructDefaultValues() 0 8 1
A testMatchMethods() 0 10 1
A matchMethodsDataProvider() 0 22 1
A matchHostPatternDataProvider() 0 17 1
A testConstructAllValues() 0 8 1
A testMatchHostPattern() 0 10 1
A matchIpsDataProvider() 0 12 1
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 Yarhon\RouteGuardBundle\Security\Http\RequestConstraint;
15
use Yarhon\RouteGuardBundle\Security\Http\RequestContext;
16
17
/**
18
 * @author Yaroslav Honcharuk <[email protected]>
19
 */
20
class RequestConstraintTest extends TestCase
21
{
22
    public function testConstructDefaultValues()
23
    {
24
        $constraint = new RequestConstraint();
25
26
        $this->assertNull($constraint->getPathPattern());
27
        $this->assertNull($constraint->getHostPattern());
28
        $this->assertNull($constraint->getMethods());
29
        $this->assertNull($constraint->getIps());
30
    }
31
32
    public function testConstructAllValues()
33
    {
34
        $constraint = new RequestConstraint('/path', 'host.com', ['get'], ['127.0.0.1']);
35
36
        $this->assertEquals('/path', $constraint->getPathPattern());
37
        $this->assertEquals('host.com', $constraint->getHostPattern());
38
        $this->assertEquals(['GET'], $constraint->getMethods());
39
        $this->assertEquals(['127.0.0.1'], $constraint->getIps());
40
    }
41
42
    /**
43
     * @dataProvider matchPathPatternDataProvider
44
     */
45
    public function testMatchPathPattern($constraintValue, $contextValue, $expected)
46
    {
47
        $constraint = new RequestConstraint($constraintValue);
48
49
        $requestContext = $this->createMock(RequestContext::class);
50
51
        $requestContext->method('getPathInfo')
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        $requestContext->/** @scrutinizer ignore-call */ 
52
                         method('getPathInfo')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
            ->willReturn($contextValue);
53
54
        $this->assertSame($expected, $constraint->matches($requestContext));
55
    }
56
57
    public function matchPathPatternDataProvider()
58
    {
59
        return [
60
            [
61
                '/admin/.*',
62
                '/admin/foo',
63
                true,
64
            ],
65
            [
66
                '^/admin/.*',
67
                '/admin2/',
68
                false,
69
            ],
70
            [
71
                '^/admin/fo o*$',
72
                '/admin/fo%20o',
73
                true,
74
            ],
75
        ];
76
    }
77
78
    /**
79
     * @dataProvider matchHostPatternDataProvider
80
     */
81
    public function testMatchHostPattern($constraintValue, $contextValue, $expected)
82
    {
83
        $constraint = new RequestConstraint(null, $constraintValue);
84
85
        $requestContext = $this->createMock(RequestContext::class);
86
87
        $requestContext->method('getHost')
88
            ->willReturn($contextValue);
89
90
        $this->assertSame($expected, $constraint->matches($requestContext));
91
    }
92
93
    public function matchHostPatternDataProvider()
94
    {
95
        return [
96
            [
97
                '.*\.site\.com',
98
                '.site.com',
99
                true,
100
            ],
101
            [
102
                '.*\.site\.com',
103
                'img.site.COM',
104
                true,
105
            ],
106
            [
107
                '.*\.site\.com',
108
                'site.net',
109
                false,
110
            ],
111
        ];
112
    }
113
114
    /**
115
     * @dataProvider matchMethodsDataProvider
116
     */
117
    public function testMatchMethods($constraintValue, $contextValue, $expected)
118
    {
119
        $constraint = new RequestConstraint(null, null, $constraintValue);
120
121
        $requestContext = $this->createMock(RequestContext::class);
122
123
        $requestContext->method('getMethod')
124
            ->willReturn($contextValue);
125
126
        $this->assertSame($expected, $constraint->matches($requestContext));
127
    }
128
129
    public function matchMethodsDataProvider()
130
    {
131
        return [
132
            [
133
                ['get', 'post'],
134
                'GET',
135
                true,
136
            ],
137
            [
138
                ['GET', 'POST'],
139
                'GET',
140
                true,
141
            ],
142
            [
143
                ['get', 'post'],
144
                'get',
145
                false,
146
            ],
147
            [
148
                ['GET', 'POST'],
149
                'PUT',
150
                false,
151
            ],
152
        ];
153
    }
154
155
    /**
156
     * @dataProvider matchIpsDataProvider
157
     */
158
    public function testMatchIps($constraintValue, $contextValue, $expected)
159
    {
160
        $constraint = new RequestConstraint(null, null, null, $constraintValue);
161
162
        $requestContext = $this->createMock(RequestContext::class);
163
164
        $requestContext->method('getClientIp')
165
            ->willReturn($contextValue);
166
167
        $this->assertSame($expected, $constraint->matches($requestContext));
168
    }
169
170
    public function matchIpsDataProvider()
171
    {
172
        return [
173
            [
174
                ['127.0.0.1', '127.0.0.2'],
175
                '127.0.0.1',
176
                true,
177
            ],
178
            [
179
                ['127.0.0.1', '127.0.0.2'],
180
                '127.0.0.3',
181
                false,
182
            ],
183
        ];
184
    }
185
}
186