| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* PHPUnit
|
|
|
4 |
*
|
|
|
5 |
* Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
|
|
|
6 |
* All rights reserved.
|
|
|
7 |
*
|
|
|
8 |
* Redistribution and use in source and binary forms, with or without
|
|
|
9 |
* modification, are permitted provided that the following conditions
|
|
|
10 |
* are met:
|
|
|
11 |
*
|
|
|
12 |
* * Redistributions of source code must retain the above copyright
|
|
|
13 |
* notice, this list of conditions and the following disclaimer.
|
|
|
14 |
*
|
|
|
15 |
* * Redistributions in binary form must reproduce the above copyright
|
|
|
16 |
* notice, this list of conditions and the following disclaimer in
|
|
|
17 |
* the documentation and/or other materials provided with the
|
|
|
18 |
* distribution.
|
|
|
19 |
*
|
|
|
20 |
* * Neither the name of Sebastian Bergmann nor the names of his
|
|
|
21 |
* contributors may be used to endorse or promote products derived
|
|
|
22 |
* from this software without specific prior written permission.
|
|
|
23 |
*
|
|
|
24 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
25 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
26 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
27 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
28 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
29 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
30 |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
31 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
32 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
33 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
34 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
35 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
36 |
*
|
|
|
37 |
* @category Testing
|
|
|
38 |
* @package PHPUnit
|
|
|
39 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
40 |
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
41 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
42 |
* @link http://www.phpunit.de/
|
|
|
43 |
* @since File available since Release 3.0.0
|
|
|
44 |
*/
|
|
|
45 |
|
|
|
46 |
require_once 'PHPUnit/Framework.php';
|
|
|
47 |
require_once 'PHPUnit/Util/Filter.php';
|
|
|
48 |
require_once 'PHPUnit/Util/Printer.php';
|
|
|
49 |
require_once 'PHPUnit/Util/Test.php';
|
|
|
50 |
|
|
|
51 |
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* A TestListener that generates JSON messages.
|
|
|
55 |
*
|
|
|
56 |
* @category Testing
|
|
|
57 |
* @package PHPUnit
|
|
|
58 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
59 |
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
60 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
61 |
* @version Release: 3.4.15
|
|
|
62 |
* @link http://www.phpunit.de/
|
|
|
63 |
* @since Class available since Release 3.0.0
|
|
|
64 |
*/
|
|
|
65 |
class PHPUnit_Util_Log_JSON extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
|
|
|
66 |
{
|
|
|
67 |
/**
|
|
|
68 |
* @var string
|
|
|
69 |
*/
|
|
|
70 |
protected $currentTestSuiteName = '';
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* @var string
|
|
|
74 |
*/
|
|
|
75 |
protected $currentTestName = '';
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* @var boolean
|
|
|
79 |
* @access private
|
|
|
80 |
*/
|
|
|
81 |
protected $currentTestPass = TRUE;
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* An error occurred.
|
|
|
85 |
*
|
|
|
86 |
* @param PHPUnit_Framework_Test $test
|
|
|
87 |
* @param Exception $e
|
|
|
88 |
* @param float $time
|
|
|
89 |
*/
|
|
|
90 |
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
|
|
|
91 |
{
|
|
|
92 |
$this->writeCase(
|
|
|
93 |
'error',
|
|
|
94 |
$time,
|
|
|
95 |
PHPUnit_Util_Filter::getFilteredStacktrace(
|
|
|
96 |
$e,
|
|
|
97 |
TRUE,
|
|
|
98 |
FALSE
|
|
|
99 |
),
|
|
|
100 |
$e->getMessage()
|
|
|
101 |
);
|
|
|
102 |
|
|
|
103 |
$this->currentTestPass = FALSE;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* A failure occurred.
|
|
|
108 |
*
|
|
|
109 |
* @param PHPUnit_Framework_Test $test
|
|
|
110 |
* @param PHPUnit_Framework_AssertionFailedError $e
|
|
|
111 |
* @param float $time
|
|
|
112 |
*/
|
|
|
113 |
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
|
|
|
114 |
{
|
|
|
115 |
$this->writeCase(
|
|
|
116 |
'fail',
|
|
|
117 |
$time,
|
|
|
118 |
PHPUnit_Util_Filter::getFilteredStacktrace(
|
|
|
119 |
$e,
|
|
|
120 |
TRUE,
|
|
|
121 |
FALSE
|
|
|
122 |
),
|
|
|
123 |
$e->getMessage()
|
|
|
124 |
);
|
|
|
125 |
|
|
|
126 |
$this->currentTestPass = FALSE;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Incomplete test.
|
|
|
131 |
*
|
|
|
132 |
* @param PHPUnit_Framework_Test $test
|
|
|
133 |
* @param Exception $e
|
|
|
134 |
* @param float $time
|
|
|
135 |
*/
|
|
|
136 |
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
|
|
|
137 |
{
|
|
|
138 |
$this->writeCase('error', $time, array(), 'Incomplete Test');
|
|
|
139 |
|
|
|
140 |
$this->currentTestPass = FALSE;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Skipped test.
|
|
|
145 |
*
|
|
|
146 |
* @param PHPUnit_Framework_Test $test
|
|
|
147 |
* @param Exception $e
|
|
|
148 |
* @param float $time
|
|
|
149 |
*/
|
|
|
150 |
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
|
|
|
151 |
{
|
|
|
152 |
$this->writeCase('error', $time, array(), 'Skipped Test');
|
|
|
153 |
|
|
|
154 |
$this->currentTestPass = FALSE;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* A testsuite started.
|
|
|
159 |
*
|
|
|
160 |
* @param PHPUnit_Framework_TestSuite $suite
|
|
|
161 |
*/
|
|
|
162 |
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
|
|
|
163 |
{
|
|
|
164 |
$this->currentTestSuiteName = $suite->getName();
|
|
|
165 |
$this->currentTestName = '';
|
|
|
166 |
|
|
|
167 |
$message = array(
|
|
|
168 |
'event' => 'suiteStart',
|
|
|
169 |
'suite' => $this->currentTestSuiteName,
|
|
|
170 |
'tests' => count($suite)
|
|
|
171 |
);
|
|
|
172 |
|
|
|
173 |
$this->write($this->encode($message));
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* A testsuite ended.
|
|
|
178 |
*
|
|
|
179 |
* @param PHPUnit_Framework_TestSuite $suite
|
|
|
180 |
*/
|
|
|
181 |
public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
|
|
|
182 |
{
|
|
|
183 |
$this->currentTestSuiteName = '';
|
|
|
184 |
$this->currentTestName = '';
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* A test started.
|
|
|
189 |
*
|
|
|
190 |
* @param PHPUnit_Framework_Test $test
|
|
|
191 |
*/
|
|
|
192 |
public function startTest(PHPUnit_Framework_Test $test)
|
|
|
193 |
{
|
|
|
194 |
$this->currentTestName = PHPUnit_Util_Test::describe($test);
|
|
|
195 |
$this->currentTestPass = TRUE;
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* A test ended.
|
|
|
200 |
*
|
|
|
201 |
* @param PHPUnit_Framework_Test $test
|
|
|
202 |
* @param float $time
|
|
|
203 |
*/
|
|
|
204 |
public function endTest(PHPUnit_Framework_Test $test, $time)
|
|
|
205 |
{
|
|
|
206 |
if ($this->currentTestPass) {
|
|
|
207 |
$this->writeCase('pass', $time);
|
|
|
208 |
}
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
/**
|
|
|
212 |
* @param string $status
|
|
|
213 |
* @param float $time
|
|
|
214 |
* @param array $trace
|
|
|
215 |
* @param string $message
|
|
|
216 |
*/
|
|
|
217 |
protected function writeCase($status, $time, array $trace = array(), $message = '')
|
|
|
218 |
{
|
|
|
219 |
$message = array(
|
|
|
220 |
'event' => 'test',
|
|
|
221 |
'suite' => $this->currentTestSuiteName,
|
|
|
222 |
'test' => $this->currentTestName,
|
|
|
223 |
'status' => $status,
|
|
|
224 |
'time' => $time,
|
|
|
225 |
'trace' => $trace,
|
|
|
226 |
'message' => $message
|
|
|
227 |
);
|
|
|
228 |
|
|
|
229 |
$this->write($this->encode($message));
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
/**
|
|
|
233 |
* @param array $message
|
|
|
234 |
* @return string
|
|
|
235 |
*/
|
|
|
236 |
protected function encode($message)
|
|
|
237 |
{
|
|
|
238 |
if (function_exists('json_encode')) {
|
|
|
239 |
return json_encode($message);
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
$first = TRUE;
|
|
|
243 |
$result = '';
|
|
|
244 |
|
|
|
245 |
if (is_scalar($message)) {
|
|
|
246 |
$message = array ($message);
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
foreach ($message as $key => $value) {
|
|
|
250 |
if (!$first) {
|
|
|
251 |
$result .= ',';
|
|
|
252 |
} else {
|
|
|
253 |
$first = FALSE;
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
$result .= sprintf('"%s":', $this->escape($key));
|
|
|
257 |
|
|
|
258 |
if (is_array($value) || is_object($value)) {
|
|
|
259 |
$result .= sprintf('%s', $this->encode($value));
|
|
|
260 |
} else {
|
|
|
261 |
$result .= sprintf('"%s"', $this->escape($value));
|
|
|
262 |
}
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
return '{' . $result . '}';
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
/**
|
|
|
269 |
* @param string $value
|
|
|
270 |
* @return string
|
|
|
271 |
*/
|
|
|
272 |
protected function escape($value)
|
|
|
273 |
{
|
|
|
274 |
return str_replace(
|
|
|
275 |
array("\\", "\"", "/", "\b", "\f", "\n", "\r", "\t"),
|
|
|
276 |
array('\\\\', '\"', '\/', '\b', '\f', '\n', '\r', '\t'),
|
|
|
277 |
$value
|
|
|
278 |
);
|
|
|
279 |
}
|
|
|
280 |
}
|
|
|
281 |
?>
|