This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestResult.php
272 lines (230 loc) · 5.92 KB
/
TestResult.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
<?php
declare(strict_types=1);
/**
* Copyright (c) 2020 Daniel Bannert
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/testomat/phpunit
*/
namespace Testomat\PHPUnit\Printer;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\BaseTestRunner;
use SebastianBergmann\Exporter\Exporter;
/**
* @internal
*/
final class TestResult
{
/**
* @readonly
*
* @var int
*/
public const RUNS = 7;
/**
* @readonly
*
* @var array<int, string>
*/
public const MAPPER = [
BaseTestRunner::STATUS_UNKNOWN => 'unknown',
BaseTestRunner::STATUS_PASSED => 'passed',
BaseTestRunner::STATUS_SKIPPED => 'skipped',
BaseTestRunner::STATUS_INCOMPLETE => 'incomplete',
BaseTestRunner::STATUS_FAILURE => 'failure',
BaseTestRunner::STATUS_ERROR => 'error',
BaseTestRunner::STATUS_RISKY => 'risky',
BaseTestRunner::STATUS_WARNING => 'warning',
self::RUNS => 'pending',
];
/**
* @readonly
*
* @var string
*/
public $class;
/**
* @readonly
*
* @var string
*/
public $method;
/**
* @readonly
*
* @var string
*/
public $description;
/**
* @readonly
*
* @var int
*/
public $type;
/**
* @readonly
*
* @var string
*/
public $icon;
/**
* @readonly
*
* @var null|float
*/
public $time;
/**
* @readonly
*
* @var string
*/
public $warning = '';
/**
* @readonly
*
* @var bool
*/
public $isSlow = false;
/**
* @readonly
*
* @var int
*/
public $speedTrapThreshold = 0;
/**
* @readonly
*
* @var bool
*/
public $isOverAssertive = false;
/**
* @readonly
*
* @var int
*/
public $overAssertiveThreshold = 0;
/**
* @readonly
*
* @var string
*/
public $failureContent = '';
/**
* @readonly
*
* @var int
*/
public $numAssertions;
private function __construct(
string $class,
string $method,
string $description,
int $type,
string $icon,
?float $time = null,
string $warning = ''
) {
$this->class = $class;
$this->method = $method;
$this->description = $description;
$this->type = $type;
$this->icon = $icon;
$this->time = $time;
$this->warning = trim((string) preg_replace("/\r|\n/", ' ', $warning));
}
public function setFailureContent(string $content): self
{
$this->failureContent = $content;
return $this;
}
public function setNumAssertions(int $numAssertions): self
{
$this->numAssertions = $numAssertions;
return $this;
}
/**
* Creates a new test from the given test case.
*/
public static function fromTestCase(
string $class,
TestCase $testCase,
int $type,
?float $time = null,
string $warning = ''
): self {
return new self($class, $testCase->getName(), self::makeDescription($testCase), $type, self::makeIcon($type), $time, $warning);
}
public function setSpeedTrap(bool $isSlow, int $threshold): self
{
$this->isSlow = $isSlow;
$this->speedTrapThreshold = $threshold;
return $this;
}
public function setOverAssertive(bool $isOverAssertive, int $overAssertiveThreshold): self
{
$this->isOverAssertive = $isOverAssertive;
$this->overAssertiveThreshold = $overAssertiveThreshold;
return $this;
}
/**
* Get the test case description.
*/
public static function makeDescription(TestCase $testCase): string
{
$name = $testCase->getName(false);
// Convert non-breaking method name to camelCase
$name = str_replace(' ', '', $name);
// First, lets replace underscore by spaces.
$name = str_replace('_', ' ', $name);
// Then, replace upper cases by spaces.
$name = (string) preg_replace('/([A-Z])/', ' $1', $name);
// Finally, if it starts with `test`, we remove it.
$name = (string) preg_replace('/^test/', '', $name);
// Removes spaces
$name = trim($name);
// Finally, lower case everything
if (! function_exists('mb_detect_encoding') || false === $encoding = mb_detect_encoding($name, null, true)) {
$name = strtolower($name);
} else {
$name = mb_strtolower($name, $encoding);
}
// Add the dataset name if it has one
$providedData = $testCase->getProvidedData();
if (count($providedData) !== 0) {
$exporter = new Exporter();
foreach ($providedData as $key => $value) {
$providedData[$key] = $exporter->shortenedExport($value);
}
$name .= \Safe\sprintf('<fg=white> with</> [%s]', implode(', ', $providedData));
}
return $name;
}
/**
* Get the test case icon.
*/
public static function makeIcon(int $type): string
{
switch ($type) {
case BaseTestRunner::STATUS_ERROR:
case BaseTestRunner::STATUS_FAILURE:
return '✘';
case BaseTestRunner::STATUS_SKIPPED:
return '↩';
case BaseTestRunner::STATUS_RISKY:
return '☢';
case BaseTestRunner::STATUS_INCOMPLETE:
return '∅';
case BaseTestRunner::STATUS_WARNING:
return '⚠';
case BaseTestRunner::STATUS_UNKNOWN:
return '?';
case self::RUNS:
return '•';
case BaseTestRunner::STATUS_PASSED:
default:
return '✓';
}
}
}