PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/PHPUnit/Listener/HtmlResultPrinter.php

https://github.com/andreaswolf/Menta
PHP | 148 lines | 98 code | 25 blank | 25 comment | 11 complexity | f4ef830a465a1014a02e9076050281bc MD5 | raw file
  1. <?php
  2. /**
  3. * HTML result printer
  4. *
  5. * @author Fabrizio Branca
  6. * @since 2011-11-13
  7. */
  8. class Menta_PHPUnit_Listener_HtmlResultPrinter extends Menta_PHPUnit_Listener_AbstractTemplatablePrinter implements PHPUnit_Framework_TestListener {
  9. /**
  10. * @var string
  11. */
  12. protected $templateFile = '###MENTA_ROOTDIR###/PHPUnit/Listener/Resources/Templates/HtmlResultTemplate.php';
  13. /**
  14. * @var array
  15. */
  16. protected $additionalFiles = array();
  17. protected $lastResult;
  18. protected $lastStatus;
  19. protected $level = 0;
  20. protected $suiteStack = array();
  21. protected $results = array();
  22. protected $count = array();
  23. protected $viewClass = 'Menta_PHPUnit_Listener_Resources_HtmlResultView';
  24. public function startTest(PHPUnit_Framework_Test $test) {}
  25. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
  26. $this->lastResult = $e;
  27. $this->lastStatus = PHPUnit_Runner_BaseTestRunner::STATUS_ERROR;
  28. }
  29. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {
  30. $this->lastResult = $e;
  31. $this->lastStatus = PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE;
  32. }
  33. public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
  34. $this->lastResult = $e;
  35. $this->lastStatus = PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE;
  36. }
  37. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
  38. $this->lastResult = $e;
  39. $this->lastStatus = PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED;
  40. }
  41. public function endTest(PHPUnit_Framework_Test $test, $time) {
  42. $testName = PHPUnit_Util_Test::describe($test);
  43. // store in result array
  44. $currentArray =& $this->results;
  45. foreach ($this->suiteStack as $suiteName) {
  46. $colonPos = strpos($suiteName, ': ');
  47. if ($colonPos !== false) {
  48. $browser = substr($suiteName, $colonPos+2);
  49. $suiteName = substr($suiteName, 0, $colonPos);
  50. // $currentArray =& $currentArray['__suites'][$suiteName]['__browsers'][$browser];
  51. $currentArray =& $currentArray['__browsers'][$browser];
  52. } else {
  53. $currentArray =& $currentArray['__suites'][$suiteName];
  54. }
  55. }
  56. if (is_null($this->lastStatus)) {
  57. $this->lastStatus = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED;
  58. }
  59. $result = array(
  60. 'testName' => $testName,
  61. 'time' => $time,
  62. 'exception' => $this->lastResult,
  63. 'status' => $this->lastStatus
  64. );
  65. if ($test instanceof Menta_Interface_ScreenshotTestcase) { /* @var $test Menta_Interface_ScreenshotTestcase */
  66. $screenshots = $test->getScreenshots();
  67. if (is_array($screenshots) && count($screenshots) > 0) {
  68. $result['screenshots'] = $screenshots;
  69. }
  70. }
  71. if (isset($this->count[$this->lastStatus])) {
  72. $this->count[$this->lastStatus]++;
  73. } else {
  74. $this->count[$this->lastStatus] = 1;
  75. }
  76. $dataSetPos = strpos($testName, ' with data set ');
  77. if ($dataSetPos !== false) {
  78. $dataSet = substr($testName, $dataSetPos+5);
  79. $dataSet = ucfirst(trim($dataSet));
  80. $testName = substr($testName, 0, $dataSetPos);
  81. $currentArray['__tests']['__datasets'][$dataSet] = $result;
  82. } else {
  83. $currentArray['__tests'][$testName] = $result;
  84. }
  85. $this->lastResult = NULL;
  86. $this->lastStatus = NULL;
  87. }
  88. public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {
  89. $this->level++;
  90. $name = PHPUnit_Util_Test::describe($suite);
  91. if (empty($name)) {
  92. //$name = get_class($suite);
  93. $name = '-';
  94. }
  95. $this->suiteStack[] = $name;
  96. }
  97. public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {
  98. $this->level--;
  99. array_pop($this->suiteStack);
  100. }
  101. /**
  102. * Flush: Copy images and additional files to folder and generate index file using a template
  103. *
  104. * This method is called once after all tests have been processed.
  105. * HINT: The flush method is only called if the TestListener inherits from PHPUnit_Util_Printer
  106. *
  107. * @param array $templateVars
  108. * @return void
  109. * @author Fabrizio Branca
  110. */
  111. public function flush(array $templateVars=array()) {
  112. ksort($this->count);
  113. $sum = array_sum($this->count);
  114. $templateVars['percentages'] = array();
  115. foreach($this->count as $key => $value) {
  116. $templateVars['percentages'][$key] = 100 * $value/$sum;
  117. }
  118. $templateVars['basedir'] = dirname($this->targetFile);
  119. $templateVars['results'] = $this->results;
  120. $templateVars['count'] = $this->count;
  121. return parent::flush($templateVars);
  122. }
  123. }