4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 |
10 +--------------------------------------------------------------------+
15 if (version_compare(\PHPUnit\Runner\Version
::id(), '7.0.0', '<')) {
16 class_alias('Civi\Test\TAPLegacy', 'Civi\Test\TAP');
19 class TAP
extends \PHPUnit\Util\Printer
implements \PHPUnit\Framework\TestListener
{
24 protected $testNumber = 0;
29 protected $testSuiteLevel = 0;
34 protected $testSuccessful = TRUE;
41 * @throws \PHPUnit\Framework\Exception
43 * @since Method available since Release 3.3.4
45 public function __construct($out = NULL) {
46 parent
::__construct($out);
48 ->write("TAP version 13\n");
54 * @param \PHPUnit\Framework\Test $test
55 * @param \Throwable $t
58 public function addError(\PHPUnit\Framework\Test
$test, \Throwable
$t, float $time): void
{
60 ->writeNotOk($test, 'Error');
66 * @param \PHPUnit\Framework\Test $test
67 * @param \PHPUnit\Framework\AssertionFailedError $e
70 public function addFailure(\PHPUnit\Framework\Test
$test, \PHPUnit\Framework\AssertionFailedError
$e, float $time): void
{
72 ->writeNotOk($test, 'Failure');
73 $message = explode("\n", \PHPUnit\Framework\TestFailure
::exceptionToString($e));
75 'message' => $message[0],
78 if ($e instanceof \PHPUnit\Framework\ExpectationFailedException
) {
80 ->getComparisonFailure();
82 $diagnostic['data'] = array(
91 if (function_exists('yaml_emit')) {
92 $content = \
yaml_emit($diagnostic, YAML_UTF8_ENCODING
);
93 $content = ' ' . strtr($content, ["\n" => "\n "]);
96 // Any valid JSON document is a valid YAML document.
97 $content = json_encode($diagnostic, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
);
98 // For closest match, drop outermost {}'s. Realign indentation.
99 $content = substr($content, 0, strrpos($content, "}")) . ' }';
100 $content = ' ' . ltrim($content);
101 $content = sprintf(" ---\n%s\n ...\n", $content);
104 $this->write($content);
110 * @param \PHPUnit\Framework\Test $test
111 * @param \Throwable $t
114 public function addIncompleteTest(\PHPUnit\Framework\Test
$test, \Throwable
$t, float $time): void
{
116 ->writeNotOk($test, '', 'TODO Incomplete Test');
122 * @param \PHPUnit\Framework\Test $test
123 * @param \Throwable $t
126 * @since Method available since Release 4.0.0
128 public function addRiskyTest(\PHPUnit\Framework\Test
$test, \Throwable
$t, float $time): void
{
130 ->write(sprintf("ok %d - # RISKY%s\n", $this->testNumber
, $t
131 ->getMessage() != '' ?
' ' . $t
132 ->getMessage() : ''));
133 $this->testSuccessful
= FALSE;
139 * @param \PHPUnit\Framework\Test $test
140 * @param \Throwable $t
143 * @since Method available since Release 3.0.0
145 public function addSkippedTest(\PHPUnit\Framework\Test
$test, \Throwable
$t, float $time): void
{
147 ->write(sprintf("ok %d - # SKIP%s\n", $this->testNumber
, $t
148 ->getMessage() != '' ?
' ' . $t
149 ->getMessage() : ''));
150 $this->testSuccessful
= FALSE;
156 * @param \PHPUnit\Framework\Test $test
157 * @param \PHPUnit\Framework\Warning $e
160 * @since Method available since Release 3.0.0
162 public function addWarning(\PHPUnit\Framework\Test
$test, \PHPUnit\Framework\Warning
$e, float $time): void
{
164 ->write(sprintf("ok %d - # Warning%s\n", $this->testNumber
, $e
165 ->getMessage() != '' ?
' ' . $e
166 ->getMessage() : ''));
167 $this->testSuccessful
= FALSE;
171 * A testsuite started.
173 * @param \PHPUnit\Framework\TestSuite $suite
175 public function startTestSuite(\PHPUnit\Framework\TestSuite
$suite): void
{
176 $this->testSuiteLevel++
;
182 * @param \PHPUnit\Framework\TestSuite $suite
184 public function endTestSuite(\PHPUnit\Framework\TestSuite
$suite): void
{
185 $this->testSuiteLevel
--;
186 if ($this->testSuiteLevel
== 0) {
188 ->write(sprintf("1..%d\n", $this->testNumber
));
195 * @param \PHPUnit\Framework\Test $test
197 public function startTest(\PHPUnit\Framework\Test
$test): void
{
199 $this->testSuccessful
= TRUE;
205 * @param \PHPUnit\Framework\Test $test
208 public function endTest(\PHPUnit\Framework\Test
$test, float $time): void
{
209 if ($this->testSuccessful
=== TRUE) {
211 ->write(sprintf("ok %d - %s\n", $this->testNumber
, \PHPUnit\Util\Test
::describeAsString($test)));
214 ->writeDiagnostics($test);
218 * @param \PHPUnit\Framework\Test $test
219 * @param string $prefix
220 * @param string $directive
222 protected function writeNotOk(\PHPUnit\Framework\Test
$test, $prefix = '', $directive = ''): void
{
224 ->write(sprintf("not ok %d - %s%s%s\n", $this->testNumber
, $prefix != '' ?
$prefix . ': ' : '', \PHPUnit\Util\Test
::describeAsString($test), $directive != '' ?
' # ' . $directive : ''));
225 $this->testSuccessful
= FALSE;
229 * @param \PHPUnit\Framework\Test $test
231 private function writeDiagnostics(\PHPUnit\Framework\Test
$test): void
{
232 if (!$test instanceof \PHPUnit\Framework\TestCase
) {
239 foreach (explode("\n", trim($test
240 ->getActualOutput())) as $line) {
242 ->write(sprintf("# %s\n", $line));