PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/zan_zan/laravel_sample
PHP | 448 lines | 238 code | 71 blank | 139 comment | 27 complexity | 8cda479c5fc0ce9ca514bf5134ca88a5 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 integer[]
  41. */
  42. protected $testSuiteTests = array(0);
  43. /**
  44. * @var integer[]
  45. */
  46. protected $testSuiteAssertions = array(0);
  47. /**
  48. * @var integer[]
  49. */
  50. protected $testSuiteErrors = array(0);
  51. /**
  52. * @var integer[]
  53. */
  54. protected $testSuiteFailures = array(0);
  55. /**
  56. * @var integer[]
  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. * @since Method available since Release 4.0.0
  183. */
  184. public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  185. {
  186. if ($this->logIncompleteSkipped && $this->currentTestCase !== null) {
  187. $error = $this->document->createElement(
  188. 'error',
  189. PHPUnit_Util_XML::prepareString(
  190. "Risky Test\n" .
  191. PHPUnit_Util_Filter::getFilteredStacktrace($e)
  192. )
  193. );
  194. $error->setAttribute('type', get_class($e));
  195. $this->currentTestCase->appendChild($error);
  196. $this->testSuiteErrors[$this->testSuiteLevel]++;
  197. } else {
  198. $this->attachCurrentTestCase = false;
  199. }
  200. }
  201. /**
  202. * Skipped test.
  203. *
  204. * @param PHPUnit_Framework_Test $test
  205. * @param Exception $e
  206. * @param float $time
  207. * @since Method available since Release 3.0.0
  208. */
  209. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  210. {
  211. if ($this->logIncompleteSkipped && $this->currentTestCase !== null) {
  212. $error = $this->document->createElement(
  213. 'error',
  214. PHPUnit_Util_XML::prepareString(
  215. "Skipped Test\n" .
  216. PHPUnit_Util_Filter::getFilteredStacktrace($e)
  217. )
  218. );
  219. $error->setAttribute('type', get_class($e));
  220. $this->currentTestCase->appendChild($error);
  221. $this->testSuiteErrors[$this->testSuiteLevel]++;
  222. } else {
  223. $this->attachCurrentTestCase = false;
  224. }
  225. }
  226. /**
  227. * A testsuite started.
  228. *
  229. * @param PHPUnit_Framework_TestSuite $suite
  230. * @since Method available since Release 2.2.0
  231. */
  232. public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
  233. {
  234. $testSuite = $this->document->createElement('testsuite');
  235. $testSuite->setAttribute('name', $suite->getName());
  236. if (class_exists($suite->getName(), false)) {
  237. try {
  238. $class = new ReflectionClass($suite->getName());
  239. $testSuite->setAttribute('file', $class->getFileName());
  240. } catch (ReflectionException $e) {
  241. }
  242. }
  243. if ($this->testSuiteLevel > 0) {
  244. $this->testSuites[$this->testSuiteLevel]->appendChild($testSuite);
  245. } else {
  246. $this->root->appendChild($testSuite);
  247. }
  248. $this->testSuiteLevel++;
  249. $this->testSuites[$this->testSuiteLevel] = $testSuite;
  250. $this->testSuiteTests[$this->testSuiteLevel] = 0;
  251. $this->testSuiteAssertions[$this->testSuiteLevel] = 0;
  252. $this->testSuiteErrors[$this->testSuiteLevel] = 0;
  253. $this->testSuiteFailures[$this->testSuiteLevel] = 0;
  254. $this->testSuiteTimes[$this->testSuiteLevel] = 0;
  255. }
  256. /**
  257. * A testsuite ended.
  258. *
  259. * @param PHPUnit_Framework_TestSuite $suite
  260. * @since Method available since Release 2.2.0
  261. */
  262. public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
  263. {
  264. $this->testSuites[$this->testSuiteLevel]->setAttribute(
  265. 'tests',
  266. $this->testSuiteTests[$this->testSuiteLevel]
  267. );
  268. $this->testSuites[$this->testSuiteLevel]->setAttribute(
  269. 'assertions',
  270. $this->testSuiteAssertions[$this->testSuiteLevel]
  271. );
  272. $this->testSuites[$this->testSuiteLevel]->setAttribute(
  273. 'failures',
  274. $this->testSuiteFailures[$this->testSuiteLevel]
  275. );
  276. $this->testSuites[$this->testSuiteLevel]->setAttribute(
  277. 'errors',
  278. $this->testSuiteErrors[$this->testSuiteLevel]
  279. );
  280. $this->testSuites[$this->testSuiteLevel]->setAttribute(
  281. 'time',
  282. sprintf('%F', $this->testSuiteTimes[$this->testSuiteLevel])
  283. );
  284. if ($this->testSuiteLevel > 1) {
  285. $this->testSuiteTests[$this->testSuiteLevel - 1] += $this->testSuiteTests[$this->testSuiteLevel];
  286. $this->testSuiteAssertions[$this->testSuiteLevel - 1] += $this->testSuiteAssertions[$this->testSuiteLevel];
  287. $this->testSuiteErrors[$this->testSuiteLevel - 1] += $this->testSuiteErrors[$this->testSuiteLevel];
  288. $this->testSuiteFailures[$this->testSuiteLevel - 1] += $this->testSuiteFailures[$this->testSuiteLevel];
  289. $this->testSuiteTimes[$this->testSuiteLevel - 1] += $this->testSuiteTimes[$this->testSuiteLevel];
  290. }
  291. $this->testSuiteLevel--;
  292. }
  293. /**
  294. * A test started.
  295. *
  296. * @param PHPUnit_Framework_Test $test
  297. */
  298. public function startTest(PHPUnit_Framework_Test $test)
  299. {
  300. $testCase = $this->document->createElement('testcase');
  301. $testCase->setAttribute('name', $test->getName());
  302. if ($test instanceof PHPUnit_Framework_TestCase) {
  303. $class = new ReflectionClass($test);
  304. $methodName = $test->getName();
  305. if ($class->hasMethod($methodName)) {
  306. $method = $class->getMethod($test->getName());
  307. $testCase->setAttribute('class', $class->getName());
  308. $testCase->setAttribute('file', $class->getFileName());
  309. $testCase->setAttribute('line', $method->getStartLine());
  310. }
  311. }
  312. $this->currentTestCase = $testCase;
  313. }
  314. /**
  315. * A test ended.
  316. *
  317. * @param PHPUnit_Framework_Test $test
  318. * @param float $time
  319. */
  320. public function endTest(PHPUnit_Framework_Test $test, $time)
  321. {
  322. if ($this->attachCurrentTestCase) {
  323. if ($test instanceof PHPUnit_Framework_TestCase) {
  324. $numAssertions = $test->getNumAssertions();
  325. $this->testSuiteAssertions[$this->testSuiteLevel] += $numAssertions;
  326. $this->currentTestCase->setAttribute(
  327. 'assertions',
  328. $numAssertions
  329. );
  330. }
  331. $this->currentTestCase->setAttribute(
  332. 'time',
  333. sprintf('%F', $time)
  334. );
  335. $this->testSuites[$this->testSuiteLevel]->appendChild(
  336. $this->currentTestCase
  337. );
  338. $this->testSuiteTests[$this->testSuiteLevel]++;
  339. $this->testSuiteTimes[$this->testSuiteLevel] += $time;
  340. if (method_exists($test, 'hasOutput') && $test->hasOutput()) {
  341. $systemOut = $this->document->createElement('system-out');
  342. $systemOut->appendChild(
  343. $this->document->createTextNode($test->getActualOutput())
  344. );
  345. $this->currentTestCase->appendChild($systemOut);
  346. }
  347. }
  348. $this->attachCurrentTestCase = true;
  349. $this->currentTestCase = null;
  350. }
  351. /**
  352. * Returns the XML as a string.
  353. *
  354. * @return string
  355. * @since Method available since Release 2.2.0
  356. */
  357. public function getXML()
  358. {
  359. return $this->document->saveXML();
  360. }
  361. /**
  362. * Enables or disables the writing of the document
  363. * in flush().
  364. *
  365. * This is a "hack" needed for the integration of
  366. * PHPUnit with Phing.
  367. *
  368. * @return string
  369. * @since Method available since Release 2.2.0
  370. */
  371. public function setWriteDocument($flag)
  372. {
  373. if (is_bool($flag)) {
  374. $this->writeDocument = $flag;
  375. }
  376. }
  377. }