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 | ||
29 | class TAP extends \PHPUnit\Util\Printer implements \PHPUnit\Framework\TestListener { | |
30 | ||
31 | /** | |
32 | * @var int | |
33 | */ | |
34 | protected $testNumber = 0; | |
35 | ||
36 | /** | |
37 | * @var int | |
38 | */ | |
39 | protected $testSuiteLevel = 0; | |
40 | ||
41 | /** | |
42 | * @var bool | |
43 | */ | |
44 | protected $testSuccessful = TRUE; | |
45 | ||
46 | /** | |
47 | * Constructor. | |
48 | * | |
49 | * @param mixed $out | |
50 | * | |
51 | * @throws \PHPUnit\Framework\Exception | |
52 | * | |
53 | * @since Method available since Release 3.3.4 | |
54 | */ | |
55 | public function __construct($out = NULL) { | |
56 | parent::__construct($out); | |
57 | $this | |
58 | ->write("TAP version 13\n"); | |
59 | } | |
60 | ||
61 | /** | |
62 | * An error occurred. | |
63 | * | |
64 | * @param \PHPUnit\Framework\Test $test | |
65 | * @param \Exception $e | |
66 | * @param float $time | |
67 | */ | |
68 | public function addError(\PHPUnit\Framework\Test $test, \Exception $e, $time) { | |
69 | $this | |
70 | ->writeNotOk($test, 'Error'); | |
71 | } | |
72 | ||
73 | /** | |
74 | * A failure occurred. | |
75 | * | |
76 | * @param \PHPUnit\Framework\Test $test | |
77 | * @param \PHPUnit\Framework\AssertionFailedError $e | |
78 | * @param float $time | |
79 | */ | |
80 | public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\AssertionFailedError $e, $time) { | |
81 | $this | |
82 | ->writeNotOk($test, 'Failure'); | |
83 | $message = explode("\n", \PHPUnit\Framework\TestFailure::exceptionToString($e)); | |
84 | $diagnostic = array( | |
85 | 'message' => $message[0], | |
86 | 'severity' => 'fail', | |
87 | ); | |
88 | if ($e instanceof \PHPUnit\Framework\ExpectationFailedException) { | |
89 | $cf = $e | |
90 | ->getComparisonFailure(); | |
91 | if ($cf !== NULL) { | |
92 | $diagnostic['data'] = array( | |
93 | 'got' => $cf | |
94 | ->getActual(), | |
95 | 'expected' => $cf | |
96 | ->getExpected(), | |
97 | ); | |
98 | } | |
99 | } | |
100 | $yaml = new \Symfony\Component\Yaml\Dumper(); | |
101 | $this | |
102 | ->write(sprintf(" ---\n%s ...\n", $yaml | |
103 | ->dump($diagnostic, 2, 2))); | |
104 | } | |
105 | ||
106 | /** | |
107 | * Incomplete test. | |
108 | * | |
109 | * @param \PHPUnit\Framework\Test $test | |
110 | * @param \Exception $e | |
111 | * @param float $time | |
112 | */ | |
113 | public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Exception $e, $time) { | |
114 | $this | |
115 | ->writeNotOk($test, '', 'TODO Incomplete Test'); | |
116 | } | |
117 | ||
118 | /** | |
119 | * Risky test. | |
120 | * | |
121 | * @param \PHPUnit\Framework\Test $test | |
122 | * @param \Exception $e | |
123 | * @param float $time | |
124 | * | |
125 | * @since Method available since Release 4.0.0 | |
126 | */ | |
127 | public function addRiskyTest(\PHPUnit\Framework\Test $test, \Exception $e, $time) { | |
128 | $this | |
129 | ->write(sprintf("ok %d - # RISKY%s\n", $this->testNumber, $e | |
130 | ->getMessage() != '' ? ' ' . $e | |
131 | ->getMessage() : '')); | |
132 | $this->testSuccessful = FALSE; | |
133 | } | |
134 | ||
135 | /** | |
136 | * Skipped test. | |
137 | * | |
138 | * @param \PHPUnit\Framework\Test $test | |
139 | * @param \Exception $e | |
140 | * @param float $time | |
141 | * | |
142 | * @since Method available since Release 3.0.0 | |
143 | */ | |
144 | public function addSkippedTest(\PHPUnit\Framework\Test $test, \Exception $e, $time) { | |
145 | $this | |
146 | ->write(sprintf("ok %d - # SKIP%s\n", $this->testNumber, $e | |
147 | ->getMessage() != '' ? ' ' . $e | |
148 | ->getMessage() : '')); | |
149 | $this->testSuccessful = FALSE; | |
150 | } | |
151 | ||
152 | /** | |
153 | * Warning test. | |
154 | * | |
155 | * @param \PHPUnit\Framework\Test $test | |
156 | * @param \PHPUnit\Framework\Warning $e | |
157 | * @param float $time | |
158 | * | |
159 | * @since Method available since Release 3.0.0 | |
160 | */ | |
161 | public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, $time) { | |
162 | $this | |
163 | ->write(sprintf("ok %d - # Warning%s\n", $this->testNumber, $e | |
164 | ->getMessage() != '' ? ' ' . $e | |
165 | ->getMessage() : '')); | |
166 | $this->testSuccessful = FALSE; | |
167 | } | |
168 | ||
169 | /** | |
170 | * A testsuite started. | |
171 | * | |
172 | * @param \PHPUnit\Framework\TestSuite $suite | |
173 | */ | |
174 | public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) { | |
175 | $this->testSuiteLevel++; | |
176 | } | |
177 | ||
178 | /** | |
179 | * A testsuite ended. | |
180 | * | |
181 | * @param \PHPUnit\Framework\TestSuite $suite | |
182 | */ | |
183 | public function endTestSuite(\PHPUnit\Framework\TestSuite $suite) { | |
184 | $this->testSuiteLevel--; | |
185 | if ($this->testSuiteLevel == 0) { | |
186 | $this | |
187 | ->write(sprintf("1..%d\n", $this->testNumber)); | |
188 | } | |
189 | } | |
190 | ||
191 | /** | |
192 | * A test started. | |
193 | * | |
194 | * @param \PHPUnit\Framework\Test $test | |
195 | */ | |
196 | public function startTest(\PHPUnit\Framework\Test $test) { | |
197 | $this->testNumber++; | |
198 | $this->testSuccessful = TRUE; | |
199 | } | |
200 | ||
201 | /** | |
202 | * A test ended. | |
203 | * | |
204 | * @param \PHPUnit\Framework\Test $test | |
205 | * @param float $time | |
206 | */ | |
207 | public function endTest(\PHPUnit\Framework\Test $test, $time) { | |
208 | if ($this->testSuccessful === TRUE) { | |
209 | $this | |
210 | ->write(sprintf("ok %d - %s\n", $this->testNumber, \PHPUnit\Util\Test::describe($test))); | |
211 | } | |
212 | $this | |
213 | ->writeDiagnostics($test); | |
214 | } | |
215 | ||
216 | /** | |
217 | * @param \PHPUnit\Framework\Test $test | |
218 | * @param string $prefix | |
219 | * @param string $directive | |
220 | */ | |
221 | protected function writeNotOk(\PHPUnit\Framework\Test $test, $prefix = '', $directive = '') { | |
222 | $this | |
223 | ->write(sprintf("not ok %d - %s%s%s\n", $this->testNumber, $prefix != '' ? $prefix . ': ' : '', \PHPUnit\Util\Test::describe($test), $directive != '' ? ' # ' . $directive : '')); | |
224 | $this->testSuccessful = FALSE; | |
225 | } | |
226 | ||
227 | /** | |
228 | * @param \PHPUnit\Framework\Test $test | |
229 | */ | |
230 | private function writeDiagnostics(\PHPUnit\Framework\Test $test) { | |
231 | if (!$test instanceof \PHPUnit\Framework\TestCase) { | |
232 | return; | |
233 | } | |
234 | if (!$test | |
235 | ->hasOutput()) { | |
236 | return; | |
237 | } | |
238 | foreach (explode("\n", trim($test | |
239 | ->getActualOutput())) as $line) { | |
240 | $this | |
241 | ->write(sprintf("# %s\n", $line)); | |
242 | } | |
243 | } | |
244 | ||
245 | } |