Commit | Line | Data |
---|---|---|
8851bdf5 SL |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
f452d72c | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
8851bdf5 | 6 | | | |
f452d72c CW |
7 | | This work is published under the GNU AGPLv3 license with some | |
8 | | permitted exceptions and without any warranty. For full license | | |
9 | | and copyright information, see https://civicrm.org/licensing | | |
8851bdf5 SL |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | namespace Civi\Test; | |
14 | ||
15 | class TAPLegacy extends \PHPUnit\Util\Printer implements \PHPUnit\Framework\TestListener { | |
16 | ||
17 | /** | |
18 | * @var int | |
19 | */ | |
20 | protected $testNumber = 0; | |
21 | ||
22 | /** | |
23 | * @var int | |
24 | */ | |
25 | protected $testSuiteLevel = 0; | |
26 | ||
27 | /** | |
28 | * @var bool | |
29 | */ | |
30 | protected $testSuccessful = TRUE; | |
31 | ||
32 | /** | |
33 | * Constructor. | |
34 | * | |
35 | * @param mixed $out | |
36 | * | |
37 | * @throws \PHPUnit\Framework\Exception | |
38 | * | |
39 | * @since Method available since Release 3.3.4 | |
40 | */ | |
41 | public function __construct($out = NULL) { | |
42 | parent::__construct($out); | |
43 | $this | |
44 | ->write("TAP version 13\n"); | |
45 | } | |
46 | ||
47 | /** | |
48 | * An error occurred. | |
49 | * | |
50 | * @param \PHPUnit\Framework\Test $test | |
51 | * @param \Exception $e | |
52 | * @param float $time | |
53 | */ | |
54 | public function addError(\PHPUnit\Framework\Test $test, \Exception $e, $time) { | |
55 | $this | |
56 | ->writeNotOk($test, 'Error'); | |
57 | } | |
58 | ||
59 | /** | |
60 | * A failure occurred. | |
61 | * | |
62 | * @param \PHPUnit\Framework\Test $test | |
63 | * @param \PHPUnit\Framework\AssertionFailedError $e | |
64 | * @param float $time | |
65 | */ | |
66 | public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\AssertionFailedError $e, $time) { | |
67 | $this | |
68 | ->writeNotOk($test, 'Failure'); | |
69 | $message = explode("\n", \PHPUnit\Framework\TestFailure::exceptionToString($e)); | |
70 | $diagnostic = array( | |
71 | 'message' => $message[0], | |
72 | 'severity' => 'fail', | |
73 | ); | |
74 | if ($e instanceof \PHPUnit\Framework\ExpectationFailedException) { | |
75 | $cf = $e | |
76 | ->getComparisonFailure(); | |
77 | if ($cf !== NULL) { | |
78 | $diagnostic['data'] = array( | |
79 | 'got' => $cf | |
80 | ->getActual(), | |
81 | 'expected' => $cf | |
82 | ->getExpected(), | |
83 | ); | |
84 | } | |
85 | } | |
86 | ||
87 | if (function_exists('yaml_emit')) { | |
88 | $content = \yaml_emit($diagnostic, YAML_UTF8_ENCODING); | |
89 | $content = ' ' . strtr($content, ["\n" => "\n "]); | |
90 | } | |
91 | else { | |
92 | // Any valid JSON document is a valid YAML document. | |
93 | $content = json_encode($diagnostic, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); | |
94 | // For closest match, drop outermost {}'s. Realign indentation. | |
95 | $content = substr($content, 0, strrpos($content, "}")) . ' }'; | |
96 | $content = ' ' . ltrim($content); | |
97 | $content = sprintf(" ---\n%s\n ...\n", $content); | |
98 | } | |
99 | ||
100 | $this->write($content); | |
101 | } | |
102 | ||
103 | /** | |
104 | * Incomplete test. | |
105 | * | |
106 | * @param \PHPUnit\Framework\Test $test | |
107 | * @param \Exception $e | |
108 | * @param float $time | |
109 | */ | |
110 | public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Exception $e, $time) { | |
111 | $this | |
112 | ->writeNotOk($test, '', 'TODO Incomplete Test'); | |
113 | } | |
114 | ||
115 | /** | |
116 | * Risky test. | |
117 | * | |
118 | * @param \PHPUnit\Framework\Test $test | |
119 | * @param \Exception $e | |
120 | * @param float $time | |
121 | * | |
122 | * @since Method available since Release 4.0.0 | |
123 | */ | |
124 | public function addRiskyTest(\PHPUnit\Framework\Test $test, \Exception $e, $time) { | |
125 | $this | |
126 | ->write(sprintf("ok %d - # RISKY%s\n", $this->testNumber, $e | |
127 | ->getMessage() != '' ? ' ' . $e | |
128 | ->getMessage() : '')); | |
129 | $this->testSuccessful = FALSE; | |
130 | } | |
131 | ||
132 | /** | |
133 | * Skipped test. | |
134 | * | |
135 | * @param \PHPUnit\Framework\Test $test | |
136 | * @param \Exception $e | |
137 | * @param float $time | |
138 | * | |
139 | * @since Method available since Release 3.0.0 | |
140 | */ | |
141 | public function addSkippedTest(\PHPUnit\Framework\Test $test, \Exception $e, $time) { | |
142 | $this | |
143 | ->write(sprintf("ok %d - # SKIP%s\n", $this->testNumber, $e | |
144 | ->getMessage() != '' ? ' ' . $e | |
145 | ->getMessage() : '')); | |
146 | $this->testSuccessful = FALSE; | |
147 | } | |
148 | ||
149 | /** | |
150 | * Warning test. | |
151 | * | |
152 | * @param \PHPUnit\Framework\Test $test | |
153 | * @param \PHPUnit\Framework\Warning $e | |
154 | * @param float $time | |
155 | * | |
156 | * @since Method available since Release 3.0.0 | |
157 | */ | |
158 | public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, $time) { | |
159 | $this | |
160 | ->write(sprintf("ok %d - # Warning%s\n", $this->testNumber, $e | |
161 | ->getMessage() != '' ? ' ' . $e | |
162 | ->getMessage() : '')); | |
163 | $this->testSuccessful = FALSE; | |
164 | } | |
165 | ||
166 | /** | |
167 | * A testsuite started. | |
168 | * | |
169 | * @param \PHPUnit\Framework\TestSuite $suite | |
170 | */ | |
171 | public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) { | |
172 | $this->testSuiteLevel++; | |
173 | } | |
174 | ||
175 | /** | |
176 | * A testsuite ended. | |
177 | * | |
178 | * @param \PHPUnit\Framework\TestSuite $suite | |
179 | */ | |
180 | public function endTestSuite(\PHPUnit\Framework\TestSuite $suite) { | |
181 | $this->testSuiteLevel--; | |
182 | if ($this->testSuiteLevel == 0) { | |
183 | $this | |
184 | ->write(sprintf("1..%d\n", $this->testNumber)); | |
185 | } | |
186 | } | |
187 | ||
188 | /** | |
189 | * A test started. | |
190 | * | |
191 | * @param \PHPUnit\Framework\Test $test | |
192 | */ | |
193 | public function startTest(\PHPUnit\Framework\Test $test) { | |
194 | $this->testNumber++; | |
195 | $this->testSuccessful = TRUE; | |
196 | } | |
197 | ||
198 | /** | |
199 | * A test ended. | |
200 | * | |
201 | * @param \PHPUnit\Framework\Test $test | |
202 | * @param float $time | |
203 | */ | |
204 | public function endTest(\PHPUnit\Framework\Test $test, $time) { | |
205 | if ($this->testSuccessful === TRUE) { | |
206 | $this | |
207 | ->write(sprintf("ok %d - %s\n", $this->testNumber, \PHPUnit\Util\Test::describe($test))); | |
208 | } | |
209 | $this | |
210 | ->writeDiagnostics($test); | |
211 | } | |
212 | ||
213 | /** | |
214 | * @param \PHPUnit\Framework\Test $test | |
215 | * @param string $prefix | |
216 | * @param string $directive | |
217 | */ | |
218 | protected function writeNotOk(\PHPUnit\Framework\Test $test, $prefix = '', $directive = '') { | |
219 | $this | |
220 | ->write(sprintf("not ok %d - %s%s%s\n", $this->testNumber, $prefix != '' ? $prefix . ': ' : '', \PHPUnit\Util\Test::describe($test), $directive != '' ? ' # ' . $directive : '')); | |
221 | $this->testSuccessful = FALSE; | |
222 | } | |
223 | ||
224 | /** | |
225 | * @param \PHPUnit\Framework\Test $test | |
226 | */ | |
227 | private function writeDiagnostics(\PHPUnit\Framework\Test $test) { | |
228 | if (!$test instanceof \PHPUnit\Framework\TestCase) { | |
229 | return; | |
230 | } | |
231 | if (!$test | |
232 | ->hasOutput()) { | |
233 | return; | |
234 | } | |
235 | foreach (explode("\n", trim($test | |
236 | ->getActualOutput())) as $line) { | |
237 | $this | |
238 | ->write(sprintf("# %s\n", $line)); | |
239 | } | |
240 | } | |
241 | ||
242 | } |