1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PSR7CsrfTest\HttpMethod; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
use PSR7Csrf\HttpMethod\IsSafeHttpRequest; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @covers \PSR7Csrf\HttpMethod\IsSafeHttpRequest |
13
|
|
|
*/ |
14
|
|
|
final class IsSafeHttpRequestTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @dataProvider httpMethodsProvider |
18
|
|
|
* |
19
|
|
|
* @param array $safeMethods |
20
|
|
|
* @param string $httpMethod |
21
|
|
|
* @param bool $expectedResult |
22
|
|
|
*/ |
23
|
|
|
public function testSafeMethods(array $safeMethods, string $httpMethod, bool $expectedResult) |
24
|
|
|
{ |
25
|
|
|
/* @var $request RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
26
|
|
|
$request = $this->createMock(RequestInterface::class); |
27
|
|
|
|
28
|
|
|
$request->expects(self::any())->method('getMethod')->willReturn($httpMethod); |
29
|
|
|
|
30
|
|
|
self::assertSame($expectedResult, (new IsSafeHttpRequest(...$safeMethods))->__invoke($request)); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function httpMethodsProvider() : array |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
'empty' => [ |
37
|
|
|
[], |
38
|
|
|
'GET', |
39
|
|
|
false, |
40
|
|
|
], |
41
|
|
|
'GET only' => [ |
42
|
|
|
['GET'], |
43
|
|
|
'GET', |
44
|
|
|
true, |
45
|
|
|
], |
46
|
|
|
'get only' => [ |
47
|
|
|
['get'], |
48
|
|
|
'GET', |
49
|
|
|
true, |
50
|
|
|
], |
51
|
|
|
'GET only, matching lowercase get' => [ |
52
|
|
|
['GET'], |
53
|
|
|
'get', |
54
|
|
|
true, |
55
|
|
|
], |
56
|
|
|
'GET only, non-matching method' => [ |
57
|
|
|
['GET'], |
58
|
|
|
'PUT', |
59
|
|
|
false, |
60
|
|
|
], |
61
|
|
|
'GET, PUT only, matching method' => [ |
62
|
|
|
['GET', 'PUT'], |
63
|
|
|
'PUT', |
64
|
|
|
true, |
65
|
|
|
], |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @dataProvider safeDefaultsMatchingProvider |
71
|
|
|
* |
72
|
|
|
* @param string $httpMethod |
73
|
|
|
* @param bool $expectedResult |
74
|
|
|
*/ |
75
|
|
|
public function testSafeMethodsWithDefaults(string $httpMethod, bool $expectedResult) |
76
|
|
|
{ |
77
|
|
|
/* @var $request RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
78
|
|
|
$request = $this->createMock(RequestInterface::class); |
79
|
|
|
|
80
|
|
|
$request->expects(self::any())->method('getMethod')->willReturn($httpMethod); |
81
|
|
|
|
82
|
|
|
self::assertSame($expectedResult, IsSafeHttpRequest::fromDefaultSafeMethods()->__invoke($request)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function safeDefaultsMatchingProvider() : array |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
'empty' => [ |
89
|
|
|
'', |
90
|
|
|
false, |
91
|
|
|
], |
92
|
|
|
'GET' => [ |
93
|
|
|
'GET', |
94
|
|
|
true, |
95
|
|
|
], |
96
|
|
|
'get' => [ |
97
|
|
|
'get', |
98
|
|
|
true, |
99
|
|
|
], |
100
|
|
|
'HEAD' => [ |
101
|
|
|
'HEAD', |
102
|
|
|
true, |
103
|
|
|
], |
104
|
|
|
'head' => [ |
105
|
|
|
'head', |
106
|
|
|
true, |
107
|
|
|
], |
108
|
|
|
'OPTIONS' => [ |
109
|
|
|
'OPTIONS', |
110
|
|
|
true, |
111
|
|
|
], |
112
|
|
|
'options' => [ |
113
|
|
|
'options', |
114
|
|
|
true, |
115
|
|
|
], |
116
|
|
|
'DELETE' => [ |
117
|
|
|
'DELETE', |
118
|
|
|
false, |
119
|
|
|
], |
120
|
|
|
'delete' => [ |
121
|
|
|
'delete', |
122
|
|
|
false, |
123
|
|
|
], |
124
|
|
|
'POST' => [ |
125
|
|
|
'POST', |
126
|
|
|
false, |
127
|
|
|
], |
128
|
|
|
'post' => [ |
129
|
|
|
'post', |
130
|
|
|
false, |
131
|
|
|
], |
132
|
|
|
'PUT' => [ |
133
|
|
|
'PUT', |
134
|
|
|
false, |
135
|
|
|
], |
136
|
|
|
'put' => [ |
137
|
|
|
'put', |
138
|
|
|
false, |
139
|
|
|
], |
140
|
|
|
'UNKNOWN' => [ |
141
|
|
|
'UNKNOWN', |
142
|
|
|
false, |
143
|
|
|
], |
144
|
|
|
'unknown' => [ |
145
|
|
|
'unknown', |
146
|
|
|
false, |
147
|
|
|
], |
148
|
|
|
]; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.