Assume an empty PATH_INFO is the root URL.
[hoplite.git] / testing / test_listener.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\test;
18
19 // This TestListener is meant to print to stdout and show CLI output for the
20 // running test suite. It unfortunately conflicts with the standard text runner
21 // UI, so it must be configured manually (see runner.php for an example).
22 //
23 // The format of the output is designed to mimic the Google Test (GTest)
24 // <http://googletest.googlecode.com> framework output.
25 class TestListener extends \PHPUnit_Util_Printer implements \PHPUnit_Framework_TestListener
26 {
27 const COLOR_NONE = 0;
28 const COLOR_RED = 1;
29 const COLOR_GREEN = 2;
30 const COLOR_BLUE = 3;
31 const COLOR_PURPLE = 4;
32 const COLOR_CYAN = 5;
33
34 // The start time of the test suite.
35 private $suite_start_time = 0;
36
37 // The suite depth.
38 private $suite_depth = 0;
39
40 // The number of errors that occured in a suite.
41 private $suite_error_counts = 0;
42
43 // Array of failing tests.
44 private $failing = array();
45
46 // Array of skipped tests.
47 private $skipped = array();
48
49 // Array of incomplete tests.
50 private $incomplete = array();
51
52 // An error occurred.
53 public function addError(\PHPUnit_Framework_Test $test,
54 \Exception $e,
55 $time)
56 {
57 $this->_Print(NULL, $this->_ErrorLocation($e));
58 $this->_Print(' ', $e->GetMessage());
59 $this->_Print('[ ERROR ]', $test->ToString() . ' (' . $this->_Round($time) . ' ms)', self::COLOR_RED);
60 ++$this->suite_error_count;
61 $this->failing[] = $test->ToString();
62 }
63
64 // A failure occurred.
65 public function addFailure(\PHPUnit_Framework_Test $test,
66 \PHPUnit_Framework_AssertionFailedError $e,
67 $time)
68 {
69 $this->_Print(NULL, $this->_ErrorLocation($e));
70 $this->_Print(' ', $e->GetMessage());
71 $this->_Print('[ FAILED ]', $test->ToString() . ' (' . $this->_Round($time) . ' ms)', self::COLOR_RED);
72 ++$this->suite_error_count;
73 $this->failing[] = $test->ToString();
74 }
75
76 // Incomplete test.
77 public function addIncompleteTest(\PHPUnit_Framework_Test $test,
78 \Exception $e, $time)
79 {
80 $this->incomplete[] = $test->ToString();
81 $this->_Print('INCOMPLETE', $e->GetMessage(), self::COLOR_PURPLE);
82 }
83
84 // Skipped test.
85 public function addSkippedTest(\PHPUnit_Framework_Test $test,
86 \Exception $e,
87 $time)
88 {
89 $this->skipped[] = $test->ToString();
90 $this->_Print('SKIPPED', $e->GetMessage(), self::COLOR_BLUE);
91 }
92
93 // A test suite started.
94 public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
95 {
96 // Wrap the suite header.
97 ob_start();
98
99 $this->_Print($this->_SuiteMarker(), $this->_DescribeSuite($suite), self::COLOR_GREEN);
100 $this->suite_start_time = microtime(TRUE);
101 ++$this->suite_depth;
102 $this->suite_error_count = 0;
103
104 // Wrap the suite contents.
105 ob_start();
106 }
107
108 // A test suite ended.
109 public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
110 {
111 $main_suite = (--$this->suite_depth == 0);
112 $color_red = (($main_suite && count($this->failing)) || $this->suite_error_count > 0);
113 $any_output = ob_get_length();
114
115 $delta = microtime(TRUE) - $this->suite_start_time;
116 $this->_Print(
117 $this->_SuiteMarker(),
118 $this->_DescribeSuite($suite) . ' (' . $this->_Round($delta) . ' ms total)',
119 ($color_red ? self::COLOR_RED : self::COLOR_GREEN));
120 $this->Write("\n");
121
122 // If this is the main suite (the one to which all other tests/suites
123 // are attached), then print the test summary.
124 if ($main_suite && $color_red) {
125 $count = count($this->failing);
126 $tests = $this->_Plural('TEST', $count, TRUE);
127 $this->Write($this->_Color(" YOU HAVE $count FAILING $tests:\n", self::COLOR_RED));
128 foreach ($this->failing as $test) {
129 $this->Write(" $test\n");
130 }
131 $this->Write("\n");
132 }
133
134 $count = count($this->incomplete);
135 $any_output |= $count > 0;
136 if ($main_suite && $count) {
137 $tests = $this->_Plural('TEST', $count, TRUE);
138 $this->Write($this->_Color(" YOU HAVE $count INCOMPLETE $tests:\n", self::COLOR_PURPLE));
139 foreach ($this->incomplete as $test) {
140 $this->Write(" $test\n");
141 }
142 $this->Write("\n");
143 }
144
145 $count = count($this->skipped);
146 if ($main_suite && $count) {
147 $tests = $this->_Plural('TEST', $count, TRUE);
148 $this->Write($this->_Color(" YOU HAVE $count SKIPPED $tests:\n", self::COLOR_BLUE));
149 foreach ($this->skipped as $test) {
150 $this->Write(" $test\n");
151 }
152 $this->Write("\n");
153 }
154
155 // Flush the test output.
156 ob_end_flush();
157
158 // Flush the suite header.
159 if ($main_suite || $any_output)
160 ob_end_flush();
161 else
162 ob_end_clean();
163 }
164
165 // A test started.
166 public function startTest(\PHPUnit_Framework_Test $test)
167 {
168 $this->_Print('[ RUN ]', $test->ToString(), self::COLOR_GREEN);
169 }
170
171 // A test ended.
172 public function endTest(\PHPUnit_Framework_Test $test, $time)
173 {
174 $name = $test->ToString();
175 if (in_array($name, $this->skipped) || in_array($name, $this->incomplete)) {
176 $this->_Print('[ ABORT ]', $name . ' (' . $this->_Round($time) . ' ms)', self::COLOR_CYAN);
177 } else if (!in_array($name, $this->failing)) {
178 $this->_Print('[ OK ]', $name . ' (' . $this->_Round($time) . ' ms)', self::COLOR_GREEN);
179 }
180 }
181
182 // Returns the description for a test suite.
183 private function _DescribeSuite(\PHPUnit_Framework_TestSuite $suite)
184 {
185 $count = $suite->Count();
186 return sprintf('%d %s from %s', $count, $this->_Plural('test', $count), $suite->GetName());
187 }
188
189 // Returns the test suite marker.
190 private function _SuiteMarker()
191 {
192 if ($this->suite_depth == 0)
193 return '[==========]';
194 else
195 return '[----------]';
196 }
197
198 // Prints a line to output.
199 private function _Print($column, $annotation, $color = self::COLOR_NONE)
200 {
201 $column = $this->_Color($column, $color);
202 $this->Write("$column $annotation\n");
203 }
204
205 // Takes in a float from microtime() and returns it formatted to display as
206 // milliseconds.
207 private function _Round($time)
208 {
209 return round($time * 1000);
210 }
211
212 // Returns the error location as a string.
213 private function _ErrorLocation(\Exception $e)
214 {
215 $trace = $e->GetTrace();
216 $frame = NULL;
217 // Find the first frame from non-PHPUnit code, which is where the error
218 // should have occurred.
219 foreach ($trace as $f) {
220 if (isset($f['file']) && strpos($f['file'], 'PHPUnit/Framework') === FALSE) {
221 $frame = $f;
222 break;
223 }
224 }
225 if (!$frame)
226 $frame = $trace[0];
227 return $frame['file'] . ':' . $frame['line'];
228 }
229
230 // Colors |$str| to be a certain |$color|.
231 private function _Color($str, $color)
232 {
233 $color_code = '';
234 switch ($color) {
235 case self::COLOR_RED: $color_code = '0;31'; break;
236 case self::COLOR_GREEN: $color_code = '0;32'; break;
237 case self::COLOR_BLUE: $color_code = '0;34'; break;
238 case self::COLOR_PURPLE: $color_code = '0;35'; break;
239 case self::COLOR_CYAN: $color_code = '0;36'; break;
240 }
241 if ($color == self::COLOR_NONE) {
242 return $str;
243 }
244 return "\x1b[{$color_code}m{$str}\x1b[0m";
245 }
246
247 // Returns the plural of the |$word| if |$count| is greater than one.
248 private function _Plural($word, $count, $capitalize = FALSE)
249 {
250 if ($count > 1)
251 return $word . ($capitalize ? 'S' : 's');
252 return $word;
253 }
254 }