1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author SignpostMarv |
4
|
|
|
*/ |
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftFramework\Tests; |
8
|
|
|
|
9
|
|
|
use Generator; |
10
|
|
|
use PHPUnit\Framework\TestCase as Base; |
11
|
|
|
use SignpostMarv\DaftFramework\Http\CookieMiddleware; |
12
|
|
|
use SignpostMarv\DaftFramework\HttpHandler; |
13
|
|
|
use SignpostMarv\DaftRouter\DaftSource; |
14
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
|
17
|
|
|
class CookieMiddlewareTest extends Base |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @dataProvider DataProvderCookeMiddlewareTest |
21
|
|
|
*/ |
22
|
|
|
public function testCookieMiddleware( |
23
|
|
|
string $implementation, |
24
|
|
|
array $postConstructionCalls, |
|
|
|
|
25
|
|
|
string $baseUrl, |
26
|
|
|
string $basePath, |
27
|
|
|
array $config, |
28
|
|
|
string $cookieName, |
29
|
|
|
string $cookieValue, |
30
|
|
|
? string $secure, |
31
|
|
|
? string $http, |
32
|
|
|
? string $sameSite |
33
|
|
|
) : void { |
34
|
|
|
$url = sprintf( |
35
|
|
|
'cookie-test/%s/%s/%s/%s/%s', |
36
|
|
|
rawurlencode($cookieName), |
37
|
|
|
rawurlencode($cookieValue), |
38
|
|
|
rawurlencode($secure ?? '1'), |
39
|
|
|
rawurlencode($http ?? '1'), |
40
|
|
|
rawurlencode($sameSite ?? 'lax') |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
if (is_string($secure) && is_string($http) && is_string($sameSite)) { |
44
|
|
|
$config[CookieMiddleware::class] = [ |
45
|
|
|
'secure' => '1' !== $secure, |
46
|
|
|
'httpOnly' => '1' !== $http, |
47
|
|
|
'sameSite' => ( |
48
|
|
|
('lax' === $sameSite) |
49
|
|
|
? 'strict' |
50
|
|
|
: 'lax' |
51
|
|
|
), |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$config[DaftSource::class]['sources'] = [ |
56
|
|
|
fixtures\Routes\CookieTest::class, |
57
|
|
|
]; |
58
|
|
|
$config[DaftSource::class]['cacheFile'] = ( |
59
|
|
|
__DIR__ . |
60
|
|
|
'/fixtures/cookie-test.fast-route.cache' |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$instance = Utilities::ObtainHttpHandlerInstance( |
64
|
|
|
$this, |
65
|
|
|
$implementation, |
66
|
|
|
$baseUrl, |
67
|
|
|
$basePath, |
68
|
|
|
$config |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$request = Request::create($baseUrl . $url); |
72
|
|
|
|
73
|
|
|
$response = $instance->handle($request); |
74
|
|
|
|
75
|
|
|
$cookie = current(array_filter( |
76
|
|
|
$response->headers->getCookies(), |
77
|
|
|
function (Cookie $cookie) use ($cookieName) : bool { |
78
|
|
|
return $cookieName === $cookie->getName(); |
79
|
|
|
} |
80
|
|
|
)); |
81
|
|
|
|
82
|
|
|
$this->assertInstanceOf(Cookie::class, $cookie); |
83
|
|
|
|
84
|
|
|
if (is_string($secure) && is_string($http) && is_string($sameSite)) { |
85
|
|
|
$this->assertSame( |
86
|
|
|
'1' === $secure, |
87
|
|
|
$cookie->isSecure(), |
88
|
|
|
'Secure must match without middleware' |
89
|
|
|
); |
90
|
|
|
$this->assertSame( |
91
|
|
|
'1' === $http, |
92
|
|
|
$cookie->isHttpOnly(), |
93
|
|
|
'HttpOnly must match without middleware' |
94
|
|
|
); |
95
|
|
|
$this->assertSame( |
96
|
|
|
$sameSite, |
97
|
|
|
$cookie->getSameSite(), |
98
|
|
|
'SameSite must match without middleware' |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$config[DaftSource::class]['sources'] = [ |
103
|
|
|
fixtures\Routes\CookieTest::class, |
104
|
|
|
CookieMiddleware::class, |
105
|
|
|
]; |
106
|
|
|
$config[DaftSource::class]['cacheFile'] = (__DIR__ . '/fixtures/cookie-middleware.fast-route.cache'); |
107
|
|
|
|
108
|
|
|
$instance = Utilities::ObtainHttpHandlerInstance( |
109
|
|
|
$this, |
110
|
|
|
$implementation, |
111
|
|
|
$baseUrl, |
112
|
|
|
$basePath, |
113
|
|
|
$config |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$request = Request::create($baseUrl . $url); |
117
|
|
|
|
118
|
|
|
$response = $instance->handle($request); |
119
|
|
|
|
120
|
|
|
$cookie = current(array_filter( |
121
|
|
|
$response->headers->getCookies(), |
122
|
|
|
function (Cookie $cookie) use ($cookieName) : bool { |
123
|
|
|
return $cookieName === $cookie->getName(); |
124
|
|
|
} |
125
|
|
|
)); |
126
|
|
|
|
127
|
|
|
$this->assertInstanceOf(Cookie::class, $cookie); |
128
|
|
|
|
129
|
|
|
if (is_string($secure) && is_string($http) && is_string($sameSite)) { |
130
|
|
|
$this->assertSame( |
131
|
|
|
$config[CookieMiddleware::class]['secure'], |
132
|
|
|
$cookie->isSecure(), |
133
|
|
|
'Secure must match flipped value with middleware' |
134
|
|
|
); |
135
|
|
|
$this->assertSame( |
136
|
|
|
$config[CookieMiddleware::class]['httpOnly'], |
137
|
|
|
$cookie->isHttpOnly(), |
138
|
|
|
'HttpOnly must match flipped value with middleware' |
139
|
|
|
); |
140
|
|
|
$this->assertSame( |
141
|
|
|
$config[CookieMiddleware::class]['sameSite'], |
142
|
|
|
$cookie->getSameSite(), |
143
|
|
|
'SameSite must match flipped value with middleware' |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function DataProvderCookeMiddlewareTest() : Generator |
149
|
|
|
{ |
150
|
|
|
foreach ($this->DataProviderCookieNameValue() as $cookie) { |
151
|
|
|
foreach ($this->DataProviderHttpHandlerInstances() as $handlerArgs) { |
152
|
|
|
yield array_merge($handlerArgs, $cookie, [null, null, null]); |
153
|
|
|
foreach ($this->DataProviderCookieSecure() as $secure) { |
154
|
|
|
foreach ($this->DataProviderCookieHttp() as $http) { |
155
|
|
|
foreach ($this->DataProviderCookieSameSite() as $sameSite) { |
156
|
|
|
yield array_merge($handlerArgs, $cookie, [$secure, $http, $sameSite]); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function DataProviderHttpHandlerInstances() : Generator |
165
|
|
|
{ |
166
|
|
|
yield from [ |
167
|
|
|
[ |
168
|
|
|
HttpHandler::class, |
169
|
|
|
[ |
170
|
|
|
], |
171
|
|
|
'https://example.com/', |
172
|
|
|
realpath(__DIR__ . '/fixtures'), |
173
|
|
|
[ |
174
|
|
|
DaftSource::class => [ |
175
|
|
|
], |
176
|
|
|
], |
177
|
|
|
], |
178
|
|
|
]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function DataProviderCookieNameValue() : Generator |
182
|
|
|
{ |
183
|
|
|
yield from [ |
184
|
|
|
[ |
185
|
|
|
'a', |
186
|
|
|
'b', |
187
|
|
|
], |
188
|
|
|
]; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function DataProviderCookieSecure() : Generator |
192
|
|
|
{ |
193
|
|
|
yield from ['0', '1']; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function DataProviderCookieHttp() : Generator |
197
|
|
|
{ |
198
|
|
|
yield from ['0', '1']; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function DataProviderCookieSameSite() : Generator |
202
|
|
|
{ |
203
|
|
|
yield from ['lax', 'strict']; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.