1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author SignpostMarv |
4
|
|
|
*/ |
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftFramework\Tests; |
8
|
|
|
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use RuntimeException; |
11
|
|
|
use SignpostMarv\DaftFramework\Framework; |
12
|
|
|
use SignpostMarv\DaftFramework\HttpHandler; |
13
|
|
|
|
14
|
|
|
class Utilities |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param mixed ...$implementationArgs |
18
|
|
|
*/ |
19
|
|
|
public static function ObtainFrameworkInstanceMixedArgs( |
20
|
|
|
TestCase $testCase, |
21
|
|
|
string $implementation, |
22
|
|
|
...$implementationArgs |
23
|
|
|
) : Framework { |
24
|
|
|
$testCase::assertIsString($implementationArgs[0] ?? null); |
|
|
|
|
25
|
|
|
$testCase::assertIsString($implementationArgs[1] ?? null); |
26
|
|
|
$testCase::assertIsArray($implementationArgs[2] ?? null); |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array{0:string, 1:string, 2:array} |
30
|
|
|
*/ |
31
|
|
|
$implementationArgs = $implementationArgs; |
32
|
|
|
|
33
|
|
|
list($baseUrl, $basePath, $config) = $implementationArgs; |
34
|
|
|
$implementationArgs = array_slice($implementationArgs, 3); |
35
|
|
|
|
36
|
|
|
return static::ObtainFrameworkInstance( |
37
|
|
|
$testCase, |
38
|
|
|
$implementation, |
39
|
|
|
$baseUrl, |
40
|
|
|
$basePath, |
41
|
|
|
$config, |
42
|
|
|
...$implementationArgs |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param mixed ...$implementationArgs |
48
|
|
|
*/ |
49
|
|
|
public static function ObtainFrameworkInstance( |
50
|
|
|
TestCase $testCase, |
51
|
|
|
string $implementation, |
52
|
|
|
string $baseUrl, |
53
|
|
|
string $basePath, |
54
|
|
|
array $config, |
55
|
|
|
...$implementationArgs |
56
|
|
|
) : Framework { |
57
|
|
|
if ( ! is_a($implementation, Framework::class, true)) { |
58
|
|
|
$testCase::assertTrue( |
59
|
|
|
is_a($implementation, Framework::class, true), |
60
|
|
|
sprintf( |
61
|
|
|
'Argument %u passed to %s must be an implementation of %s', |
62
|
|
|
1, |
63
|
|
|
__METHOD__, |
64
|
|
|
Framework::class |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
throw new RuntimeException('unreachable line here'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var Framework |
73
|
|
|
*/ |
74
|
|
|
$out = new $implementation($baseUrl, $basePath, $config, ...$implementationArgs); |
75
|
|
|
|
76
|
|
|
return $out; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param mixed ...$implementationArgs |
81
|
|
|
*/ |
82
|
|
|
public static function ObtainHttpHandlerInstanceMixedArgs( |
83
|
|
|
TestCase $testCase, |
84
|
|
|
string $implementation, |
85
|
|
|
...$implementationArgs |
86
|
|
|
) : HttpHandler { |
87
|
|
|
$testCase::assertIsString($implementationArgs[0] ?? null); |
88
|
|
|
$testCase::assertIsString($implementationArgs[1] ?? null); |
89
|
|
|
$testCase::assertIsArray($implementationArgs[2] ?? null); |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var array{0:string, 1:string, 2:array} |
93
|
|
|
*/ |
94
|
|
|
$implementationArgs = $implementationArgs; |
95
|
|
|
|
96
|
|
|
list($baseUrl, $basePath, $config) = $implementationArgs; |
97
|
|
|
$implementationArgs = array_slice($implementationArgs, 3); |
98
|
|
|
|
99
|
|
|
return static::ObtainHttpHandlerInstance( |
100
|
|
|
$testCase, |
101
|
|
|
$implementation, |
102
|
|
|
$baseUrl, |
103
|
|
|
$basePath, |
104
|
|
|
$config, |
105
|
|
|
...$implementationArgs |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param mixed ...$implementationArgs |
111
|
|
|
*/ |
112
|
|
|
public static function ObtainHttpHandlerInstance( |
113
|
|
|
TestCase $testCase, |
114
|
|
|
string $implementation, |
115
|
|
|
string $baseUrl, |
116
|
|
|
string $basePath, |
117
|
|
|
array $config, |
118
|
|
|
...$implementationArgs |
119
|
|
|
) : HttpHandler { |
120
|
|
|
$testCase::assertTrue( |
121
|
|
|
is_a($implementation, HttpHandler::class, true), |
122
|
|
|
sprintf( |
123
|
|
|
'Argument %u passed to %s must be an implementation of %s', |
124
|
|
|
1, |
125
|
|
|
__METHOD__, |
126
|
|
|
HttpHandler::class |
127
|
|
|
) |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @var HttpHandler |
132
|
|
|
*/ |
133
|
|
|
$instance = static::ObtainFrameworkInstanceMixedArgs( |
134
|
|
|
$testCase, |
135
|
|
|
$implementation, |
136
|
|
|
$baseUrl, |
137
|
|
|
$basePath, |
138
|
|
|
$config, |
139
|
|
|
...$implementationArgs |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
return $instance; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param array<string, mixed[]> $postConstructionCalls |
147
|
|
|
*/ |
148
|
|
|
public static function ConfigureFrameworkInstance( |
149
|
|
|
TestCase $testCase, |
150
|
|
|
Framework $instance, |
151
|
|
|
array $postConstructionCalls |
152
|
|
|
) : void { |
153
|
|
|
if (count($postConstructionCalls) > 0) { |
154
|
|
|
foreach (array_keys($postConstructionCalls) as $method) { |
155
|
|
|
$testCase::assertTrue(method_exists($instance, $method), sprintf( |
156
|
|
|
'Argument %u passed to %s must contain keys referring to methods on %s', |
157
|
|
|
2, |
158
|
|
|
__METHOD__, |
159
|
|
|
get_class($instance) |
160
|
|
|
)); |
161
|
|
|
|
162
|
|
|
$instance->$method(...($postConstructionCalls[$method])); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|