/lib/Cake/TestSuite/Reporter/TextReporter.php

https://github.com/shama/cakephp · PHP · 189 lines · 86 code · 21 blank · 82 comment · 7 complexity · eba18390c2a3528fbae3784bee037dfe MD5 · raw file

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