PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/vendor/phpunit/phpunit/src/Util/Log/JUnit.php

https://gitlab.com/Georgiy.Zhegusov/museum_documents
PHP | 458 lines | 231 code | 65 blank | 162 comment | 24 complexity | 68f895f2671a66a5dac0365c2be7f981 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of PHPUnit.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * A TestListener that generates a logfile of the test execution in XML markup.
  12. *
  13. * The XML markup used is the same as the one that is used by the JUnit Ant task.
  14. *
  15. * @since Class available since Release 2.1.0
  16. */
  17. class PHPUnit_Util_Log_JUnit extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
  18. {
  19. /**
  20. * @var DOMDocument
  21. */
  22. protected $document;
  23. /**
  24. * @var DOMElement
  25. */
  26. protected $root;
  27. /**
  28. * @var bool
  29. */
  30. protected $logIncompleteSkipped = false;
  31. /**
  32. * @var bool
  33. */
  34. protected $writeDocument = true;
  35. /**
  36. * @var DOMElement[]
  37. */
  38. protected $testSuites = [];
  39. /**
  40. * @var int[]
  41. */
  42. protected $testSuiteTests = [0];
  43. /**
  44. * @var int[]
  45. */
  46. protected $testSuiteAssertions = [0];
  47. /**
  48. * @var int[]
  49. */
  50. protected $testSuiteErrors = [0];
  51. /**
  52. * @var int[]
  53. */
  54. protected $testSuiteFailures = [0];
  55. /**
  56. * @var int[]
  57. */
  58. protected $testSuiteTimes = [0];
  59. /**
  60. * @var int
  61. */
  62. protected $testSuiteLevel = 0;
  63. /**
  64. * @var DOMElement
  65. */
  66. protected $currentTestCase = null;
  67. /**
  68. * @var bool
  69. */
  70. protected $attachCurrentTestCase = true;
  71. /**
  72. * Constructor.
  73. *
  74. * @param mixed $out
  75. * @param bool $logIncompleteSkipped
  76. */
  77. public function __construct($out = null, $logIncompleteSkipped = false)
  78. {
  79. $this->document = new DOMDocument('1.0', 'UTF-8');
  80. $this->document->formatOutput = true;
  81. $this->root = $this->document->createElement('testsuites');
  82. $this->document->appendChild($this->root);
  83. parent::__construct($out);
  84. $this->logIncompleteSkipped = $logIncompleteSkipped;
  85. }
  86. /**
  87. * Flush buffer and close output.
  88. */
  89. public function flush()
  90. {
  91. if ($this->writeDocument === true) {
  92. $this->write($this->getXML());
  93. }
  94. parent::flush();
  95. }
  96. /**
  97. * An error occurred.
  98. *
  99. * @param PHPUnit_Framework_Test $test
  100. * @param Exception $e
  101. * @param float $time
  102. */
  103. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
  104. {
  105. $this->doAddFault($test, $e, $time, 'error');
  106. $this->testSuiteErrors[$this->testSuiteLevel]++;
  107. }
  108. /**
  109. * A warning occurred.
  110. *
  111. * @param PHPUnit_Framework_Test $test
  112. * @param PHPUnit_Framework_Warning $e
  113. * @param float $time
  114. *
  115. * @since Method available since Release 5.1.0
  116. */
  117. public function addWarning(PHPUnit_Framework_Test $test, PHPUnit_Framework_Warning $e, $time)
  118. {
  119. $this->doAddFault($test, $e, $time, 'warning');
  120. $this->testSuiteFailures[$this->testSuiteLevel]++;
  121. }
  122. /**
  123. * A failure occurred.
  124. *
  125. * @param PHPUnit_Framework_Test $test
  126. * @param PHPUnit_Framework_AssertionFailedError $e
  127. * @param float $time
  128. */
  129. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
  130. {
  131. $this->doAddFault($test, $e, $time, 'failure');
  132. $this->testSuiteFailures[$this->testSuiteLevel]++;
  133. }
  134. /**
  135. * Incomplete test.
  136. *
  137. * @param PHPUnit_Framework_Test $test
  138. * @param Exception $e
  139. * @param float $time
  140. */
  141. public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  142. {
  143. if ($this->logIncompleteSkipped && $this->currentTestCase !== null) {
  144. $error = $this->document->createElement(
  145. 'error',
  146. PHPUnit_Util_XML::prepareString(
  147. "Incomplete Test\n" .
  148. PHPUnit_Util_Filter::getFilteredStacktrace($e)
  149. )
  150. );
  151. $error->setAttribute('type', get_class($e));
  152. $this->currentTestCase->appendChild($error);
  153. $this->testSuiteErrors[$this->testSuiteLevel]++;
  154. } else {
  155. $this->attachCurrentTestCase = false;
  156. }
  157. }
  158. /**
  159. * Risky test.
  160. *
  161. * @param PHPUnit_Framework_Test $test
  162. * @param Exception $e
  163. * @param float $time
  164. *
  165. * @since Method available since Release 4.0.0
  166. */
  167. public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  168. {
  169. if ($this->logIncompleteSkipped && $this->currentTestCase !== null) {
  170. $error = $this->document->createElement(
  171. 'error',
  172. PHPUnit_Util_XML::prepareString(
  173. "Risky Test\n" .
  174. PHPUnit_Util_Filter::getFilteredStacktrace($e)
  175. )
  176. );
  177. $error->setAttribute('type', get_class($e));
  178. $this->currentTestCase->appendChild($error);
  179. $this->testSuiteErrors[$this->testSuiteLevel]++;
  180. } else {
  181. $this->attachCurrentTestCase = false;
  182. }
  183. }
  184. /**
  185. * Skipped test.
  186. *
  187. * @param PHPUnit_Framework_Test $test
  188. * @param Exception $e
  189. * @param float $time
  190. *
  191. * @since Method available since Release 3.0.0
  192. */
  193. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  194. {
  195. if ($this->logIncompleteSkipped && $this->currentTestCase !== null) {
  196. $error = $this->document->createElement(
  197. 'error',
  198. PHPUnit_Util_XML::prepareString(
  199. "Skipped Test\n" .
  200. PHPUnit_Util_Filter::getFilteredStacktrace($e)
  201. )
  202. );
  203. $error->setAttribute('type', get_class($e));
  204. $this->currentTestCase->appendChild($error);
  205. $this->testSuiteErrors[$this->testSuiteLevel]++;
  206. } else {
  207. $this->attachCurrentTestCase = false;
  208. }
  209. }
  210. /**
  211. * A testsuite started.
  212. *
  213. * @param PHPUnit_Framework_TestSuite $suite
  214. *
  215. * @since Method available since Release 2.2.0
  216. */
  217. public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
  218. {
  219. $testSuite = $this->document->createElement('testsuite');
  220. $testSuite->setAttribute('name', $suite->getName());
  221. if (class_exists($suite->getName(), false)) {
  222. try {
  223. $class = new ReflectionClass($suite->getName());
  224. $testSuite->setAttribute('file', $class->getFileName());
  225. } catch (ReflectionException $e) {
  226. }
  227. }
  228. if ($this->testSuiteLevel > 0) {
  229. $this->testSuites[$this->testSuiteLevel]->appendChild($testSuite);
  230. } else {
  231. $this->root->appendChild($testSuite);
  232. }
  233. $this->testSuiteLevel++;
  234. $this->testSuites[$this->testSuiteLevel] = $testSuite;
  235. $this->testSuiteTests[$this->testSuiteLevel] = 0;
  236. $this->testSuiteAssertions[$this->testSuiteLevel] = 0;
  237. $this->testSuiteErrors[$this->testSuiteLevel] = 0;
  238. $this->testSuiteFailures[$this->testSuiteLevel] = 0;
  239. $this->testSuiteTimes[$this->testSuiteLevel] = 0;
  240. }
  241. /**
  242. * A testsuite ended.
  243. *
  244. * @param PHPUnit_Framework_TestSuite $suite
  245. *
  246. * @since Method available since Release 2.2.0
  247. */
  248. public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
  249. {
  250. $this->testSuites[$this->testSuiteLevel]->setAttribute(
  251. 'tests',
  252. $this->testSuiteTests[$this->testSuiteLevel]
  253. );
  254. $this->testSuites[$this->testSuiteLevel]->setAttribute(
  255. 'assertions',
  256. $this->testSuiteAssertions[$this->testSuiteLevel]
  257. );
  258. $this->testSuites[$this->testSuiteLevel]->setAttribute(
  259. 'failures',
  260. $this->testSuiteFailures[$this->testSuiteLevel]
  261. );
  262. $this->testSuites[$this->testSuiteLevel]->setAttribute(
  263. 'errors',
  264. $this->testSuiteErrors[$this->testSuiteLevel]
  265. );
  266. $this->testSuites[$this->testSuiteLevel]->setAttribute(
  267. 'time',
  268. sprintf('%F', $this->testSuiteTimes[$this->testSuiteLevel])
  269. );
  270. if ($this->testSuiteLevel > 1) {
  271. $this->testSuiteTests[$this->testSuiteLevel - 1] += $this->testSuiteTests[$this->testSuiteLevel];
  272. $this->testSuiteAssertions[$this->testSuiteLevel - 1] += $this->testSuiteAssertions[$this->testSuiteLevel];
  273. $this->testSuiteErrors[$this->testSuiteLevel - 1] += $this->testSuiteErrors[$this->testSuiteLevel];
  274. $this->testSuiteFailures[$this->testSuiteLevel - 1] += $this->testSuiteFailures[$this->testSuiteLevel];
  275. $this->testSuiteTimes[$this->testSuiteLevel - 1] += $this->testSuiteTimes[$this->testSuiteLevel];
  276. }
  277. $this->testSuiteLevel--;
  278. }
  279. /**
  280. * A test started.
  281. *
  282. * @param PHPUnit_Framework_Test $test
  283. */
  284. public function startTest(PHPUnit_Framework_Test $test)
  285. {
  286. $testCase = $this->document->createElement('testcase');
  287. $testCase->setAttribute('name', $test->getName());
  288. if ($test instanceof PHPUnit_Framework_TestCase) {
  289. $class = new ReflectionClass($test);
  290. $methodName = $test->getName();
  291. if ($class->hasMethod($methodName)) {
  292. $method = $class->getMethod($test->getName());
  293. $testCase->setAttribute('class', $class->getName());
  294. $testCase->setAttribute('file', $class->getFileName());
  295. $testCase->setAttribute('line', $method->getStartLine());
  296. }
  297. }
  298. $this->currentTestCase = $testCase;
  299. }
  300. /**
  301. * A test ended.
  302. *
  303. * @param PHPUnit_Framework_Test $test
  304. * @param float $time
  305. */
  306. public function endTest(PHPUnit_Framework_Test $test, $time)
  307. {
  308. if ($this->attachCurrentTestCase) {
  309. if ($test instanceof PHPUnit_Framework_TestCase) {
  310. $numAssertions = $test->getNumAssertions();
  311. $this->testSuiteAssertions[$this->testSuiteLevel] += $numAssertions;
  312. $this->currentTestCase->setAttribute(
  313. 'assertions',
  314. $numAssertions
  315. );
  316. }
  317. $this->currentTestCase->setAttribute(
  318. 'time',
  319. sprintf('%F', $time)
  320. );
  321. $this->testSuites[$this->testSuiteLevel]->appendChild(
  322. $this->currentTestCase
  323. );
  324. $this->testSuiteTests[$this->testSuiteLevel]++;
  325. $this->testSuiteTimes[$this->testSuiteLevel] += $time;
  326. if (method_exists($test, 'hasOutput') && $test->hasOutput()) {
  327. $systemOut = $this->document->createElement('system-out');
  328. $systemOut->appendChild(
  329. $this->document->createTextNode($test->getActualOutput())
  330. );
  331. $this->currentTestCase->appendChild($systemOut);
  332. }
  333. }
  334. $this->attachCurrentTestCase = true;
  335. $this->currentTestCase = null;
  336. }
  337. /**
  338. * Returns the XML as a string.
  339. *
  340. * @return string
  341. *
  342. * @since Method available since Release 2.2.0
  343. */
  344. public function getXML()
  345. {
  346. return $this->document->saveXML();
  347. }
  348. /**
  349. * Enables or disables the writing of the document
  350. * in flush().
  351. *
  352. * This is a "hack" needed for the integration of
  353. * PHPUnit with Phing.
  354. *
  355. * @return string
  356. *
  357. * @since Method available since Release 2.2.0
  358. */
  359. public function setWriteDocument($flag)
  360. {
  361. if (is_bool($flag)) {
  362. $this->writeDocument = $flag;
  363. }
  364. }
  365. /**
  366. * Method which generalizes addError() and addFailure()
  367. *
  368. * @param PHPUnit_Framework_Test $test
  369. * @param Exception $e
  370. * @param float $time
  371. * @param string $type
  372. */
  373. private function doAddFault(PHPUnit_Framework_Test $test, Exception $e, $time, $type)
  374. {
  375. if ($this->currentTestCase === null) {
  376. return;
  377. }
  378. if ($test instanceof PHPUnit_Framework_SelfDescribing) {
  379. $buffer = $test->toString() . "\n";
  380. } else {
  381. $buffer = '';
  382. }
  383. $buffer .= PHPUnit_Framework_TestFailure::exceptionToString($e) .
  384. "\n" .
  385. PHPUnit_Util_Filter::getFilteredStacktrace($e);
  386. $fault = $this->document->createElement(
  387. $type,
  388. PHPUnit_Util_XML::prepareString($buffer)
  389. );
  390. $fault->setAttribute('type', get_class($e));
  391. $this->currentTestCase->appendChild($fault);
  392. }
  393. }