Repository navigation
Expand file tree
/
Copy pathrequest.php
More file actions
312 lines (284 loc) · 10.1 KB
/
Copy pathrequest.php
File metadata and controls
312 lines (284 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
$TEST_URL = 'http://localhost:8000';
include_once(__DIR__ . '/expect.php');
/**
* request(string $endpoint, array $args=[], array $headers=[]): Request
*
* request() executes a request to a url containing the global constant CONFIG_TEST_URL:
*
* CONFIG_TEST_URL . '/services/' . $object . '.php?'
*
* $endpoint and $args are built into a query and appended to $url.
*
* $headers are sent as headers.
*
* request() returns an object with the following properties:
*
* ->response - the response data JSON as an array
*
* ->info - the response info fropm curl_getinfo
*
* ->curl_error - the error message from curl_error
*
* And a endpoint, ->expect(), with the following signatures:
*
* ->expect(array $arg1): $this;
* - expect response to contain $arg1 and/or strings
* matching regex pattern values of $arg1.
*
* ->expect(string $arg1): $this;
* - expect response['errors'] to contain a value matching $arg1.
*
* ->expect(int $arg1): $this;
* - expect status code $arg1.
*
* ->expect(string $arg1, string $arg2): $this;
* - expect response[$arg1] to match $arg2.
*
* ->expect(string $arg1, mixed $arg2): $this;
* - expect response[$arg1] to equal $arg2.
*
* ->expect(int $arg1, array | string $arg2): $this;
* - expect status code $arg1 and expect response['errors'] to contain
* a value matching $arg2 if $arg1 is an HTTP Client Error,
* the response to contain $arg2 if it's an HTTP Success.
*
* Expectations may also be set on the response, info, or curl_errors attached to the request object.
*
* $request->expect->response->to_contain(['key' => 'value']);
*
* Or keys of the response array
*
* // (response['key'] === 'value')
* $request(...)->expect->key->to_equal('value');
*
* Unmet expectations fail the enclosing test().
*
* Usage Example:
*
* test('Test Name', function() {
*
* * Expect a 200 status code, a response containing 'value' at 'key',
* * and a string matching '/regex/' at 'key2'.
*
* request('endpoint', ['arg1' => 'value'])
* ->expect(200, ['key' => 'value', 'key2' => '/regex/']);
*
* });
*/
function
request(string $endpoint, array $args=[], array $headers=[]): Request
{
global $TEST_URL;
// Convert associative headers array to a list of strings like 'key: value'.
if (!array_is_list($headers)) {
$headers = array_map(fn($key, $value) => $key . ': ' . $value, array_keys($headers), $headers);
}
$curl_opts = [
CURLOPT_HTTPHEADER => $headers, // Send headers.
CURLOPT_RETURNTRANSFER => true, // Return the response as a string to decode as JSON.
];
try {
$object_url = $TEST_URL . $endpoint;
$query = http_build_query(['endpoint' => $endpoint] + $args);
$url = $object_url . $query;
$curl = curl_init($url);
curl_setopt_array($curl, $curl_opts);
$res = curl_exec($curl);
$response = json_decode($res, true) ?? ['errors' => [$res]];
$info = curl_getinfo($curl);
$curl_error = curl_error($curl);
} finally {
curl_close($curl);
}
return (new Request($curl_error, $info, $response));
}
class
Request
{
public function
__construct(
public string $curl_error,
public array $info,
public array $response
){}
/** Overload otherwise invalid calls to endpoints.
* Currently just a passthrough for ->expect() to enable it to be a property as well.
*/
public function
__call($endpoint, $args)
{
return (match($endpoint) {
'expect'
=> $this->expect(...$args),
default
=> throw new BadendpointCallException('No endpoint called ' . $endpoint . '.')
});
}
/**
* Overload otherwise invalid property access.
*
* Allows ->expect to be a property as well as a endpoint.
* This Exposes:
* ->expect->curl_error,
* ->expect->info,
* ->expect->response
* ->expect-><key> for each <key> in $response.
* All act as expect($value) with the corresponding value as its argument.
* They also pass through the _Request object to after expect() endpoint is executed.
*
* Additionally, values of the request array are available at their keys.
*
* Examples:
* $request('endpoint', [], $headers)
* ->expect->response->to_contain(['key' => 'value']);
*
* $request('endpoint', [], $headers)
* ->expect->info->to_satisfy(fn($info) => $info['content-type'] === 'application/json');
*
* $request('endpoint', [], $headers)
* ->expect->status->to_equal(200);
*
* (response['key'] === 'value')
* $value = $request('endpoint', [], $headers)
* ->key['value']
*/
public function
__get($name)
{
return (match($name) {
'expect'
=> (object) [
'curl_error' => expect($this->curl_error, $this),
'info' => expect($this->info, $this),
'response' => expect($this->response, $this),
...array_reduce(array_keys($this->response), fn($acc, $key) => [
...$acc,
$key => expect($this->response[$key], $this)
], [])
],
default
=> $this->response[$name] ??
throw new BadendpointCallException('Attempting to access ' . $$name . ' on response failed.')
});
}
// int $arg1, null | array | string $arg2
private function
http_status_code(int $arg1, null | array | string $arg2=null): void
{
try {
// int $arg1, mixed $arg2
expect($arg1)
// $arg1 === http status code.
->to_equal($this->info['http_code']);
} catch (Exception | AssertionError $e) {
throw new ErrorException(implode('\n\n', $this->response['errors'] ?? []) . "\n\n" . $e->getMessage());
}
match(true) {
// int $arg1
is_null($arg2) =>
// No op.
null,
// HTTP Success
200 <= $arg1 && $arg1 < 300 => match(true) {
// int $arg1, array | string $arg2
is_array($arg2) || is_string($arg2) =>
// response contains same or matching values as array $arg1 or matching string $arg1.
expect($this->response)->to_contain($arg2),
default => $this->invalid_arg2_with_http_status_code($arg2)
},
// HTTP Client Error
400 <= $arg1 && $arg1 < 500 => match(true) {
// int $arg1, array | string $arg2
is_array($arg2) || is_string($arg2) =>
// response['errors'] contains same or matching values as array $arg1 or matching string $arg1.
expect($this->response['errors'])->to_contain($arg2),
default => $this->invalid_arg2_with_http_status_code($arg2)
},
default => $this->unexpected_http_status_code($arg1)
};
}
private function
invalid_arg2_with_http_status_code(mixed $arg2): never
{
throw new InvalidArgumentException('$arg2 of->expect($arg1, $arg2) must be null, string, or array when $arg1 is an HTTP status code, not ' . gettype($arg2) . '.');
}
// string $arg1, mixed $arg2
private function
string_arg1(string $arg1, mixed $arg2=null): void
{
match (true) {
// string $arg1
is_null($arg2) =>
// reponse contains error matching $arg1.
expect($this->response['errors'])->to_contain($arg1),
// string $arg1, string $arg2
is_string($arg2) =>
// response[$arg1] matches $arg2.
expect($this->response[$arg1])->to_match($arg2),
// string $arg1, mixed $arg2
default =>
// response[$arg1] === $arg2.
expect($this->response[$arg1])->to_equal($arg2)
};
}
private function
unexpected_http_status_code(int $arg1): never
{
throw new UnexpectedValueException('Unexpected HTTP Status Code argument: ' . $arg1);
}
private function
expect(array | int | string $arg1, mixed $arg2=null): Request
{
match (true) {
// array $arg1
is_array($arg1) && is_null($arg2) =>
// response contains same or matching values as $arg1.
expect($this->response)->to_contain($arg1),
// int $arg1, null | array | string $arg2
is_int($arg1) =>
// $arg1 === http status code and check $arg2 if present.
$this->http_status_code($arg1, $arg2),
// string $arg1, mixed $arg2
is_string($arg1) =>
// Check for values matching $arg2 at response[$arg1] or, with null $arg2, match $arg1 with an error.
$this->string_arg1($arg1, $arg2),
default => throw new InvalidArgumentException(
'Invalid arguments for request->expect():' . "\n\t" . '$arg1: ' . gettype($arg1) . "\n\t" . '$arg2: ' . gettype($arg2)
)
};
return ($this);
}
/**
* Prints human readable response with ->print() in the call chain.
*
* With boolean true as an argument, prints curl_error and curl_getinfo.
*
* Example:
*
* request('endpoint', [], $headers)
* ->print()
* ->expect(200);
*
* Output:
*
* Array
* (
* [status] => 200
* )
*
*/
public function
print($verbose=false): Request
{
print_r($this->response);
if ($verbose) {
echo "\n";
print_r($this->info);
echo "\n";
print_r($this->curl_error);
}
return ($this);
}
}
?>