Civi/Test/TAP - Replace Symfony YAML dependency
[civicrm-core.git] / Civi / Test / TAP.php
CommitLineData
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
27namespace Civi\Test;
28
29class 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 }
0640a622
TO
100
101 if (function_exists('yaml_emit')) {
102 $content = \yaml_emit($diagnostic, YAML_UTF8_ENCODING);
103 $content = ' ' . strtr($content, ["\n" => "\n "]);
104 }
105 else {
106 // Any valid JSON document is a valid YAML document.
107 $content = json_encode($diagnostic, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
108 // For closest match, drop outermost {}'s. Realign indentation.
109 $content = substr($content, 0, strrpos($content, "}")) . ' }';
110 $content = ' ' . ltrim($content);
111 $content = sprintf(" ---\n%s\n ...\n", $content);
112 }
113
114 $this->write($content);
e6c546c8
SL
115 }
116
117 /**
118 * Incomplete test.
119 *
120 * @param \PHPUnit\Framework\Test $test
121 * @param \Exception $e
122 * @param float $time
123 */
124 public function addIncompleteTest(\PHPUnit\Framework\Test $test, \Exception $e, $time) {
125 $this
126 ->writeNotOk($test, '', 'TODO Incomplete Test');
127 }
128
129 /**
130 * Risky test.
131 *
132 * @param \PHPUnit\Framework\Test $test
133 * @param \Exception $e
134 * @param float $time
135 *
136 * @since Method available since Release 4.0.0
137 */
138 public function addRiskyTest(\PHPUnit\Framework\Test $test, \Exception $e, $time) {
139 $this
140 ->write(sprintf("ok %d - # RISKY%s\n", $this->testNumber, $e
141 ->getMessage() != '' ? ' ' . $e
142 ->getMessage() : ''));
143 $this->testSuccessful = FALSE;
144 }
145
146 /**
147 * Skipped test.
148 *
149 * @param \PHPUnit\Framework\Test $test
150 * @param \Exception $e
151 * @param float $time
152 *
153 * @since Method available since Release 3.0.0
154 */
155 public function addSkippedTest(\PHPUnit\Framework\Test $test, \Exception $e, $time) {
156 $this
157 ->write(sprintf("ok %d - # SKIP%s\n", $this->testNumber, $e
158 ->getMessage() != '' ? ' ' . $e
159 ->getMessage() : ''));
160 $this->testSuccessful = FALSE;
161 }
162
163 /**
164 * Warning test.
165 *
166 * @param \PHPUnit\Framework\Test $test
167 * @param \PHPUnit\Framework\Warning $e
168 * @param float $time
169 *
170 * @since Method available since Release 3.0.0
171 */
172 public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, $time) {
173 $this
174 ->write(sprintf("ok %d - # Warning%s\n", $this->testNumber, $e
175 ->getMessage() != '' ? ' ' . $e
176 ->getMessage() : ''));
177 $this->testSuccessful = FALSE;
178 }
179
180 /**
181 * A testsuite started.
182 *
183 * @param \PHPUnit\Framework\TestSuite $suite
184 */
185 public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) {
186 $this->testSuiteLevel++;
187 }
188
189 /**
190 * A testsuite ended.
191 *
192 * @param \PHPUnit\Framework\TestSuite $suite
193 */
194 public function endTestSuite(\PHPUnit\Framework\TestSuite $suite) {
195 $this->testSuiteLevel--;
196 if ($this->testSuiteLevel == 0) {
197 $this
198 ->write(sprintf("1..%d\n", $this->testNumber));
199 }
200 }
201
202 /**
203 * A test started.
204 *
205 * @param \PHPUnit\Framework\Test $test
206 */
207 public function startTest(\PHPUnit\Framework\Test $test) {
208 $this->testNumber++;
209 $this->testSuccessful = TRUE;
210 }
211
212 /**
213 * A test ended.
214 *
215 * @param \PHPUnit\Framework\Test $test
216 * @param float $time
217 */
218 public function endTest(\PHPUnit\Framework\Test $test, $time) {
219 if ($this->testSuccessful === TRUE) {
220 $this
221 ->write(sprintf("ok %d - %s\n", $this->testNumber, \PHPUnit\Util\Test::describe($test)));
222 }
223 $this
224 ->writeDiagnostics($test);
225 }
226
227 /**
228 * @param \PHPUnit\Framework\Test $test
229 * @param string $prefix
230 * @param string $directive
231 */
232 protected function writeNotOk(\PHPUnit\Framework\Test $test, $prefix = '', $directive = '') {
233 $this
234 ->write(sprintf("not ok %d - %s%s%s\n", $this->testNumber, $prefix != '' ? $prefix . ': ' : '', \PHPUnit\Util\Test::describe($test), $directive != '' ? ' # ' . $directive : ''));
235 $this->testSuccessful = FALSE;
236 }
237
238 /**
239 * @param \PHPUnit\Framework\Test $test
240 */
241 private function writeDiagnostics(\PHPUnit\Framework\Test $test) {
242 if (!$test instanceof \PHPUnit\Framework\TestCase) {
243 return;
244 }
245 if (!$test
246 ->hasOutput()) {
247 return;
248 }
249 foreach (explode("\n", trim($test
250 ->getActualOutput())) as $line) {
251 $this
252 ->write(sprintf("# %s\n", $line));
253 }
254 }
255
256}