PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

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