1: <?php
2: /**
3: * CakeBaseReporter contains common functionality to all cake test suite reporters.
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:
18: if (!class_exists('PHPUnit_TextUI_ResultPrinter')) {
19: require_once 'PHPUnit/TextUI/ResultPrinter.php';
20: }
21:
22: /**
23: * CakeBaseReporter contains common reporting features used in the CakePHP Test suite
24: *
25: * @package Cake.TestSuite.Reporter
26: */
27: class CakeBaseReporter extends PHPUnit_TextUI_ResultPrinter {
28:
29: /**
30: * Headers sent
31: *
32: * @var bool
33: */
34: protected $_headerSent = false;
35:
36: /**
37: * Array of request parameters. Usually parsed GET params.
38: *
39: * @var array
40: */
41: public $params = array();
42:
43: /**
44: * Character set for the output of test reporting.
45: *
46: * @var string
47: */
48: protected $_characterSet;
49:
50: /**
51: * Does nothing yet. The first output will
52: * be sent on the first test start.
53: *
54: * ### Params
55: *
56: * - show_passes - Should passes be shown
57: * - plugin - Plugin test being run?
58: * - core - Core test being run.
59: * - case - The case being run
60: * - codeCoverage - Whether the case/group being run is being code covered.
61: *
62: * @param string $charset The character set to output with. Defaults to UTF-8
63: * @param array $params Array of request parameters the reporter should use. See above.
64: */
65: public function __construct($charset = 'utf-8', $params = array()) {
66: if (!$charset) {
67: $charset = 'utf-8';
68: }
69: $this->_characterSet = $charset;
70: $this->params = $params;
71: }
72:
73: /**
74: * Retrieves a list of test cases from the active Manager class,
75: * displaying it in the correct format for the reporter subclass
76: *
77: * @return mixed
78: */
79: public function testCaseList() {
80: $testList = CakeTestLoader::generateTestList($this->params);
81: return $testList;
82: }
83:
84: /**
85: * Paints the start of the response from the test suite.
86: * Used to paint things like head elements in an html page.
87: *
88: * @return void
89: */
90: public function paintDocumentStart() {
91: }
92:
93: /**
94: * Paints the end of the response from the test suite.
95: * Used to paint things like </body> in an html page.
96: *
97: * @return void
98: */
99: public function paintDocumentEnd() {
100: }
101:
102: /**
103: * Paint a list of test sets, core, app, and plugin test sets
104: * available.
105: *
106: * @return void
107: */
108: public function paintTestMenu() {
109: }
110:
111: /**
112: * Get the baseUrl if one is available.
113: *
114: * @return string The base URL for the request.
115: */
116: public function baseUrl() {
117: if (!empty($_SERVER['PHP_SELF'])) {
118: return $_SERVER['PHP_SELF'];
119: }
120: return '';
121: }
122:
123: /**
124: * Print result
125: *
126: * @param PHPUnit_Framework_TestResult $result The result object
127: * @return void
128: */
129: public function printResult(PHPUnit_Framework_TestResult $result) {
130: $this->paintFooter($result);
131: }
132:
133: /**
134: * Paint result
135: *
136: * @param PHPUnit_Framework_TestResult $result The result object
137: * @return void
138: */
139: public function paintResult(PHPUnit_Framework_TestResult $result) {
140: $this->paintFooter($result);
141: }
142:
143: /**
144: * An error occurred.
145: *
146: * @param PHPUnit_Framework_Test $test The test to add an error for.
147: * @param Exception $e The exception object to add.
148: * @param float $time The current time.
149: * @return void
150: */
151: public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
152: $this->paintException($e, $test);
153: }
154:
155: /**
156: * A failure occurred.
157: *
158: * @param PHPUnit_Framework_Test $test The test that failed
159: * @param PHPUnit_Framework_AssertionFailedError $e The assertion that failed.
160: * @param float $time The current time.
161: * @return void
162: */
163: public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {
164: $this->paintFail($e, $test);
165: }
166:
167: /**
168: * Incomplete test.
169: *
170: * @param PHPUnit_Framework_Test $test The test that was incomplete.
171: * @param Exception $e The incomplete exception
172: * @param float $time The current time.
173: * @return void
174: */
175: public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
176: $this->paintSkip($e, $test);
177: }
178:
179: /**
180: * Skipped test.
181: *
182: * @param PHPUnit_Framework_Test $test The test that failed.
183: * @param Exception $e The skip object.
184: * @param float $time The current time.
185: * @return void
186: */
187: public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
188: $this->paintSkip($e, $test);
189: }
190:
191: /**
192: * A test suite started.
193: *
194: * @param PHPUnit_Framework_TestSuite $suite The suite to start
195: * @return void
196: */
197: public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {
198: if (!$this->_headerSent) {
199: echo $this->paintHeader();
200: }
201: echo __d('cake_dev', 'Running %s', $suite->getName()) . "\n";
202: }
203:
204: /**
205: * A test suite ended.
206: *
207: * @param PHPUnit_Framework_TestSuite $suite The suite that ended.
208: * @return void
209: */
210: public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {
211: }
212:
213: /**
214: * A test started.
215: *
216: * @param PHPUnit_Framework_Test $test The test that started.
217: * @return void
218: */
219: public function startTest(PHPUnit_Framework_Test $test) {
220: }
221:
222: /**
223: * A test ended.
224: *
225: * @param PHPUnit_Framework_Test $test The test that ended
226: * @param float $time The current time.
227: * @return void
228: */
229: public function endTest(PHPUnit_Framework_Test $test, $time) {
230: $this->numAssertions += $test->getNumAssertions();
231: if ($test->hasFailed()) {
232: return;
233: }
234: $this->paintPass($test, $time);
235: }
236:
237: }
238: