PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/codeception/codeception/src/Codeception/PHPUnit/ResultPrinter/HTML.php

https://bitbucket.org/23289368/coursework2
PHP | 245 lines | 149 code | 35 blank | 61 comment | 6 complexity | 88e14f1977c6890b6db70b0762daf689 MD5 | raw file
Possible License(s): BSD-3-Clause, CC-BY-SA-3.0, Unlicense, Apache-2.0, MIT, CC-BY-4.0, JSON, BSD-2-Clause, 0BSD
  1. <?php
  2. namespace Codeception\PHPUnit\ResultPrinter;
  3. use Codeception\PHPUnit\ResultPrinter as CodeceptionResultPrinter;
  4. use Codeception\Step;
  5. use Codeception\Step\Meta;
  6. class HTML extends CodeceptionResultPrinter
  7. {
  8. /**
  9. * @var boolean
  10. */
  11. protected $printsHTML = true;
  12. /**
  13. * @var integer
  14. */
  15. protected $id = 0;
  16. /**
  17. * @var string
  18. */
  19. protected $scenarios = '';
  20. /**
  21. * @var string
  22. */
  23. protected $templatePath;
  24. /**
  25. * @var int
  26. */
  27. protected $timeTaken = 0;
  28. protected $failures = [];
  29. /**
  30. * Constructor.
  31. *
  32. * @param mixed $out
  33. * @throws InvalidArgumentException
  34. */
  35. public function __construct($out = null)
  36. {
  37. parent::__construct($out);
  38. $this->templatePath = sprintf(
  39. '%s%stemplate%s',
  40. dirname(__FILE__),
  41. DIRECTORY_SEPARATOR,
  42. DIRECTORY_SEPARATOR
  43. );
  44. }
  45. /**
  46. * Handler for 'start class' event.
  47. *
  48. * @param string $name
  49. */
  50. protected function startClass($name)
  51. {
  52. }
  53. /**
  54. * Handler for 'on test' event.
  55. *
  56. * @param string $name
  57. * @param boolean $success
  58. * @param array $steps
  59. */
  60. protected function onTest($name, $success = true, array $steps = [], $time = 0)
  61. {
  62. $this->timeTaken += $time;
  63. switch ($this->testStatus) {
  64. case \PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE:
  65. $scenarioStatus = 'scenarioFailed';
  66. break;
  67. case \PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED:
  68. $scenarioStatus = 'scenarioSkipped';
  69. break;
  70. case \PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE:
  71. $scenarioStatus = 'scenarioIncomplete';
  72. break;
  73. case \PHPUnit_Runner_BaseTestRunner::STATUS_ERROR:
  74. $scenarioStatus = 'scenarioFailed';
  75. break;
  76. default:
  77. $scenarioStatus = 'scenarioSuccess';
  78. }
  79. $stepsBuffer = '';
  80. $metaStep = null;
  81. $subStepsBuffer = '';
  82. foreach ($steps as $step) {
  83. /** @var $step Step **/
  84. if ($step->getMetaStep()) {
  85. $subStepsBuffer .= $this->renderStep($step);
  86. $metaStep = $step->getMetaStep();
  87. continue;
  88. }
  89. if ($step->getMetaStep() != $metaStep) {
  90. $stepsBuffer .= $this->renderSubsteps($metaStep, $subStepsBuffer);
  91. $subStepsBuffer = '';
  92. }
  93. $metaStep = $step->getMetaStep();
  94. $stepsBuffer .= $this->renderStep($step);
  95. }
  96. if ($subStepsBuffer and $metaStep) {
  97. $stepsBuffer .= $this->renderSubsteps($metaStep, $subStepsBuffer);
  98. }
  99. $scenarioTemplate = new \Text_Template(
  100. $this->templatePath . 'scenario.html'
  101. );
  102. $failure = '';
  103. if (isset($this->failures[$name])) {
  104. $failTemplate = new \Text_Template(
  105. $this->templatePath . 'fail.html'
  106. );
  107. $failTemplate->setVar(['fail' => nl2br($this->failures[$name])]);
  108. $failure = $failTemplate->render();
  109. }
  110. $scenarioTemplate->setVar(
  111. [
  112. 'id' => ++$this->id,
  113. 'name' => ucfirst($name),
  114. 'scenarioStatus' => $scenarioStatus,
  115. 'steps' => $stepsBuffer,
  116. 'failure' => $failure,
  117. 'time' => round($time, 2)
  118. ]
  119. );
  120. $this->scenarios .= $scenarioTemplate->render();
  121. }
  122. public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
  123. {
  124. $suiteTemplate = new \Text_Template(
  125. $this->templatePath . 'suite.html'
  126. );
  127. $suiteTemplate->setVar(['suite' => ucfirst($suite->getName())]);
  128. $this->scenarios .= $suiteTemplate->render();
  129. }
  130. /**
  131. * Handler for 'end run' event.
  132. *
  133. */
  134. protected function endRun()
  135. {
  136. $scenarioHeaderTemplate = new \Text_Template(
  137. $this->templatePath . 'scenario_header.html'
  138. );
  139. $status = !$this->failed
  140. ? '<span style="color: green">OK</span>'
  141. : '<span style="color: #e74c3c">FAILED</span>';
  142. $scenarioHeaderTemplate->setVar(
  143. [
  144. 'name' => 'Codeception Results',
  145. 'status' => $status,
  146. 'time' => round($this->timeTaken, 1)
  147. ]
  148. );
  149. $header = $scenarioHeaderTemplate->render();
  150. $scenariosTemplate = new \Text_Template(
  151. $this->templatePath . 'scenarios.html'
  152. );
  153. $scenariosTemplate->setVar(
  154. [
  155. 'header' => $header,
  156. 'scenarios' => $this->scenarios,
  157. 'successfulScenarios' => $this->successful,
  158. 'failedScenarios' => $this->failed,
  159. 'skippedScenarios' => $this->skipped,
  160. 'incompleteScenarios' => $this->incomplete
  161. ]
  162. );
  163. $this->write($scenariosTemplate->render());
  164. }
  165. /**
  166. * An error occurred.
  167. *
  168. * @param \PHPUnit_Framework_Test $test
  169. * @param \Exception $e
  170. * @param float $time
  171. */
  172. public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time)
  173. {
  174. $this->failures[$test->toString()] = $e->getMessage();
  175. parent::addError($test, $e, $time);
  176. }
  177. /**
  178. * A failure occurred.
  179. *
  180. * @param PHPUnit_Framework_Test $test
  181. * @param PHPUnit_Framework_AssertionFailedError $e
  182. * @param float $time
  183. */
  184. public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time)
  185. {
  186. $this->failures[$test->toString()] = $e->getMessage();
  187. parent::addFailure($test, $e, $time);
  188. }
  189. /**
  190. * @param $step
  191. * @return string
  192. */
  193. protected function renderStep(Step $step)
  194. {
  195. $stepTemplate = new \Text_Template($this->templatePath . 'step.html');
  196. $stepTemplate->setVar(['action' => $step->getHtml(), 'error' => $step->hasFailed() ? 'failedStep' : '']);
  197. return $stepTemplate->render();
  198. }
  199. /**
  200. * @param $metaStep
  201. * @param $substepsBuffer
  202. * @return string
  203. */
  204. protected function renderSubsteps(Meta $metaStep, $substepsBuffer)
  205. {
  206. $metaTemplate = new \Text_Template($this->templatePath . 'substeps.html');
  207. $metaTemplate->setVar(['metaStep' => $metaStep, 'steps' => $substepsBuffer, 'id' => uniqid()]);
  208. return $metaTemplate->render();
  209. }
  210. }