CakeTextReporter.php 4.5 KB

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