PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/Civi/Test/TAP.php

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