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