1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author SignpostMarv |
4
|
|
|
*/ |
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftFramework\Tests; |
8
|
|
|
|
9
|
|
|
use Generator; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use RuntimeException; |
12
|
|
|
use SignpostMarv\DaftFramework\HttpHandler; |
13
|
|
|
use SignpostMarv\DaftRouter\DaftSource; |
14
|
|
|
use SignpostMarv\DaftRouter\Tests\ImplementationTest as Base; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
18
|
|
|
use Symfony\Component\HttpKernel\Controller\ArgumentResolver; |
19
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerResolver; |
20
|
|
|
use Symfony\Component\HttpKernel\HttpKernel; |
21
|
|
|
|
22
|
|
|
class HttpHandlerTest extends Base |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @psalm-return Generator<int, array{0:class-string<HttpHandler>, 1:array, 2:string, 3:string, 4:array}, mixed, void> |
26
|
|
|
*/ |
27
|
|
|
public function DataProviderHttpHandlerInstances() : Generator |
28
|
|
|
{ |
29
|
|
|
yield from [ |
30
|
|
|
[ |
31
|
|
|
HttpHandler::class, |
32
|
|
|
['ConfigureDatabaseConnection' => ['sqlite::memory:', null, null, []]], |
33
|
|
|
'https://example.com/', |
34
|
|
|
realpath(__DIR__ . '/fixtures'), |
35
|
|
|
[ |
36
|
|
|
DaftSource::class => [ |
37
|
|
|
'cacheFile' => (__DIR__ . '/fixtures/http-kernel.fast-route.cache'), |
38
|
|
|
], |
39
|
|
|
], |
40
|
|
|
], |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @psalm-return Generator<int, array{0:HttpHandler, 1:Request, 2:int, 3:string}, mixed, void> |
46
|
|
|
*/ |
47
|
|
|
public function DataProviderHttpHandlerHandle() : Generator |
48
|
|
|
{ |
49
|
|
|
foreach ($this->DataProviderHttpHandlerInstances() as $args) { |
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
$implementation = $args[0]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array<string, mixed[]> |
57
|
|
|
*/ |
58
|
|
|
$postConstructionCalls = $args[1]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
$basePath = $args[3]; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var array<string, mixed[]> |
67
|
|
|
*/ |
68
|
|
|
$config = $args[4]; |
69
|
|
|
|
70
|
|
|
foreach ($this->DataProviderVerifyHandlerGood() as $testArgs) { |
71
|
|
|
list($baseUrl, $config, $testArgs) = $this->prepDataProviderVerifyHandlerGoodArgs( |
72
|
|
|
$config, |
73
|
|
|
$testArgs |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
if ( |
77
|
|
|
! isset($testArgs[0], $testArgs[1], $testArgs[2], $testArgs[3], $testArgs[4]) |
78
|
|
|
) { |
79
|
|
|
throw new RuntimeException(sprintf( |
80
|
|
|
'Unsupported args derived from %s::prepDataProviderVerifyHandlerGoodArgs', |
81
|
|
|
get_class($this) |
82
|
|
|
)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$baseUrl = (string) $baseUrl; |
86
|
|
|
$config = (array) $config; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var array |
90
|
|
|
*/ |
91
|
|
|
$testArgs = $testArgs; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @var int |
95
|
|
|
*/ |
96
|
|
|
$expectedStatus = $testArgs[2]; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @var string |
100
|
|
|
*/ |
101
|
|
|
$expectedContent = $testArgs[3]; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @var array |
105
|
|
|
*/ |
106
|
|
|
$requestArgs = $testArgs[4]; |
107
|
|
|
|
108
|
|
|
$instance = Utilities::ObtainHttpHandlerInstanceMixedArgs( |
109
|
|
|
$this, |
110
|
|
|
$implementation, |
111
|
|
|
$baseUrl, |
112
|
|
|
$basePath, |
113
|
|
|
$config |
114
|
|
|
); |
115
|
|
|
Utilities::ConfigureFrameworkInstance($this, $instance, $postConstructionCalls); |
116
|
|
|
|
117
|
|
|
$uri = (string) $requestArgs[0]; |
118
|
|
|
$method = (string) ($requestArgs[1] ?? 'GET'); |
119
|
|
|
$parameters = (array) ($requestArgs[2] ?? []); |
120
|
|
|
$cookies = (array) ($requestArgs[3] ?? []); |
121
|
|
|
$files = (array) ($requestArgs[4] ?? []); |
122
|
|
|
$server = (array) ($requestArgs[5] ?? []); |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @var string|resource|null |
126
|
|
|
*/ |
127
|
|
|
$content = ($requestArgs[6] ?? null); |
128
|
|
|
|
129
|
|
|
$request = Request::create( |
130
|
|
|
$uri, |
131
|
|
|
$method, |
132
|
|
|
$parameters, |
133
|
|
|
$cookies, |
134
|
|
|
$files, |
135
|
|
|
$server, |
136
|
|
|
$content |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
yield [$instance, $request, $expectedStatus, $expectedContent]; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @dataProvider DataProviderHttpHandlerHandle |
146
|
|
|
*/ |
147
|
|
|
public function testHandlerGoodOnHttpHandler( |
148
|
|
|
HttpHandler $instance, |
149
|
|
|
Request $request, |
150
|
|
|
int $expectedStatus, |
151
|
|
|
string $expectedContent |
152
|
|
|
) : void { |
153
|
|
|
$response = $instance->handle($request); |
154
|
|
|
|
155
|
|
|
static::assertSame($expectedStatus, $response->getStatusCode()); |
|
|
|
|
156
|
|
|
static::assertSame($expectedContent, $response->getContent()); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @psalm-return Generator<int, array{0:class-string<HttpHandler>, 1:string, 2:string, 3:array, 4:array<string, mixed[]>}, mixed, void> |
161
|
|
|
*/ |
162
|
|
|
public function DataProviderTestDroppedConfigProperty() : Generator |
163
|
|
|
{ |
164
|
|
|
foreach ($this->DataProviderHttpHandlerInstances() as $args) { |
165
|
|
|
list($implementation, , , $basePath, $config) = $args; |
166
|
|
|
|
167
|
|
|
foreach ($this->DataProviderVerifyHandlerGood() as $testArgs) { |
168
|
|
|
list($baseUrl, $config) = $this->prepDataProviderVerifyHandlerGoodArgs( |
169
|
|
|
(array) $config, |
170
|
|
|
$testArgs |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
foreach (['cacheFile', 'sources'] as $omitSubProperty) { |
174
|
|
|
/** |
175
|
|
|
* @var array<string, mixed> |
176
|
|
|
*/ |
177
|
|
|
$modifiedConfig = (array) $config; |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @var array<string, mixed> |
181
|
|
|
*/ |
182
|
|
|
$modifiedDaftSourceConfig = $modifiedConfig[DaftSource::class]; |
183
|
|
|
|
184
|
|
|
unset($modifiedDaftSourceConfig[$omitSubProperty]); |
185
|
|
|
|
186
|
|
|
$modifiedConfig[DaftSource::class] = $modifiedDaftSourceConfig; |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @var array<string, mixed[]> |
190
|
|
|
*/ |
191
|
|
|
$args1 = $args[1]; |
192
|
|
|
|
193
|
|
|
yield [$implementation, $baseUrl, $basePath, $modifiedConfig, $args1]; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param array<string, mixed[]> $args1 |
201
|
|
|
* |
202
|
|
|
* @dataProvider DataProviderTestDroppedConfigProperty |
203
|
|
|
*/ |
204
|
|
|
public function testDroppedConfigProperty( |
205
|
|
|
string $implementation, |
206
|
|
|
string $baseUrl, |
207
|
|
|
string $basePath, |
208
|
|
|
array $config, |
209
|
|
|
array $args1 |
210
|
|
|
) : void { |
211
|
|
|
$this->expectException(InvalidArgumentException::class); |
212
|
|
|
$this->expectExceptionMessage(sprintf('%s config not found!', DaftSource::class)); |
|
|
|
|
213
|
|
|
|
214
|
|
|
$instance = Utilities::ObtainHttpHandlerInstanceMixedArgs( |
215
|
|
|
$this, |
216
|
|
|
$implementation, |
217
|
|
|
$baseUrl, |
218
|
|
|
$basePath, |
219
|
|
|
$config |
220
|
|
|
); |
221
|
|
|
Utilities::ConfigureFrameworkInstance($this, $instance, $args1); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @depends testHandlerGoodOnHttpHandler |
226
|
|
|
* |
227
|
|
|
* @dataProvider DataProviderHttpHandlerHandle |
228
|
|
|
*/ |
229
|
|
|
public function testHandlerGoodWithHttpKernel( |
230
|
|
|
HttpHandler $instance, |
231
|
|
|
Request $request, |
232
|
|
|
int $expectedStatus, |
233
|
|
|
string $expectedContent |
234
|
|
|
) : void { |
235
|
|
|
$dispatcher = new EventDispatcher(); |
236
|
|
|
$instance->AttachToEventDispatcher($dispatcher); |
237
|
|
|
|
238
|
|
|
$kernel = new HttpKernel( |
239
|
|
|
$dispatcher, |
240
|
|
|
new ControllerResolver(), |
241
|
|
|
new RequestStack(), |
242
|
|
|
new ArgumentResolver() |
243
|
|
|
); |
244
|
|
|
|
245
|
|
|
$response = $kernel->handle($request); |
246
|
|
|
|
247
|
|
|
$kernel->terminate($request, $response); |
248
|
|
|
|
249
|
|
|
static::assertSame($expectedStatus, $response->getStatusCode()); |
|
|
|
|
250
|
|
|
static::assertSame($expectedContent, $response->getContent()); |
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @psalm-return array{0:string, 1:array, 2:array} |
255
|
|
|
*/ |
256
|
|
|
protected function prepDataProviderVerifyHandlerGoodArgs( |
257
|
|
|
array $config, |
258
|
|
|
array $testArgs |
259
|
|
|
) : array { |
260
|
|
|
list($sources, $prefix, , , $requestArgs) = $testArgs; |
261
|
|
|
|
262
|
|
|
list($uri) = (array) $requestArgs; |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @var array |
266
|
|
|
*/ |
267
|
|
|
$parsed = parse_url((string) $uri); |
268
|
|
|
|
269
|
|
|
$baseUrl = (string) ($parsed['scheme'] ?? '') . '://' . (string) ($parsed['host'] ?? ''); |
270
|
|
|
|
271
|
|
|
if (isset($parsed['port']) && is_int($parsed['port'])) { |
272
|
|
|
$baseUrl .= ':' . (string) $parsed['port']; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$baseUrl .= '/' . (string) $prefix; |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @var array<string, string|array<int, string>> |
279
|
|
|
* @var array<int, string> $daftSourceConfig['sources'] |
280
|
|
|
* @var string $daftSourceConfig['cacheFile'] |
281
|
|
|
*/ |
282
|
|
|
$daftSourceConfig = (array) $config[DaftSource::class]; |
283
|
|
|
|
284
|
|
|
$daftSourceConfig['sources'] = array_filter((array) $sources, 'is_string'); |
285
|
|
|
$daftSourceConfig['cacheFile'] = __DIR__ . '/fixtures/http-kernel.fast-route.cache'; |
286
|
|
|
|
287
|
|
|
if (is_file($daftSourceConfig['cacheFile'])) { |
288
|
|
|
unlink((string) ($daftSourceConfig['cacheFile'])); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
$config[DaftSource::class] = $daftSourceConfig; |
292
|
|
|
|
293
|
|
|
return [$baseUrl, $config, $testArgs]; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|