PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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