-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
TestStubs.php
73 lines (54 loc) · 1.29 KB
/
TestStubs.php
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
<?php
function value($value, ...$args)
{
return $value instanceof \Closure ? $value(...$args) : $value;
}
function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null)
{
$attempts = 0;
$backoff = [];
if (is_array($times)) {
$backoff = $times;
$times = count($times) + 1;
}
beginning:
$attempts++;
$times--;
try {
return $callback($attempts);
} catch (Exception $e) {
if ($times < 1 || ($when && !$when($e))) {
throw $e;
}
$sleepMilliseconds = $backoff[$attempts - 1] ?? $sleepMilliseconds;
if ($sleepMilliseconds) {
usleep(value($sleepMilliseconds, $attempts, $e) * 1000);
}
goto beginning;
}
}
function get()
{
$contents = file_get_contents('http://127.0.0.1:8000/');
if (!$contents) {
throw new Exception('No contents');
}
return $contents;
}
$home = retry(10, fn () => get(), 1000);
$needles = [
'<div id="app" data-components="',
'Welcome to your Splade application!',
];
$missing = [];
foreach ($needles as $needle) {
if (!str_contains($home, $needle)) {
$missing[] = $needle;
echo 'Not found: ' . $needle . PHP_EOL;
}
}
if (empty($missing)) {
echo 'OK';
exit(0);
}
exit(1);