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