|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of PHPUnit. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Sebastian Bergmann <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace EdmondsCommerce\PHPQA\PHPUnit\TestDox; |
|
12
|
|
|
|
|
13
|
|
|
final class TestResult |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var callable |
|
17
|
|
|
*/ |
|
18
|
|
|
private $colorize; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $testClass; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $testMethod; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
private $testSuccesful; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $symbol; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $additionalInformation; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var bool |
|
47
|
|
|
*/ |
|
48
|
|
|
private $additionalInformationVerbose; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var float |
|
52
|
|
|
*/ |
|
53
|
|
|
private $runtime; |
|
54
|
|
|
|
|
55
|
|
|
public function __construct(callable $colorize, string $testClass, string $testMethod) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->colorize = $colorize; |
|
58
|
|
|
$this->testClass = $testClass; |
|
59
|
|
|
$this->testMethod = $testMethod; |
|
60
|
|
|
$this->testSuccesful = true; |
|
61
|
|
|
$this->symbol = ($this->colorize)('fg-green', '✔'); |
|
62
|
|
|
$this->additionalInformation = ''; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function isTestSuccessful(): bool |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->testSuccesful; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function fail( |
|
71
|
|
|
string $symbol, |
|
72
|
|
|
string $additionalInformation, |
|
73
|
|
|
bool $additionalInformationVerbose = false |
|
74
|
|
|
): void { |
|
75
|
|
|
$this->testSuccesful = false; |
|
76
|
|
|
$this->symbol = $symbol; |
|
77
|
|
|
$this->additionalInformation .= "\n".$additionalInformation; |
|
78
|
|
|
$this->additionalInformationVerbose = $additionalInformationVerbose; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function setRuntime(float $runtime): void |
|
82
|
|
|
{ |
|
83
|
|
|
$this->runtime = $runtime; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function toString(?self $previousTestResult, $verbose = false): string |
|
87
|
|
|
{ |
|
88
|
|
|
return \sprintf( |
|
89
|
|
|
"%s%s %s %s%s\n%s", |
|
90
|
|
|
$previousTestResult && $previousTestResult->additionalInformationPrintable($verbose) ? "\n" : '', |
|
91
|
|
|
$this->getClassNameHeader($previousTestResult ? $previousTestResult->testClass : null), |
|
92
|
|
|
$this->symbol, |
|
93
|
|
|
$this->testMethod, |
|
94
|
|
|
$verbose ? ' '.$this->getFormattedRuntime() : '', |
|
95
|
|
|
$this->getFormattedAdditionalInformation($verbose) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function getClassNameHeader(?string $previousTestClass): string |
|
100
|
|
|
{ |
|
101
|
|
|
$className = ''; |
|
102
|
|
|
|
|
103
|
|
|
if ($this->testClass !== $previousTestClass) { |
|
104
|
|
|
if (null !== $previousTestClass) { |
|
105
|
|
|
$className = "\n"; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$className .= \sprintf("%s\n", $this->testClass); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $className; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function getFormattedRuntime(): string |
|
115
|
|
|
{ |
|
116
|
|
|
if ($this->runtime > 5) { |
|
117
|
|
|
return ($this->colorize)('fg-red', \sprintf('[%.2f ms]', $this->runtime * 1000)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if ($this->runtime > 1) { |
|
121
|
|
|
return ($this->colorize)('fg-yellow', \sprintf('[%.2f ms]', $this->runtime * 1000)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return \sprintf('[%.2f ms]', $this->runtime * 1000); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
private function getFormattedAdditionalInformation($verbose): string |
|
128
|
|
|
{ |
|
129
|
|
|
if (!$this->additionalInformationPrintable($verbose)) { |
|
130
|
|
|
return ''; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return \sprintf( |
|
134
|
|
|
" │\n%s\n", |
|
135
|
|
|
\implode( |
|
136
|
|
|
"\n", |
|
137
|
|
|
\array_map( |
|
138
|
|
|
function (string $text) { |
|
139
|
|
|
return \sprintf(' │ %s', $text); |
|
140
|
|
|
}, |
|
141
|
|
|
\explode("\n", $this->additionalInformation) |
|
142
|
|
|
) |
|
143
|
|
|
) |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
private function additionalInformationPrintable(bool $verbose): bool |
|
148
|
|
|
{ |
|
149
|
|
|
if ($this->additionalInformation === '') { |
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
if ($this->additionalInformationVerbose && !$verbose) { |
|
154
|
|
|
return false; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return true; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|