PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/techniconline/kmc
PHP | 454 lines | 238 code | 71 blank | 145 comment | 27 complexity | 3cfdbfb21fc8652e0ee09fee769009a9 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 = array();
  39. /**
  40. * @var int[]
  41. */
  42. protected $testSuiteTests = array(0);
  43. /**
  44. * @var int[]
  45. */
  46. protected $testSuiteAssertions = array(0);
  47. /**
  48. * @var int[]
  49. */
  50. protected $testSuiteErrors = array(0);
  51. /**
  52. * @var int[]
  53. */
  54. protected $testSuiteFailures = array(0);
  55. /**
  56. * @var int[]
  57. */
  58. protected $testSuiteTimes = array(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. if ($this->currentTestCase === null) {
  106. return;
  107. }
  108. if ($test instanceof PHPUnit_Framework_SelfDescribing) {
  109. $buffer = $test->toString() . "\n";
  110. } else {
  111. $buffer = '';
  112. }
  113. $buffer .= PHPUnit_Framework_TestFailure::exceptionToString($e) .
  114. "\n" .
  115. PHPUnit_Util_Filter::getFilteredStacktrace($e);
  116. $error = $this->document->createElement(
  117. 'error',
  118. PHPUnit_Util_XML::prepareString($buffer)
  119. );
  120. $error->setAttribute('type', get_class($e));
  121. $this->currentTestCase->appendChild($error);
  122. $this->testSuiteErrors[$this->testSuiteLevel]++;
  123. }
  124. /**
  125. * A failure occurred.
  126. *
  127. * @param PHPUnit_Framework_Test $test
  128. * @param PHPUnit_Framework_AssertionFailedError $e
  129. * @param float $time
  130. */
  131. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
  132. {
  133. if ($this->currentTestCase === null) {
  134. return;
  135. }
  136. if ($test instanceof PHPUnit_Framework_SelfDescribing) {
  137. $buffer = $test->toString() . "\n";
  138. } else {
  139. $buffer = '';
  140. }
  141. $buffer .= PHPUnit_Framework_TestFailure::exceptionToString($e) .
  142. "\n" .
  143. PHPUnit_Util_Filter::getFilteredStacktrace($e);
  144. $failure = $this->document->createElement(
  145. 'failure',
  146. PHPUnit_Util_XML::prepareString($buffer)
  147. );
  148. $failure->setAttribute('type', get_class($e));
  149. $this->currentTestCase->appendChild($failure);
  150. $this->testSuiteFailures[$this->testSuiteLevel]++;
  151. }
  152. /**
  153. * Incomplete test.
  154. *
  155. * @param PHPUnit_Framework_Test $test
  156. * @param Exception $e
  157. * @param float $time
  158. */
  159. public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  160. {
  161. if ($this->logIncompleteSkipped && $this->currentTestCase !== null) {
  162. $error = $this->document->createElement(
  163. 'error',
  164. PHPUnit_Util_XML::prepareString(
  165. "Incomplete Test\n" .
  166. PHPUnit_Util_Filter::getFilteredStacktrace($e)
  167. )
  168. );
  169. $error->setAttribute('type', get_class($e));
  170. $this->currentTestCase->appendChild($error);
  171. $this->testSuiteErrors[$this->testSuiteLevel]++;
  172. } else {
  173. $this->attachCurrentTestCase = false;
  174. }
  175. }
  176. /**
  177. * Risky test.
  178. *
  179. * @param PHPUnit_Framework_Test $test
  180. * @param Exception $e
  181. * @param float $time
  182. *
  183. * @since Method available since Release 4.0.0
  184. */
  185. public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  186. {
  187. if ($this->logIncompleteSkipped && $this->currentTestCase !== null) {
  188. $error = $this->document->createElement(
  189. 'error',
  190. PHPUnit_Util_XML::prepareString(
  191. "Risky Test\n" .
  192. PHPUnit_Util_Filter::getFilteredStacktrace($e)
  193. )
  194. );
  195. $error->setAttribute('type', get_class($e));
  196. $this->currentTestCase->appendChild($error);
  197. $this->testSuiteErrors[$this->testSuiteLevel]++;
  198. } else {
  199. $this->attachCurrentTestCase = false;
  200. }
  201. }
  202. /**
  203. * Skipped test.
  204. *
  205. * @param PHPUnit_Framework_Test $test
  206. * @param Exception $e
  207. * @param float $time
  208. *
  209. * @since Method available since Release 3.0.0
  210. */
  211. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  212. {
  213. if ($this->logIncompleteSkipped && $this->currentTestCase !== null) {
  214. $error = $this->document->createElement(
  215. 'error',
  216. PHPUnit_Util_XML::prepareString(
  217. "Skipped Test\n" .
  218. PHPUnit_Util_Filter::getFilteredStacktrace($e)
  219. )
  220. );
  221. $error->setAttribute('type', get_class($e));
  222. $this->currentTestCase->appendChild($error);
  223. $this->testSuiteErrors[$this->testSuiteLevel]++;
  224. } else {
  225. $this->attachCurrentTestCase = false;
  226. }
  227. }
  228. /**
  229. * A testsuite started.
  230. *
  231. * @param PHPUnit_Framework_TestSuite $suite
  232. *
  233. * @since Method available since Release 2.2.0
  234. */
  235. public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
  236. {
  237. $testSuite = $this->document->createElement('testsuite');
  238. $testSuite->setAttribute('name', $suite->getName());
  239. if (class_exists($suite->getName(), false)) {
  240. try {
  241. $class = new ReflectionClass($suite->getName());
  242. $testSuite->setAttribute('file', $class->getFileName());
  243. } catch (ReflectionException $e) {
  244. }
  245. }
  246. if ($this->testSuiteLevel > 0) {
  247. $this->testSuites[$this->testSuiteLevel]->appendChild($testSuite);
  248. } else {
  249. $this->root->appendChild($testSuite);
  250. }
  251. $this->testSuiteLevel++;
  252. $this->testSuites[$this->testSuiteLevel] = $testSuite;
  253. $this->testSuiteTests[$this->testSuiteLevel] = 0;
  254. $this->testSuiteAssertions[$this->testSuiteLevel] = 0;
  255. $this->testSuiteErrors[$this->testSuiteLevel] = 0;
  256. $this->testSuiteFailures[$this->testSuiteLevel] = 0;
  257. $this->testSuiteTimes[$this->testSuiteLevel] = 0;
  258. }
  259. /**
  260. * A testsuite ended.
  261. *
  262. * @param PHPUnit_Framework_TestSuite $suite
  263. *
  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. *
  360. * @since Method available since Release 2.2.0
  361. */
  362. public function getXML()
  363. {
  364. return $this->document->saveXML();
  365. }
  366. /**
  367. * Enables or disables the writing of the document
  368. * in flush().
  369. *
  370. * This is a "hack" needed for the integration of
  371. * PHPUnit with Phing.
  372. *
  373. * @return string
  374. *
  375. * @since Method available since Release 2.2.0
  376. */
  377. public function setWriteDocument($flag)
  378. {
  379. if (is_bool($flag)) {
  380. $this->writeDocument = $flag;
  381. }
  382. }
  383. }