-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPilot.php
490 lines (444 loc) · 13.8 KB
/
Pilot.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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
declare(strict_types=1);
namespace Exacodis;
use Exacodis\Runner;
use Exacodis\Report;
use Closure;
use Exception;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use function count;
use function is_file;
use function is_string;
use function round;
use function str_contains;
use const DIRECTORY_SEPARATOR;
/**
* @method assertCount(int $nb)
* @method assertEqual(mixed $to)
* @method assertException(object|string $class = 'Exception')
* @method assertIn(array $values)
* @method assertInStrict(array $values)
* @method assertIsArray()
* @method assertIsBool()
* @method assertIsFloat()
* @method assertIsInt()
* @method assertIsInstanceOf(object|string $class)
* @method assertIsNotInstanceOf(object|string $class)
* @method assertIsObject()
* @method assertIsResource()
* @method assertIsScalar()
* @method assertIsString()
* @method assertNotEqual(mixed $to)
* @method assertNotException(object|string $class = 'Exception')
* @method assertNotIn(array $values)
* @method assertNotInStrict(array $values)
*/
class Pilot
{
/**
* @var int
*/
private static int $counter = -1;
/**
* @var string
*/
private string $project_title;
/**
* @var array
*/
private array $helpers = [];
/**
* @var array [resource's name => mixed]
*/
private array $resources = [];
/**
* @var array [Runner]
*/
private array $runners = [];
/**
* @var Runner|null
*/
private Runner|null $current_runner = null;
/**
* @var Report HTML Report Builder
*/
private Report $report;
/**
* @var array
*/
private array $stats = ['passed_assertions' => 0, 'failed_assertions' => 0];
/**
* @var int
*/
private int $milliseconds = 0;
/**
* @param string $project_title
* @param Report|null $report
* @throws Exception
*/
public function __construct(string $project_title, ?Report $report = null)
{
$this->project_title = $project_title;
$this->report = $report ?? new Report($this);
}
/**
* @param string $name
* @param mixed $args
* @return mixed
* @throws Exception
*/
public function __call(string $name, mixed $args): mixed
{
if (isset($this->helpers[$name])) {
$helper = $this->helpers[$name];
return $helper(...$args);
} else {
throw new Exception("Unknown helper: {$name}");
}
}
//region helpers
/**
* @param string $name
* @param Closure $helper
*/
public function addHelper(string $name, Closure $helper): void
{
$helper->bindTo($this, $this);
$this->helpers[$name] = $helper;
}
/**
* @param array $helpers [name => Closure]
*/
public function addHelpers(array $helpers): void
{
foreach ($helpers as $name => $closure) {
if ($closure instanceof Closure) {
$this->addHelper($name, $closure);
}
}
}
/**
* By default, the standard helper library is 'stdPilotHelpers.php'
* localised in the same directory as this file
* @param string|null $full_path
* @throws Exception
*/
public function injectStandardHelpers(?string $full_path = null): void
{
$filename = $full_path ?? __DIR__.DIRECTORY_SEPARATOR.'stdExacodisHelpers.php';
$this->injectHelpers($filename);
}
/**
* Inject the helpers from a file
* @param string $path
* @throws Exception
*/
public function injectHelpers(string $path): void
{
if (is_file($path)) {
$this->addHelpers(include $path);
} else {
throw new Exception("Helper file not found: {$path}");
}
}
//endregion
//region Resource
/**
* @param string $name
* @param mixed $resource
* @throws Exception
*/
public function addResource(string $name, mixed $resource): void
{
if (isset($this->resources[$name])) {
throw new Exception("Resource's name: {$name} is already defined");
} else {
$this->resources[$name] = $resource;
}
}
/**
* @param string $name
* @param mixed $resource
* @throws Exception
*/
public function overrideResource(string $name, mixed $resource): void
{
if (isset($this->resources[$name])) {
$this->resources[$name] = $resource;
} else {
throw new Exception("Resource's name: {$name} is not defined");
}
}
/**
* @param string $name
* @return mixed
* @throws Exception
*/
public function getResource(string $name): mixed
{
if (isset($this->resources[$name])) {
return $this->resources[$name];
} else {
throw new Exception("Resource: {$name} does not exist");
}
}
/**
* @param string $name
* @throws Exception
*/
public function removeResource(string $name): void
{
if (isset($this->resources[$name])) {
unset($this->resources[$name]);
} else {
throw new Exception("Resource: {$name} does not exist");
}
}
//endregion
/**
* @param int|string|null $id
* @param Closure $test
* @param string|null $description
* @return int|string Test id
* @throws Exception("Runner's id: {$id} is already defined and locked")
* @throws Exception
*/
public function run(int|string|null $id, Closure $test, string $description = ''): int|string
{
$id = $this->getRunnerId($id);
$runner = new Runner($test, $description);
$runner->setId($id);
$this->current_runner = $runner;
$this->runners[$id] = $runner;
$this->milliseconds += $runner->getMilliseconds();
return $id;
}
/**
* For testing purpose of protected or private methods in a class instance
*
* @param int|string|null $id
* @param object|string $class
* @param string $description
* @param string|null $method
* @param array $params
* @return int|string
* @throws ReflectionException
* @throws Exception('The class cannot be a string, it must be an object')
* @throws Exception('The method must not be empty')
*/
public function runClassMethod(
int|string|null $id,
object|string $class,
string $description = '',
?string $method = null,
array $params = [],
) {
$id = $this->getRunnerId($id);
if (is_string($class)) {
// intercept the short notation class::method
if (str_contains($class, '::')) {
[$class, $method] = explode('::', $class);
}
// the class constructor must not have any required parameters
// otherwise the given class must already be instanced (object and not a string)
$reflection_class = new ReflectionClass($class);
$constructor = $reflection_class->getConstructor();
if (($constructor !== null) && ($constructor->getNumberOfRequiredParameters()) > 0) {
throw new Exception('The class cannot be a string, it must be an object');
}
$class = new $class;
}
if (empty($method)) {
throw new Exception('The method must not be empty');
}
$reflection_method = new ReflectionMethod($class, $method);
$reflection_method->setAccessible(true);
if ($reflection_method->isStatic()) {
$class = null;
}
$runner = new Runner(fn() => $reflection_method->invoke($class, ...$params), $description);
$runner->setId($id);
$this->current_runner = $runner;
$this->runners[$id] = $runner;
$this->milliseconds += $runner->getMilliseconds();
return $id;
}
/**
* @param int|string|null $id
* @return int|string
* @throws Exception("Runner's id: {$id} is already defined and locked")
*/
private function getRunnerId(int|string|null $id): int|string
{
if ($id === null) {
return ++self::$counter;
} elseif (isset($this->runners[$id])) {
throw new Exception("Runner's id: {$id} is already defined and locked");
} else {
return $id;
}
}
/**
* @param int $max_str_length
* @throws Exception
*/
public function createReport(int $max_str_length = 500): void
{
$this->report->create($max_str_length);
}
/**
* @return string
*/
public function getProjectTitle(): string
{
return $this->project_title;
}
/**
* @param int|string|null $id If null then extract the latest run
* @return Runner
* @throws Exception
*/
public function getRunner(int|string|null $id = null): Runner
{
return $this->runners[$this->getValidRunnerId($id)];
}
/**
* @return array [Runner]
*/
public function getAllRunners(): array
{
return $this->runners;
}
/**
* Extract an existing test runner and set it as the current one
*
* @param int|string $id
* @throws Exception("No test ran")
* @throws Exception("Unknown runner's id: '{$id}'")
*/
public function setCurrentRunnerTo(int|string $id): void
{
$this->current_runner = $this->runners[$this->getValidRunnerId($id)];
}
/**
* @param int|string|null $id If null then apply to the last run
* @return int|string
* @throws Exception("No test ran")
* @throws Exception("Unknown runner's id: '{$id}'")
*/
private function getValidRunnerId(int|string|null $id = null): int|string
{
if (isset($id)) {
if (isset($this->runners[$id])) {
return $this->runners[$id]->getId();
} else {
throw new Exception("Unknown runner's id: '{$id}'");
}
} elseif (isset($this->current_runner)) {
return $this->current_runner->getId();
} else {
throw new Exception("No test ran");
}
}
/**
* @param Closure $test
* @param string|null $test_name
* @param mixed $expected
*/
public function assert(Closure $test, ?string $test_name = null, mixed $expected = null)
{
$runner_id = $this->getValidRunnerId();
if ($test() === true) {
$this->runners[$runner_id]->addAssertResult(
result: true,
test_name: $test_name
);
$this->stats['passed_assertions'] += 1;
} else {
$this->runners[$runner_id]->addAssertResult(
result: false,
test_name: $test_name,
expected: $expected
);
$this->stats['failed_assertions'] += 1;
}
}
/**
* @param string|null $test_name
* @throws Exception
*/
public function addSuccess(string|null $test_name = null): void
{
$runner_id = $this->getValidRunnerId();
$this->runners[$runner_id]->addAssertResult(result: true, test_name: $test_name);
$this->stats['passed_assertions'] += 1;
}
/**
* @param array|int|float|string $expected
* @param string|null $test_name
* @throws Exception
*/
public function addFailure(array|int|float|string $expected, string|null $test_name = null): void
{
$runner_id = $this->getValidRunnerId();
$this->runners[$runner_id]->addAssertResult(result: false, expected: $expected, test_name: $test_name);
$this->stats['failed_assertions'] += 1;
}
/**
* @return array
*/
public function getStats(): array
{
// runs
$nb_runs = count($this->runners);
$passed_runs = 0;
/** @var Runner $runner */
foreach ($this->runners as $runner) {
if ($runner->hasPassed()) {
++$passed_runs;
}
}
$failed_runs = $nb_runs - $passed_runs;
if ($nb_runs) {
$passed_runs_percent = round($passed_runs / $nb_runs * 100, 2);
$failed_runs_percent = (100 - $passed_runs_percent);
} else {
$passed_runs_percent = 0;
$failed_runs_percent = 0;
}
// assertions
['passed_assertions' => $passed_assertions, 'failed_assertions' => $failed_assertions] = $this->stats;
$nb_assertions = $passed_assertions + $failed_assertions;
if ($nb_assertions) {
$passed_assertions_percent = round($passed_assertions / $nb_assertions * 100, 2);
$failed_assertions_percent = (100 - $passed_assertions_percent);
} else {
$passed_assertions_percent = 0;
$failed_assertions_percent = 0;
}
// convert seconds to hms
$converter = function(int $seconds): string {
$base_60 = fn ($p) => ($p >= 60) ? [$p % 60, intdiv($p, 60)] : [$p, 0];
$sec = $base_60($seconds);
$min = $base_60($sec[1]);
$hours = $min[1];
return sprintf("%02d:%02d:%02d", $hours, $min[0], $sec[0]);
};
return [
'nb_runs' => $nb_runs,
'passed_runs' => $passed_runs,
'failed_runs' => $failed_runs,
'passed_runs_percent' => $passed_runs_percent,
'failed_runs_percent' => $failed_runs_percent,
'nb_assertions' => $nb_assertions,
'passed_assertions' => $passed_assertions,
'failed_assertions' => $failed_assertions,
'passed_assertions_percent' => $passed_assertions_percent,
'failed_assertions_percent' => $failed_assertions_percent,
'milliseconds' => $this->milliseconds,
'hms' => $converter(intdiv($this->milliseconds, 1000)),
];
}
}