-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
95 lines (79 loc) · 3.1 KB
/
test.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace Tapping;
use function Task\{forkTask, getProcessStatus};
use function Crayon\{text};
use function Load\{dots,removeLastCharsByCount};
use TryPhp\{PredictOutputTrait, PredictIsTrait, PredictExceptionTrait};
/**
* function to trigger a atomic test run and show an success/fail indicator (and on provided flag exit the parent process)
* @param string $description
* @param callable $test
*/
function test(string $description, callable $test) {
$options = getopt('bq', ['build', 'quite']);
$buildFlag = isset($options['b']) || isset($options['build']);
$quiteFlag = isset($options['q']) || isset($options['quite']);
$phpOutputBuffer = 'php://output';
$loadingDescription = text($description)->yellow();
$successIndicator = text('ok')->green();
$successMessage = "$description $successIndicator";
$failIndicator = text('not ok')->red();
$failMessage = "$description $failIndicator";
$cleanupBlock = str_repeat(' ', strlen($description) + 2);
$redBlock = text('▌')->red();
if (!$quiteFlag) {
set_error_handler(function ($code, $message, $file, $line) use ($redBlock, $cleanupBlock, $phpOutputBuffer) {
$errorMessage = text($message)->red();
$errorMessage = "\r$redBlock $errorMessage (Error $code) $cleanupBlock";
$errorMessage .= "\n$redBlock $file #$line";
fwrite(fopen($phpOutputBuffer, 'w'), "$errorMessage\n\n");
});
}
try {
$pid = forkTask($test, [new class() {
use PredictOutputTrait;
use PredictIsTrait;
use PredictExceptionTrait;
}]);
} catch(\Exception $ex) {
if (!$quiteFlag) {
$exceptionFile = text($ex->getFile());
$exceptionLine = text($ex->getLine());
$exceptionMessage = text($ex->getMessage())->red();
$exceptionOutput = "\r$cleanupBlock";
$exceptionOutput .= "\n$redBlock $description";
$exceptionOutput .= "\n$redBlock $exceptionFile #$exceptionLine";
$exceptionOutput .= "\n$redBlock $exceptionMessage\n";
fwrite(fopen($phpOutputBuffer, 'w'), "$exceptionOutput\n");
}
exit(1);
}
if ($pid > 0) {
try {
dots(function () use ($pid, $loadingDescription) {
$processStatus = getProcessStatus($pid, $status);
if ($processStatus > 0) {
return $status === 0;
}
return "$loadingDescription";
}, "$successMessage");
} catch (\Exception $ex) {
fwrite(fopen($phpOutputBuffer, 'w'), "$failMessage\n");
// exit parent process if in a ci run
if ($buildFlag) {
exit(1);
}
}
} else {
exit;
}
}
/**
* function to trigger an indicator that there is something to do on test run
* @param string $description
*/
function todo(string $description) {
$todoIndicator = text('todo')->magenta()->italic();
$todoDescription = text($description);
fwrite(fopen('php://output', 'w'), "$todoIndicator $todoDescription\n");
}