-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReport.php
236 lines (215 loc) · 7.36 KB
/
Report.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
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
<?php
declare(strict_types=1);
namespace Exacodis;
use DateTime;
use DateTimeZone;
use Exception;
use function htmlspecialchars;
use function is_array;
use function is_bool;
use function is_float;
use function is_int;
use function is_object;
use function is_resource;
use function is_scalar;
use function is_string;
use function mb_strlen;
use function mb_substr;
use function print_r;
use function round;
use const ENT_QUOTES;
class Report
{
/**
* @var Pilot
*/
protected Pilot $pilot;
/**
* @param Pilot $pilot
* @throws Exception
*/
public function __construct(Pilot $pilot)
{
$this->pilot = $pilot;
}
/**
* @param array|int|float|string $p
* @return array|string
* @throws Exception
*/
public function __invoke(array|int|float|string $p): array|string
{
$hsc = fn($p) => htmlspecialchars((string)$p, ENT_QUOTES);
$hsc_array = function(array $part) use (&$hsc_array, $hsc): array {
$data = [];
foreach ($part as $k => $v) {
$sk = $hsc($k);
if (is_array($v)) {
$data[$sk] = $hsc_array($v);
} elseif (is_scalar($v)) {
$data[$sk] = $hsc($v);
} else {
throw new Exception("Unable to generate the report: scalar value expected");
}
}
return $data;
};
if (is_array($p)) {
return $hsc_array($p);
} else {
return $hsc($p);
}
}
/**
* @param int $max_str_length
* @throws Exception
*/
public function create(int $max_str_length = 500): void
{
$short_string = fn(string $p): string => mb_strlen($p) > $max_str_length ? mb_substr($p, 0, $max_str_length).'...' : $p;
$date_time = new DateTime(timezone: new DateTimeZone('UTC'));
$stats = $this->pilot->getStats();
$global = $stats['failed_runs'] === 0
? '<span style="background-color: green; color: white;"> PASSED </span>'
: '<span style="background-color: red; color: white;"> FAILED </span>';
$html = [];
$html[] = <<<html
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>EXACODIS REPORT</title>
<style>
* {
font-family: "Courier New";
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 4px;
}
.failed {
background-color: yellow;
}
</style>
</head>
<body>
<h2>EXACODIS REPORT<br>{$date_time->format('Y-m-d H:i:s')} GMT/UTC/ZULU
<br>---
<br>Project: {$this($this->pilot->getProjectTitle())}
<br>---
<br>{$stats['nb_runs']} runs ⇒ {$stats['passed_runs']} passed + {$stats['failed_runs']} failed
⇔ {$stats['passed_runs_percent']} % passed + {$stats['failed_runs_percent']} % failed
<br>{$stats['nb_assertions']} assertions ⇒ {$stats['passed_assertions']} passed + {$stats['failed_assertions']} failed
⇔ {$stats['passed_assertions_percent']} % passed + {$stats['failed_assertions_percent']} % failed
<br>---
<br>Global test code evaluation: <strong>{$global}</strong><br>in {$stats['milliseconds']} ms ⇔ {$stats['hms']}
</h2>
<table>
<tbody>
html;
$header = function(Runner $runner): string {
$nb_assertions = $runner->getNbAssertions();
$plural = $nb_assertions >= 2 ? 's' : '';
if ($runner->hasPassed()) {
return <<<html
<tr><td colspan="4">Run: {$this($runner->getId())} - PASSED - {$runner->getMilliseconds()} ms -
{$nb_assertions} assertion{$plural}<br>{$this($runner->getDescription())}</td></tr>
html;
} else {
if ($nb_assertions) {
$nb_passed = $runner->getNbAssertionsPassed();
$nb_failed = $nb_assertions - $nb_passed;
$nb_passed_percent = round($nb_passed / $nb_assertions * 100, 2);
$nb_failed_percent = 100 - $nb_passed_percent;
} else {
$nb_passed = 0;
$nb_failed = 0;
$nb_passed_percent = 0;
$nb_failed_percent = 0;
}
return <<<html
<tr class="failed"><td colspan="4">Run: {$this($runner->getId())} - FAILED - {$runner->getMilliseconds()} ms -
{$nb_assertions} assertion{$plural} ⇛ {$nb_passed} passed + {$nb_failed} failed ⇔ {$nb_passed_percent} % passed + {$nb_failed_percent} % failed
<br>{$this($runner->getDescription())}</td></tr>
html;
}
};
foreach ($this->pilot->getAllRunners() as $runner) {
/** @var Runner $runner */
if ($runner->hasPassed()) {
// if all asserts in a run are ok, we only echo the global header and not the detail assert by assert
$html[] = $header($runner);
} else {
// otherwise, we echo the result assert by assert
$html[] = $header($runner);
foreach ($runner->getAssertResults() as $v) {
['result' => $result, 'expected' => $expected, 'test_name' => $test_name] = $v;
if ($result) {
$html[] = <<<html
<tr><td> </td><td colspan="3">PASSED: {$this((string)$test_name)}</td></tr>
html;
} else {
$html[] = <<<html
<tr>
<td rowspan="2"> </td>
<td class="failed" rowspan="2">FAILED: {$this((string)$test_name)}</td>
<td class="failed">Expected</td>
<td class="failed">{$this($short_string(print_r($expected, true)))}</td>
</tr>
<tr>
<td class="failed">Given</td>
<td class="failed">{$this->valueAnalyser($runner->getResult())}</td>
</tr>
html;
}
}
}
}
$html[] = <<<html
</tbody>
</table>
</body>
</html>
html;
echo implode('', $html);
}
/**
* @param mixed $v
* @param int $max_str_length
* @return string
* @throws Exception
*/
protected function valueAnalyser(mixed $v, int $max_str_length = 500): string
{
$short_string = fn(string $p): string => mb_strlen($p) >= $max_str_length ? mb_substr($p, 0, $max_str_length).'...' : $p;
if ($v === null) {
return 'null';
} elseif (is_string($v)) {
return $short_string('string: '.$this($v));
} elseif (is_array($v)) {
return $short_string('array<br>'.$this(print_r($v, true)));
} elseif (is_int($v)) {
return 'integer: '.$v;
} elseif (is_float($v)) {
return 'float: '.$v;
} elseif (is_bool($v)) {
return 'boolean: '.$v ? 'true' : 'false';
} elseif (is_object($v)) {
if ($v instanceof Exception) {
return 'Exception: '.$v::class
.'<br>Code: '.$v->getCode()
.'<br>Message: '.$v->getMessage()
.'<br>File: '.$v->getFile()
.'<br>Line: '.$v->getLine()
.'<br>Trace: '.$v->getTraceAsString();
} else {
return 'Object: '.$v::class;
}
} elseif (is_resource($v)) {
return $short_string('resource: '.$this(print_r($v, true)));
} else {
return 'UNKNOWN TYPE';
}
}
}