/vendor/phpunit/phpunit/src/Util/TestDox/ResultPrinter.php

https://bitbucket.org/alan_cordova/api-sb-map · PHP · 399 lines · 188 code · 60 blank · 151 comment · 33 complexity · d7cc558d30dfe04281221bcc630c8b7a 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. * Base class for printers of TestDox documentation.
  12. */
  13. abstract class PHPUnit_Util_TestDox_ResultPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
  14. {
  15. /**
  16. * @var PHPUnit_Util_TestDox_NamePrettifier
  17. */
  18. protected $prettifier;
  19. /**
  20. * @var string
  21. */
  22. protected $testClass = '';
  23. /**
  24. * @var int
  25. */
  26. protected $testStatus = false;
  27. /**
  28. * @var array
  29. */
  30. protected $tests = [];
  31. /**
  32. * @var int
  33. */
  34. protected $successful = 0;
  35. /**
  36. * @var int
  37. */
  38. protected $warned = 0;
  39. /**
  40. * @var int
  41. */
  42. protected $failed = 0;
  43. /**
  44. * @var int
  45. */
  46. protected $risky = 0;
  47. /**
  48. * @var int
  49. */
  50. protected $skipped = 0;
  51. /**
  52. * @var int
  53. */
  54. protected $incomplete = 0;
  55. /**
  56. * @var string
  57. */
  58. protected $currentTestClassPrettified;
  59. /**
  60. * @var string
  61. */
  62. protected $currentTestMethodPrettified;
  63. /**
  64. * @var array
  65. */
  66. private $groups;
  67. /**
  68. * @var array
  69. */
  70. private $excludeGroups;
  71. /**
  72. * @param resource $out
  73. * @param array $groups
  74. * @param array $excludeGroups
  75. */
  76. public function __construct($out = null, array $groups = [], array $excludeGroups = [])
  77. {
  78. parent::__construct($out);
  79. $this->groups = $groups;
  80. $this->excludeGroups = $excludeGroups;
  81. $this->prettifier = new PHPUnit_Util_TestDox_NamePrettifier;
  82. $this->startRun();
  83. }
  84. /**
  85. * Flush buffer and close output.
  86. */
  87. public function flush()
  88. {
  89. $this->doEndClass();
  90. $this->endRun();
  91. parent::flush();
  92. }
  93. /**
  94. * An error occurred.
  95. *
  96. * @param PHPUnit_Framework_Test $test
  97. * @param Exception $e
  98. * @param float $time
  99. */
  100. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
  101. {
  102. if (!$this->isOfInterest($test)) {
  103. return;
  104. }
  105. $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_ERROR;
  106. $this->failed++;
  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. public function addWarning(PHPUnit_Framework_Test $test, PHPUnit_Framework_Warning $e, $time)
  116. {
  117. if (!$this->isOfInterest($test)) {
  118. return;
  119. }
  120. $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_WARNING;
  121. $this->warned++;
  122. }
  123. /**
  124. * A failure occurred.
  125. *
  126. * @param PHPUnit_Framework_Test $test
  127. * @param PHPUnit_Framework_AssertionFailedError $e
  128. * @param float $time
  129. */
  130. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
  131. {
  132. if (!$this->isOfInterest($test)) {
  133. return;
  134. }
  135. $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE;
  136. $this->failed++;
  137. }
  138. /**
  139. * Incomplete test.
  140. *
  141. * @param PHPUnit_Framework_Test $test
  142. * @param Exception $e
  143. * @param float $time
  144. */
  145. public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  146. {
  147. if (!$this->isOfInterest($test)) {
  148. return;
  149. }
  150. $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE;
  151. $this->incomplete++;
  152. }
  153. /**
  154. * Risky test.
  155. *
  156. * @param PHPUnit_Framework_Test $test
  157. * @param Exception $e
  158. * @param float $time
  159. */
  160. public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  161. {
  162. if (!$this->isOfInterest($test)) {
  163. return;
  164. }
  165. $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_RISKY;
  166. $this->risky++;
  167. }
  168. /**
  169. * Skipped test.
  170. *
  171. * @param PHPUnit_Framework_Test $test
  172. * @param Exception $e
  173. * @param float $time
  174. */
  175. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  176. {
  177. if (!$this->isOfInterest($test)) {
  178. return;
  179. }
  180. $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED;
  181. $this->skipped++;
  182. }
  183. /**
  184. * A testsuite started.
  185. *
  186. * @param PHPUnit_Framework_TestSuite $suite
  187. */
  188. public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
  189. {
  190. }
  191. /**
  192. * A testsuite ended.
  193. *
  194. * @param PHPUnit_Framework_TestSuite $suite
  195. */
  196. public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
  197. {
  198. }
  199. /**
  200. * A test started.
  201. *
  202. * @param PHPUnit_Framework_Test $test
  203. */
  204. public function startTest(PHPUnit_Framework_Test $test)
  205. {
  206. if (!$this->isOfInterest($test)) {
  207. return;
  208. }
  209. $class = get_class($test);
  210. if ($this->testClass != $class) {
  211. if ($this->testClass != '') {
  212. $this->doEndClass();
  213. }
  214. $classAnnotations = PHPUnit_Util_Test::parseTestMethodAnnotations($class);
  215. if (isset($classAnnotations['class']['testdox'][0])) {
  216. $this->currentTestClassPrettified = $classAnnotations['class']['testdox'][0];
  217. } else {
  218. $this->currentTestClassPrettified = $this->prettifier->prettifyTestClass($class);
  219. }
  220. $this->startClass($class);
  221. $this->testClass = $class;
  222. $this->tests = [];
  223. }
  224. $annotations = $test->getAnnotations();
  225. if (isset($annotations['method']['testdox'][0])) {
  226. $this->currentTestMethodPrettified = $annotations['method']['testdox'][0];
  227. } else {
  228. $this->currentTestMethodPrettified = $this->prettifier->prettifyTestMethod($test->getName(false));
  229. }
  230. if ($test instanceof PHPUnit_Framework_TestCase && $test->usesDataProvider()) {
  231. $this->currentTestMethodPrettified .= ' ' . $test->dataDescription();
  232. }
  233. $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED;
  234. }
  235. /**
  236. * A test ended.
  237. *
  238. * @param PHPUnit_Framework_Test $test
  239. * @param float $time
  240. */
  241. public function endTest(PHPUnit_Framework_Test $test, $time)
  242. {
  243. if (!$this->isOfInterest($test)) {
  244. return;
  245. }
  246. if (!isset($this->tests[$this->currentTestMethodPrettified])) {
  247. if ($this->testStatus == PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
  248. $this->tests[$this->currentTestMethodPrettified]['success'] = 1;
  249. $this->tests[$this->currentTestMethodPrettified]['failure'] = 0;
  250. } else {
  251. $this->tests[$this->currentTestMethodPrettified]['success'] = 0;
  252. $this->tests[$this->currentTestMethodPrettified]['failure'] = 1;
  253. }
  254. } else {
  255. if ($this->testStatus == PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
  256. $this->tests[$this->currentTestMethodPrettified]['success']++;
  257. } else {
  258. $this->tests[$this->currentTestMethodPrettified]['failure']++;
  259. }
  260. }
  261. $this->currentTestClassPrettified = null;
  262. $this->currentTestMethodPrettified = null;
  263. }
  264. protected function doEndClass()
  265. {
  266. foreach ($this->tests as $name => $data) {
  267. $this->onTest($name, $data['failure'] == 0);
  268. }
  269. $this->endClass($this->testClass);
  270. }
  271. /**
  272. * Handler for 'start run' event.
  273. */
  274. protected function startRun()
  275. {
  276. }
  277. /**
  278. * Handler for 'start class' event.
  279. *
  280. * @param string $name
  281. */
  282. protected function startClass($name)
  283. {
  284. }
  285. /**
  286. * Handler for 'on test' event.
  287. *
  288. * @param string $name
  289. * @param bool $success
  290. */
  291. protected function onTest($name, $success = true)
  292. {
  293. }
  294. /**
  295. * Handler for 'end class' event.
  296. *
  297. * @param string $name
  298. */
  299. protected function endClass($name)
  300. {
  301. }
  302. /**
  303. * Handler for 'end run' event.
  304. */
  305. protected function endRun()
  306. {
  307. }
  308. /**
  309. * @param PHPUnit_Framework_Test $test
  310. *
  311. * @return bool
  312. */
  313. private function isOfInterest(PHPUnit_Framework_Test $test)
  314. {
  315. if (!$test instanceof PHPUnit_Framework_TestCase) {
  316. return false;
  317. }
  318. if ($test instanceof PHPUnit_Framework_WarningTestCase) {
  319. return false;
  320. }
  321. if (!empty($this->groups)) {
  322. foreach ($test->getGroups() as $group) {
  323. if (in_array($group, $this->groups)) {
  324. return true;
  325. }
  326. }
  327. return false;
  328. }
  329. if (!empty($this->excludeGroups)) {
  330. foreach ($test->getGroups() as $group) {
  331. if (in_array($group, $this->excludeGroups)) {
  332. return false;
  333. }
  334. }
  335. return true;
  336. }
  337. return true;
  338. }
  339. }