|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author SignpostMarv |
|
4
|
|
|
*/ |
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftFramework\Tests; |
|
8
|
|
|
|
|
9
|
|
|
use Generator; |
|
10
|
|
|
use SignpostMarv\DaftFramework\HttpHandler; |
|
11
|
|
|
use SignpostMarv\DaftRouter\DaftSource; |
|
12
|
|
|
use SignpostMarv\DaftRouter\Tests\ImplementationTest as Base; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
|
|
15
|
|
|
class HttpHandlerTest extends Base |
|
16
|
|
|
{ |
|
17
|
|
|
public function DataProviderHttpHandlerInstances() : Generator |
|
18
|
|
|
{ |
|
19
|
|
|
yield from [ |
|
20
|
|
|
[ |
|
21
|
|
|
HttpHandler::class, |
|
22
|
|
|
[ |
|
23
|
|
|
'ConfigureDatabaseConnection' => [ |
|
24
|
|
|
'sqlite::memory:', |
|
25
|
|
|
null, |
|
26
|
|
|
null, |
|
27
|
|
|
[], |
|
28
|
|
|
], |
|
29
|
|
|
], |
|
30
|
|
|
'https://example.com/', |
|
31
|
|
|
realpath(__DIR__ . '/fixtures'), |
|
32
|
|
|
[ |
|
33
|
|
|
DaftSource::class => [ |
|
34
|
|
|
'cacheFile' => (__DIR__ . '/fixtures/http-kernel.fast-route.cache'), |
|
35
|
|
|
], |
|
36
|
|
|
], |
|
37
|
|
|
], |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function DataProviderHttpHandlerHandle() : Generator |
|
42
|
|
|
{ |
|
43
|
|
|
foreach ($this->DataProviderHttpHandlerInstances() as $args) { |
|
44
|
|
|
list($implementation, $postConstructionCalls, $baseUrl, $basePath, $config) = $args; |
|
45
|
|
|
|
|
46
|
|
|
foreach ($this->DataProviderVerifyHandlerGood() as $testArgs) { |
|
47
|
|
|
list( |
|
48
|
|
|
$sources, |
|
49
|
|
|
$prefix, |
|
50
|
|
|
$expectedStatus, |
|
51
|
|
|
$expectedContent, |
|
52
|
|
|
$uri, |
|
53
|
|
|
$method, |
|
54
|
|
|
$parameters, |
|
55
|
|
|
$cookies, |
|
56
|
|
|
$files, |
|
57
|
|
|
$server, |
|
58
|
|
|
$content |
|
59
|
|
|
) = $testArgs; |
|
60
|
|
|
|
|
61
|
|
|
$parsed = parse_url($uri); |
|
62
|
|
|
|
|
63
|
|
|
$baseUrl = $parsed['scheme'] . '://' . $parsed['host']; |
|
64
|
|
|
|
|
65
|
|
|
if (isset($parsed['port'])) { |
|
66
|
|
|
$baseUrl .= ':' . $parsed['port']; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$baseUrl .= '/' . $prefix; |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
$config[DaftSource::class]['sources'] = $sources; |
|
72
|
|
|
$config[DaftSource::class]['cacheFile'] = ( |
|
73
|
|
|
__DIR__ . |
|
74
|
|
|
'/fixtures/http-kernel.fast-route.cache' |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
if (is_file($config[DaftSource::class]['cacheFile'])) { |
|
78
|
|
|
unlink($config[DaftSource::class]['cacheFile']); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$instance = Utilities::ObtainHttpHandlerInstance( |
|
82
|
|
|
$this, |
|
83
|
|
|
$implementation, |
|
84
|
|
|
$baseUrl, |
|
85
|
|
|
$basePath, |
|
86
|
|
|
$config |
|
87
|
|
|
); |
|
88
|
|
|
Utilities::ConfigureFrameworkInstance($this, $instance, $args[1]); |
|
89
|
|
|
|
|
90
|
|
|
$request = Request::create( |
|
91
|
|
|
$uri, |
|
92
|
|
|
$method, |
|
93
|
|
|
$parameters, |
|
94
|
|
|
$cookies, |
|
95
|
|
|
$files, |
|
96
|
|
|
$server, |
|
97
|
|
|
$content |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
yield [$instance, $request, $expectedStatus, $expectedContent]; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @dataProvider DataProviderHttpHandlerHandle |
|
107
|
|
|
*/ |
|
108
|
|
|
public function testHandlerGoodOnHttpHandler( |
|
109
|
|
|
HttpHandler $instance, |
|
110
|
|
|
Request $request, |
|
111
|
|
|
int $expectedStatus, |
|
112
|
|
|
string $expectedContent |
|
113
|
|
|
) : void { |
|
114
|
|
|
$response = $instance->handle($request); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertSame($expectedStatus, $response->getStatusCode()); |
|
117
|
|
|
$this->assertSame($expectedContent, $response->getContent()); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|