-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runner.php
182 lines (162 loc) · 3.62 KB
/
Runner.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
<?php
declare(strict_types=1);
namespace Exacodis;
use Closure;
use Exception;
use function count;
use function hrtime;
/**
* Project Exacodis Test PHP Framework
* @author Martin LACROIX - [email protected]
* @license MIT
* @copyright 2021+
*/
class Runner
{
/**
* @var int|string|null
*/
private int|string|null $id = null;
/**
* @var string
*/
private string $description = '';
/**
* @var mixed|Exception
*/
private mixed $result = null;
/**
* @var int
*/
private int $milliseconds;
/**
* @var array
*/
private array $assert_results = [];
/**
* @param Closure $closure
* @param string $description
*/
public function __construct(Closure $closure, string $description = '')
{
$this->description = $description;
$start = $end = 0;
try {
$start = hrtime(true);
$this->result = $closure();
$end = hrtime(true);
} catch (Exception $e) {
$this->result = $e;
$end = hrtime(true);
}
$this->milliseconds = (int)(($end - $start) / 1e+6); // convert nanoseconds to milliseconds
}
/**
* @return mixed
*/
public function getResult(): mixed
{
return $this->result;
}
/**
* @return int
*/
public function getMilliseconds(): int
{
return $this->milliseconds;
}
/**
* Once defined not updatable
*
* @param int|string $id
* @throws Exception
*/
public function setId(int|string $id)
{
if (isset($this->id)) {
throw new Exception("Test id is already defined and locked, not updatable with '{$id}'");
} else {
$this->id = $id;
}
}
/**
* @return int|string|null
*/
public function getId(): int|string|null
{
return $this->id;
}
/**
* @return string
*/
public function getDescription(): string
{
return $this->description;
}
/**
* @param bool $result
* @param array|int|float|string|null $expected
* @param string|null $test_name
*/
public function addAssertResult(bool $result, array|int|float|string|null $expected = null, string|null $test_name = null): void
{
$this->assert_results[] = ['result' => $result, 'expected' => $expected, 'test_name' => $test_name];
}
/**
* @return array [[result => bool, expected => string|null, test_name => string|null]]
*/
public function getAssertResults(): array
{
return $this->assert_results;
}
/**
* @return int
*/
public function getNbAssertions(): int
{
return count($this->assert_results);
}
/**
* @return int
*/
public function getNbAssertionsPassed(): int
{
$nb = 0;
foreach ($this->assert_results as $v) {
if ($v['result']) {
++$nb;
}
}
return $nb;
}
/**
* @return bool
*/
public function getStatus(): bool
{
if (empty($this->assert_results)) {
return false;
} else {
foreach ($this->assert_results as $v) {
if ($v['result'] === false) {
return false;
}
}
return true;
}
}
/**
* @return bool
*/
public function hasPassed(): bool
{
return $this->getStatus() === true;
}
/**
* @return bool
*/
public function hasFailed(): bool
{
return $this->getStatus() === false;
}
}