1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\Middlewares\SafeRequests; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Psr\Http\Message\RequestInterface; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers \TheCodingMachine\Middlewares\SafeRequests\IsSafeHttpMethod |
12
|
|
|
*/ |
13
|
|
|
final class IsSafeHttpMethodTest extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @dataProvider httpMethodsProvider |
17
|
|
|
* |
18
|
|
|
* @param array $safeMethods |
19
|
|
|
* @param string $httpMethod |
20
|
|
|
* @param bool $expectedResult |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
public function testSafeMethods(array $safeMethods, string $httpMethod, bool $expectedResult) |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
/* @var $request RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
25
|
|
|
$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); |
26
|
|
|
|
27
|
|
|
$request->expects(self::any())->method('getMethod')->willReturn($httpMethod); |
|
|
|
|
28
|
|
|
|
29
|
|
|
self::assertSame($expectedResult, (new IsSafeHttpMethod(...$safeMethods))->__invoke($request)); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function httpMethodsProvider() : array |
33
|
|
|
{ |
34
|
|
|
return [ |
35
|
|
|
'empty' => [ |
36
|
|
|
[], |
37
|
|
|
'GET', |
38
|
|
|
false, |
39
|
|
|
], |
40
|
|
|
'GET only' => [ |
41
|
|
|
['GET'], |
42
|
|
|
'GET', |
43
|
|
|
true, |
44
|
|
|
], |
45
|
|
|
'get only' => [ |
46
|
|
|
['get'], |
47
|
|
|
'GET', |
48
|
|
|
true, |
49
|
|
|
], |
50
|
|
|
'GET only, matching lowercase get' => [ |
51
|
|
|
['GET'], |
52
|
|
|
'get', |
53
|
|
|
true, |
54
|
|
|
], |
55
|
|
|
'GET only, non-matching method' => [ |
56
|
|
|
['GET'], |
57
|
|
|
'PUT', |
58
|
|
|
false, |
59
|
|
|
], |
60
|
|
|
'GET, PUT only, matching method' => [ |
61
|
|
|
['GET', 'PUT'], |
62
|
|
|
'PUT', |
63
|
|
|
true, |
64
|
|
|
], |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @dataProvider safeDefaultsMatchingProvider |
70
|
|
|
* |
71
|
|
|
* @param string $httpMethod |
72
|
|
|
* @param bool $expectedResult |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function testSafeMethodsWithDefaults(string $httpMethod, bool $expectedResult) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
/* @var $request RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
77
|
|
|
$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); |
78
|
|
|
|
79
|
|
|
$request->expects(self::any())->method('getMethod')->willReturn($httpMethod); |
|
|
|
|
80
|
|
|
|
81
|
|
|
self::assertSame($expectedResult, IsSafeHttpMethod::fromDefaultSafeMethods()->__invoke($request)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function safeDefaultsMatchingProvider() : array |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
'empty' => [ |
88
|
|
|
'', |
89
|
|
|
false, |
90
|
|
|
], |
91
|
|
|
'GET' => [ |
92
|
|
|
'GET', |
93
|
|
|
true, |
94
|
|
|
], |
95
|
|
|
'get' => [ |
96
|
|
|
'get', |
97
|
|
|
true, |
98
|
|
|
], |
99
|
|
|
'HEAD' => [ |
100
|
|
|
'HEAD', |
101
|
|
|
true, |
102
|
|
|
], |
103
|
|
|
'head' => [ |
104
|
|
|
'head', |
105
|
|
|
true, |
106
|
|
|
], |
107
|
|
|
'OPTIONS' => [ |
108
|
|
|
'OPTIONS', |
109
|
|
|
true, |
110
|
|
|
], |
111
|
|
|
'options' => [ |
112
|
|
|
'options', |
113
|
|
|
true, |
114
|
|
|
], |
115
|
|
|
'DELETE' => [ |
116
|
|
|
'DELETE', |
117
|
|
|
false, |
118
|
|
|
], |
119
|
|
|
'delete' => [ |
120
|
|
|
'delete', |
121
|
|
|
false, |
122
|
|
|
], |
123
|
|
|
'POST' => [ |
124
|
|
|
'POST', |
125
|
|
|
false, |
126
|
|
|
], |
127
|
|
|
'post' => [ |
128
|
|
|
'post', |
129
|
|
|
false, |
130
|
|
|
], |
131
|
|
|
'PUT' => [ |
132
|
|
|
'PUT', |
133
|
|
|
false, |
134
|
|
|
], |
135
|
|
|
'put' => [ |
136
|
|
|
'put', |
137
|
|
|
false, |
138
|
|
|
], |
139
|
|
|
'UNKNOWN' => [ |
140
|
|
|
'UNKNOWN', |
141
|
|
|
false, |
142
|
|
|
], |
143
|
|
|
'unknown' => [ |
144
|
|
|
'unknown', |
145
|
|
|
false, |
146
|
|
|
], |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.