-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubClientTest.php
More file actions
427 lines (354 loc) · 13.1 KB
/
Copy pathGitHubClientTest.php
File metadata and controls
427 lines (354 loc) · 13.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
declare(strict_types=1);
namespace LightTest\Unit\App\Service;
use JsonException;
use Light\App\Service\GitHubClient;
use Light\App\Service\GitHubClientInterface;
use LightTest\Unit\UnitTest;
use ReflectionMethod;
use ReflectionProperty;
use RuntimeException;
use function fclose;
use function feof;
use function fgets;
use function file_get_contents;
use function fsockopen;
use function function_exists;
use function fwrite;
use function is_file;
use function is_resource;
use function json_decode;
use function proc_close;
use function proc_get_status;
use function proc_open;
use function proc_terminate;
use function sprintf;
use function str_contains;
use function stream_set_timeout;
use function stream_socket_get_name;
use function stream_socket_server;
use function strrchr;
use function substr;
use function sys_get_temp_dir;
use function tempnam;
use function trim;
use function unlink;
use function usleep;
use const DIRECTORY_SEPARATOR;
use const JSON_THROW_ON_ERROR;
use const PHP_BINARY;
use const PHP_EOL;
/**
* Drives the real cURL transport against a local stand-in for the GitHub API.
*
* `GitHubClient::absoluteUrl()` passes absolute URLs through untouched, which is the seam that
* lets these tests point the client at 127.0.0.1 without any production code changing.
*/
class GitHubClientTest extends UnitTest
{
private const string TOKEN = 'gh-test-token';
private const string USER_AGENT = 'dotkernel.com-test';
/**
* Spawning the stand-in needs process and socket functions that a hardened PHP build may
* have disabled. Skipping with a named reason beats an unexplained error.
*/
private const array REQUIRED_FUNCTIONS = [
'proc_open',
'proc_get_status',
'proc_terminate',
'stream_socket_server',
'fsockopen',
];
/** @var resource|null */
private static $server;
private static string $baseUrl = '';
/**
* The stand-in's own output, kept so a startup failure can report why rather than vanishing.
*/
private static ?string $outputFile = null;
public static function setUpBeforeClass(): void
{
foreach (self::REQUIRED_FUNCTIONS as $function) {
if (! function_exists($function)) {
self::markTestSkipped(sprintf(
'%s() is unavailable, so the local GitHub API stand-in cannot be started.',
$function
));
}
}
$router = __DIR__ . DIRECTORY_SEPARATOR . 'Fixture' . DIRECTORY_SEPARATOR . 'github-api-server.php';
if (! is_file($router)) {
self::fail(sprintf('The GitHub API stand-in is missing from %s.', $router));
}
$outputFile = tempnam(sys_get_temp_dir(), 'github-api-server-');
if ($outputFile === false) {
self::fail('Unable to create a log file for the local GitHub API stand-in.');
}
self::$outputFile = $outputFile;
$port = self::findFreePort();
$command = [PHP_BINARY, '-S', sprintf('127.0.0.1:%d', $port), $router];
$process = proc_open(
$command,
[
0 => ['file', '/dev/null', 'r'],
1 => ['file', $outputFile, 'a'],
2 => ['file', $outputFile, 'a'],
],
$pipes
);
if (! is_resource($process)) {
self::markTestSkipped(sprintf('Unable to run %s -S 127.0.0.1:%d.', PHP_BINARY, $port));
}
self::$server = $process;
self::$baseUrl = sprintf('http://127.0.0.1:%d', $port);
self::waitForServer($port);
}
public static function tearDownAfterClass(): void
{
if (is_resource(self::$server)) {
proc_terminate(self::$server);
proc_close(self::$server);
}
if (self::$outputFile !== null && is_file(self::$outputFile)) {
unlink(self::$outputFile);
}
self::$server = null;
self::$baseUrl = '';
self::$outputFile = null;
}
public function testGetReturnsTheResponseBody(): void
{
$this->assertSame('{"lifecycle":"active"}', $this->createClient()->get($this->url('/ok')));
}
public function testGetReturnsNullWhenTheResourceDoesNotExist(): void
{
$this->assertNull($this->createClient()->get($this->url('/missing')));
}
public function testGetThrowsOnAnUnexpectedStatus(): void
{
$url = $this->url('/server-error');
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(sprintf('GitHub returned HTTP 500 for %s.', $url));
$this->createClient()->get($url);
}
public function testGetFollowsRedirects(): void
{
$this->assertSame('{"lifecycle":"active"}', $this->createClient()->get($this->url('/redirect')));
}
public function testGetThrowsWhenTheTransportFails(): void
{
$url = sprintf('http://127.0.0.1:%d/ok', self::findFreePort());
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(sprintf('Request to %s failed:', $url));
$this->createClient()->get($url);
}
/**
* @throws JsonException
*/
public function testGetSendsTheExpectedRequestHeaders(): void
{
$body = $this->createClient()->get($this->url('/echo-request'));
$this->assertIsString($body);
$this->assertSame([
'accept' => GitHubClientInterface::ACCEPT_JSON,
'apiVersion' => '2022-11-28',
'authorization' => 'Bearer ' . self::TOKEN,
'userAgent' => self::USER_AGENT,
], json_decode($body, true, 512, JSON_THROW_ON_ERROR));
}
/**
* @throws JsonException
*/
public function testGetSendsTheRequestedAcceptHeader(): void
{
$body = $this->createClient()->get($this->url('/echo-request'), GitHubClientInterface::ACCEPT_RAW);
$this->assertIsString($body);
$decoded = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
$this->assertIsArray($decoded);
$this->assertSame(GitHubClientInterface::ACCEPT_RAW, $decoded['accept']);
}
/**
* An unauthenticated client still works, it just has a lower rate limit.
*
* @throws JsonException
*/
public function testGetOmitsTheAuthorizationHeaderWithoutAToken(): void
{
$body = $this->createClient('')->get($this->url('/echo-request'));
$this->assertIsString($body);
$decoded = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
$this->assertIsArray($decoded);
$this->assertSame('', $decoded['authorization']);
}
/**
* cURL rejects an empty user agent and GitHub rejects requests without one.
*
* @throws JsonException
*/
public function testAnEmptyUserAgentFallsBackToTheDefault(): void
{
$body = $this->createClient(self::TOKEN, '')->get($this->url('/echo-request'));
$this->assertIsString($body);
$decoded = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
$this->assertIsArray($decoded);
$this->assertSame('dotkernel.com', $decoded['userAgent']);
}
public function testGetAllPagesFollowsNextLinksAndDiscardsNonArrayMembers(): void
{
$this->assertSame(
[['name' => 'one'], ['name' => 'two'], ['name' => 'three']],
$this->createClient()->getAllPages($this->url('/page-1'))
);
}
public function testGetAllPagesReturnsASinglePageWhenThereIsNoNextLink(): void
{
$this->assertSame([['name' => 'three']], $this->createClient()->getAllPages($this->url('/page-2')));
}
public function testGetAllPagesThrowsWhenThePayloadIsNotAJsonArray(): void
{
$url = $this->url('/not-an-array');
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(sprintf('Expected a JSON array from %s.', $url));
$this->createClient()->getAllPages($url);
}
public function testGetAllPagesThrowsOnAnUnexpectedStatus(): void
{
$url = $this->url('/server-error');
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(sprintf('GitHub returned HTTP 500 for %s.', $url));
$this->createClient()->getAllPages($url);
}
/**
* A relative path is resolved against the API root rather than being used verbatim.
*/
public function testRelativePathsAreResolvedAgainstTheApiRoot(): void
{
$client = $this->createClient();
$this->assertSame(
'https://api.github.com/orgs/dotkernel/repos',
$this->invokeAbsoluteUrl($client, '/orgs/dotkernel/repos')
);
$this->assertSame(
'http://example.com/passed-through',
$this->invokeAbsoluteUrl($client, 'http://example.com/passed-through')
);
$this->assertSame(
'https://example.com/passed-through',
$this->invokeAbsoluteUrl($client, 'https://example.com/passed-through')
);
}
private function invokeAbsoluteUrl(GitHubClient $client, string $path): string
{
$method = new ReflectionMethod($client, 'absoluteUrl');
$result = $method->invoke($client, $path);
$this->assertIsString($result);
return $result;
}
public function testTheConfiguredUserAgentIsKept(): void
{
$property = new ReflectionProperty(GitHubClient::class, 'userAgent');
$this->assertSame(self::USER_AGENT, $property->getValue($this->createClient()));
}
private function createClient(string $token = self::TOKEN, string $userAgent = self::USER_AGENT): GitHubClient
{
return new GitHubClient($token, $userAgent, 10, 5);
}
/**
* @return non-empty-string
*/
private function url(string $path): string
{
/** @var non-empty-string $url */
$url = self::$baseUrl . $path;
$this->assertNotSame('', $url);
return $url;
}
/**
* Binding to port 0 lets the OS pick a port that is known to be free.
*/
private static function findFreePort(): int
{
$socket = stream_socket_server('tcp://127.0.0.1:0', $errorNumber, $errorMessage);
if (! is_resource($socket)) {
self::fail(sprintf('Unable to reserve a local port: %s', $errorMessage));
}
$name = stream_socket_get_name($socket, false);
fclose($socket);
if ($name === false) {
self::fail('Unable to determine the reserved local port.');
}
$port = strrchr($name, ':');
return $port === false ? 0 : (int) substr($port, 1);
}
private static function waitForServer(int $port): void
{
for ($attempt = 0; $attempt < 100; $attempt++) {
if (is_resource(self::$server)) {
$status = proc_get_status(self::$server);
if ($status['running'] === false) {
self::markTestSkipped(sprintf(
'The local GitHub API stand-in exited with code %d.%s',
$status['exitcode'],
self::serverOutput()
));
}
}
if (self::probeWithARealRequest($port)) {
return;
}
usleep(50_000);
}
self::markTestSkipped(sprintf(
'The local GitHub API stand-in never accepted a connection on port %d.%s',
$port,
self::serverOutput()
));
}
/**
* Sends a complete request and reads the whole response back.
*
* The stand-in is single threaded, so it must be allowed to finish a full request cycle
* before the first test runs. Probing by opening a connection and dropping it without
* sending anything leaves the server reading EOF, and the next response comes back empty.
*/
private static function probeWithARealRequest(int $port): bool
{
$connection = @fsockopen('127.0.0.1', $port, $errorNumber, $errorMessage, 0.5);
if (! is_resource($connection)) {
return false;
}
stream_set_timeout($connection, 1);
fwrite($connection, sprintf(
"GET /ok HTTP/1.0%sHost: 127.0.0.1:%d%sUser-Agent: readiness-probe%s%s",
"\r\n",
$port,
"\r\n",
"\r\n",
"\r\n"
));
$response = '';
while (! feof($connection)) {
$chunk = fgets($connection);
if ($chunk === false) {
break;
}
$response .= $chunk;
}
fclose($connection);
return str_contains($response, '200') && str_contains($response, '{"lifecycle":"active"}');
}
/**
* Whatever the stand-in wrote before giving up, appended to a failure message.
*/
private static function serverOutput(): string
{
if (self::$outputFile === null || ! is_file(self::$outputFile)) {
return '';
}
$output = file_get_contents(self::$outputFile);
if ($output === false || trim($output) === '') {
return ' It produced no output.';
}
return sprintf("%sIt reported:%s%s", PHP_EOL, PHP_EOL, trim($output));
}
}