Commit | Line | Data |
---|---|---|
e6c546c8 SL |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 5 | | |
6 | +--------------------------------------------------------------------+ | |
7 | | This file is a part of CiviCRM. | | |
8 | | | | |
9 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
10 | | under the terms of the GNU Affero General Public License | | |
11 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
12 | | | | |
13 | | CiviCRM is distributed in the hope that it will be useful, but | | |
14 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
16 | | See the GNU Affero General Public License for more details. | | |
17 | | | | |
18 | | You should have received a copy of the GNU Affero General Public | | |
19 | | License and the CiviCRM Licensing Exception along | | |
20 | | with this program; if not, contact CiviCRM LLC | | |
21 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
22 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
23 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
24 | +--------------------------------------------------------------------+ | |
25 | */ | |
26 | ||
27 | namespace Civi\Test; | |
28 | ||
8851bdf5 SL |
29 | if (version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<')) { |
30 | class_alias('Civi\Test\TAPLegacy', 'Civi\Test\TAP'); | |
31 | } | |
32 | else { | |
33 | class TAP extends \PHPUnit\Util\Printer implements \PHPUnit\Framework\TestListener { | |
e6c546c8 | 34 | |
8851bdf5 SL |
35 | /** |
36 | * @var int | |
37 | */ | |
38 | protected $testNumber = 0; | |
e6c546c8 | 39 | |
8851bdf5 SL |
40 | /** |
41 | * @var int | |
42 | */ | |
43 | protected $testSuiteLevel = 0; | |
e6c546c8 | 44 | |
8851bdf5 SL |
45 | /** |
46 | * @var bool | |
47 | */ | |
48 | protected $testSuccessful = TRUE; | |
e6c546c8 | 49 | |
8851bdf5 SL |
50 | /** |
51 | * Constructor. | |
52 | * | |
53 | * @param mixed $out | |
54 | * | |
55 | * @throws \PHPUnit\Framework\Exception | |
56 | * | |
57 | * @since Method available since Release 3.3.4 | |
58 | */ | |
59 | public function __construct($out = NULL) { | |
60 | parent::__construct($out); | |
61 | $this | |
62 | ->write("TAP version 13\n"); | |
e6c546c8 | 63 | } |
0640a622 | 64 | |
8851bdf5 SL |
65 | /** |
66 | * An error occurred. | |
67 | * | |
68 | * @param \PHPUnit\Framework\Test $test | |
69 | * @param \Throwable $t | |
70 | * @param float $time | |
71 | */ | |
72 | public function addError(\PHPUnit\Framework\Test $test, \Throwable $t, float $time): void { | |
73 | $this | |
74 | ->writeNotOk($test, 'Error'); | |
0640a622 TO |
75 | } |
76 | ||
8851bdf5 SL |
77 | /** |
78 | * A failure occurred. | |
79 | * | |
80 | * @param \PHPUnit\Framework\Test $test | |
81 | * @param \PHPUnit\Framework\AssertionFailedError $e | |
82 | * @param float $time | |
83 | */ | |
84 | public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\AssertionFailedError $e, float $time): void { | |
85 | $this | |
86 | ->writeNotOk($test, 'Failure'); | |
87 | $message = explode("\n", \PHPUnit\Framework\TestFailure::exceptionToString($e)); | |
88 | $diagnostic = array( | |
89 | 'message' => $message[0], | |
90 | 'severity' => 'fail', | |
91 | ); | |
92 | if ($e instanceof \PHPUnit\Framework\ExpectationFailedException) { | |
93 | $cf = $e | |
94 | ->getComparisonFailure(); | |
95 | if ($cf !== NULL) { | |
96 | $diagnostic['data'] = array( | |
97 | 'got' => $cf | |
98 | ->getActual(), | |
99 | 'expected' => $cf | |
100 | ->getExpected(), | |
101 | ); | |
102 | } | |
103 | } | |
e6c546c8 | 104 | |
8851bdf5 SL |
105 | if (function_exists('yaml_emit')) { |
106 | $content = \yaml_emit($diagnostic, YAML_UTF8_ENCODING); | |
107 | $content = ' ' . strtr($content, ["\n" => "\n "]); | |
108 | } | |
109 | else { | |
110 | // Any valid JSON document is a valid YAML document. | |
111 | $content = json_encode($diagnostic, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); | |
112 | // For closest match, drop outermost {}'s. Realign indentation. | |
113 | $content = substr($content, 0, strrpos($content, "}")) . ' }'; | |
114 | $content = ' ' . ltrim($content); | |
115 | $content = sprintf(" ---\n%s\n ...\n", $content); | |
116 | } | |
e6c546c8 | 117 | |
8851bdf5 SL |
118 | $this->write($content); |
119 | } | |
e6c546c8 | 120 | |
8851bdf5 SL |
121 | /** |
122 | * Incomplete test. | |
123 | * | |
124 | * @param \PHPUnit\Framework\Test $test | |
125 | * @param \Throwable $t | |
126 | * @param float $time | |
127 | */ | |
128 | public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Throwable $t, float $time): void { | |
129 | $this | |
130 | ->writeNotOk($test, '', 'TODO Incomplete Test'); | |
131 | } | |
e6c546c8 | 132 | |
8851bdf5 SL |
133 | /** |
134 | * Risky test. | |
135 | * | |
136 | * @param \PHPUnit\Framework\Test $test | |
137 | * @param \Throwable $t | |
138 | * @param float $time | |
139 | * | |
140 | * @since Method available since Release 4.0.0 | |
141 | */ | |
142 | public function addRiskyTest(\PHPUnit\Framework\Test $test, \Throwable $t, float $time): void { | |
e6c546c8 | 143 | $this |
8851bdf5 SL |
144 | ->write(sprintf("ok %d - # RISKY%s\n", $this->testNumber, $t |
145 | ->getMessage() != '' ? ' ' . $t | |
146 | ->getMessage() : '')); | |
147 | $this->testSuccessful = FALSE; | |
e6c546c8 | 148 | } |
e6c546c8 | 149 | |
8851bdf5 SL |
150 | /** |
151 | * Skipped test. | |
152 | * | |
153 | * @param \PHPUnit\Framework\Test $test | |
154 | * @param \Throwable $t | |
155 | * @param float $time | |
156 | * | |
157 | * @since Method available since Release 3.0.0 | |
158 | */ | |
159 | public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $t, float $time): void { | |
160 | $this | |
161 | ->write(sprintf("ok %d - # SKIP%s\n", $this->testNumber, $t | |
162 | ->getMessage() != '' ? ' ' . $t | |
163 | ->getMessage() : '')); | |
164 | $this->testSuccessful = FALSE; | |
165 | } | |
e6c546c8 | 166 | |
8851bdf5 SL |
167 | /** |
168 | * Warning test. | |
169 | * | |
170 | * @param \PHPUnit\Framework\Test $test | |
171 | * @param \PHPUnit\Framework\Warning $e | |
172 | * @param float $time | |
173 | * | |
174 | * @since Method available since Release 3.0.0 | |
175 | */ | |
176 | public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, float $time): void { | |
e6c546c8 | 177 | $this |
8851bdf5 SL |
178 | ->write(sprintf("ok %d - # Warning%s\n", $this->testNumber, $e |
179 | ->getMessage() != '' ? ' ' . $e | |
180 | ->getMessage() : '')); | |
181 | $this->testSuccessful = FALSE; | |
e6c546c8 | 182 | } |
e6c546c8 | 183 | |
8851bdf5 SL |
184 | /** |
185 | * A testsuite started. | |
186 | * | |
187 | * @param \PHPUnit\Framework\TestSuite $suite | |
188 | */ | |
189 | public function startTestSuite(\PHPUnit\Framework\TestSuite $suite): void { | |
190 | $this->testSuiteLevel++; | |
191 | } | |
192 | ||
193 | /** | |
194 | * A testsuite ended. | |
195 | * | |
196 | * @param \PHPUnit\Framework\TestSuite $suite | |
197 | */ | |
198 | public function endTestSuite(\PHPUnit\Framework\TestSuite $suite): void { | |
199 | $this->testSuiteLevel--; | |
200 | if ($this->testSuiteLevel == 0) { | |
201 | $this | |
202 | ->write(sprintf("1..%d\n", $this->testNumber)); | |
203 | } | |
204 | } | |
e6c546c8 | 205 | |
8851bdf5 SL |
206 | /** |
207 | * A test started. | |
208 | * | |
209 | * @param \PHPUnit\Framework\Test $test | |
210 | */ | |
211 | public function startTest(\PHPUnit\Framework\Test $test): void { | |
212 | $this->testNumber++; | |
213 | $this->testSuccessful = TRUE; | |
e6c546c8 | 214 | } |
8851bdf5 SL |
215 | |
216 | /** | |
217 | * A test ended. | |
218 | * | |
219 | * @param \PHPUnit\Framework\Test $test | |
220 | * @param float $time | |
221 | */ | |
222 | public function endTest(\PHPUnit\Framework\Test $test, float $time): void { | |
223 | if ($this->testSuccessful === TRUE) { | |
224 | $this | |
225 | ->write(sprintf("ok %d - %s\n", $this->testNumber, \PHPUnit\Util\Test::describeAsString($test))); | |
226 | } | |
227 | $this | |
228 | ->writeDiagnostics($test); | |
e6c546c8 | 229 | } |
8851bdf5 SL |
230 | |
231 | /** | |
232 | * @param \PHPUnit\Framework\Test $test | |
233 | * @param string $prefix | |
234 | * @param string $directive | |
235 | */ | |
236 | protected function writeNotOk(\PHPUnit\Framework\Test $test, $prefix = '', $directive = ''): void { | |
e6c546c8 | 237 | $this |
8851bdf5 SL |
238 | ->write(sprintf("not ok %d - %s%s%s\n", $this->testNumber, $prefix != '' ? $prefix . ': ' : '', \PHPUnit\Util\Test::describeAsString($test), $directive != '' ? ' # ' . $directive : '')); |
239 | $this->testSuccessful = FALSE; | |
e6c546c8 | 240 | } |
e6c546c8 | 241 | |
8851bdf5 SL |
242 | /** |
243 | * @param \PHPUnit\Framework\Test $test | |
244 | */ | |
245 | private function writeDiagnostics(\PHPUnit\Framework\Test $test): void { | |
246 | if (!$test instanceof \PHPUnit\Framework\TestCase) { | |
247 | return; | |
248 | } | |
249 | if (!$test | |
250 | ->hasOutput()) { | |
251 | return; | |
252 | } | |
253 | foreach (explode("\n", trim($test | |
254 | ->getActualOutput())) as $line) { | |
255 | $this | |
256 | ->write(sprintf("# %s\n", $line)); | |
257 | } | |
258 | } | |
259 | ||
260 | } | |
e6c546c8 | 261 | } |