CakeTextReporter.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * CakeTextReporter contains reporting features used for plain text based output
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @since CakePHP(tm) v 1.3
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. App::uses('CakeBaseReporter', 'TestSuite/Reporter');
  19. App::uses('TextCoverageReport', 'TestSuite/Coverage');
  20. /**
  21. * CakeTextReporter contains reporting features used for plain text based output
  22. *
  23. * @package Cake.TestSuite.Reporter
  24. */
  25. class CakeTextReporter extends CakeBaseReporter {
  26. /**
  27. * Sets the text/plain header if the test is not a CLI test.
  28. *
  29. * @return void
  30. */
  31. public function paintDocumentStart() {
  32. if (!headers_sent()) {
  33. header('Content-type: text/plain');
  34. }
  35. }
  36. /**
  37. * Paints a pass
  38. *
  39. * @return void
  40. */
  41. public function paintPass() {
  42. echo '.';
  43. }
  44. /**
  45. * Paints a failing test.
  46. *
  47. * @param PHPUnit_Framework_AssertionFailedError $message Failure object displayed in
  48. * the context of the other tests.
  49. * @return void
  50. */
  51. public function paintFail($message) {
  52. $context = $message->getTrace();
  53. $realContext = $context[3];
  54. $context = $context[2];
  55. printf(
  56. "FAIL on line %s\n%s in\n%s %s()\n\n",
  57. $context['line'], $message->toString(), $context['file'], $realContext['function']
  58. );
  59. }
  60. /**
  61. * Paints the end of the test with a summary of
  62. * the passes and failures.
  63. *
  64. * @param PHPUnit_Framework_TestResult $result Result object
  65. * @return void
  66. */
  67. public function paintFooter($result) {
  68. if ($result->failureCount() + $result->errorCount()) {
  69. echo "FAILURES!!!\n";
  70. } else {
  71. echo "\nOK\n";
  72. }
  73. echo "Test cases run: " . $result->count() .
  74. "/" . ($result->count() - $result->skippedCount()) .
  75. ', Passes: ' . $this->numAssertions .
  76. ', Failures: ' . $result->failureCount() .
  77. ', Exceptions: ' . $result->errorCount() . "\n";
  78. echo 'Time: ' . $result->time() . " seconds\n";
  79. echo 'Peak memory: ' . number_format(memory_get_peak_usage()) . " bytes\n";
  80. if (isset($this->params['codeCoverage']) && $this->params['codeCoverage']) {
  81. $coverage = $result->getCodeCoverage()->getSummary();
  82. echo $this->paintCoverage($coverage);
  83. }
  84. }
  85. /**
  86. * Paints the title only.
  87. *
  88. * @return void
  89. */
  90. public function paintHeader() {
  91. $this->paintDocumentStart();
  92. flush();
  93. }
  94. /**
  95. * Paints a PHP exception.
  96. *
  97. * @param Exception $exception Exception to describe.
  98. * @return void
  99. */
  100. public function paintException($exception) {
  101. $message = 'Unexpected exception of type [' . get_class($exception) .
  102. '] with message [' . $exception->getMessage() .
  103. '] in [' . $exception->getFile() .
  104. ' line ' . $exception->getLine() . ']';
  105. echo $message . "\n\n";
  106. }
  107. /**
  108. * Prints the message for skipping tests.
  109. *
  110. * @param string $message Text of skip condition.
  111. * @return void
  112. */
  113. public function paintSkip($message) {
  114. printf("Skip: %s\n", $message->getMessage());
  115. }
  116. /**
  117. * Paints formatted text such as dumped variables.
  118. *
  119. * @param string $message Text to show.
  120. * @return void
  121. */
  122. public function paintFormattedMessage($message) {
  123. echo "$message\n";
  124. flush();
  125. }
  126. /**
  127. * Generate a test case list in plain text.
  128. * Creates as series of url's for tests that can be run.
  129. * One case per line.
  130. *
  131. * @return void
  132. */
  133. public function testCaseList() {
  134. $testCases = parent::testCaseList();
  135. $app = $this->params['app'];
  136. $plugin = $this->params['plugin'];
  137. $buffer = "Core Test Cases:\n";
  138. $urlExtra = '';
  139. if ($app) {
  140. $buffer = "App Test Cases:\n";
  141. $urlExtra = '&app=true';
  142. } elseif ($plugin) {
  143. $buffer = Inflector::humanize($plugin) . " Test Cases:\n";
  144. $urlExtra = '&plugin=' . $plugin;
  145. }
  146. if (1 > count($testCases)) {
  147. $buffer .= "EMPTY";
  148. echo $buffer;
  149. }
  150. foreach ($testCases as $testCase) {
  151. $buffer .= $_SERVER['SERVER_NAME'] . $this->baseUrl() . "?case=" . $testCase . "&output=text\n";
  152. }
  153. $buffer .= "\n";
  154. echo $buffer;
  155. }
  156. /**
  157. * Generates a Text summary of the coverage data.
  158. *
  159. * @param array $coverage Array of coverage data.
  160. * @return void
  161. */
  162. public function paintCoverage($coverage) {
  163. $reporter = new TextCoverageReport($coverage, $this);
  164. echo $reporter->report();
  165. }
  166. }