forked from simpletest/simpletest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheclipse.php
More file actions
321 lines (296 loc) · 9.15 KB
/
eclipse.php
File metadata and controls
321 lines (296 loc) · 9.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
require_once 'unit_tester.php';
require_once 'test_case.php';
require_once 'invoker.php';
require_once 'socket.php';
require_once 'mock_objects.php';
/**
* base reported class for eclipse plugin
*/
class EclipseReporter extends SimpleScorer
{
/**
* Reporter to be run inside of Eclipse interface.
*
* @param object $listener Eclipse listener (?).
* @param bool $cc Whether to include test coverage.
*/
public function __construct(&$listener, $cc=false)
{
$this->listener = $listener;
$this->SimpleScorer();
$this->case = '';
$this->group = '';
$this->method = '';
$this->cc = $cc;
$this->error = false;
$this->fail = false;
}
/**
* Means to display human readable object comparisons.
*
* @return SimpleDumper Visual comparer.
*/
public function getDumper()
{
return new SimpleDumper();
}
/**
* Localhost connection from Eclipse.
*
* @param int $port Port to connect to Eclipse.
* @param string $host Normally localhost.
*
* @return SimpleSocket Connection to Eclipse.
*/
public function createListener($port, $host='127.0.0.1')
{
$tmplistener = new SimpleSocket($host, $port, 5);
return $tmplistener;
}
/**
* Wraps the test in an output buffer.
*
* @param SimpleInvoker $invoker Current test runner.
*
* @return EclipseInvoker Decorator with output buffering.
*/
public function createInvoker(&$invoker)
{
$eclinvoker = new EclipseInvoker($invoker, $this->listener);
return $eclinvoker;
}
/**
* C style escaping.
*
* @param string $raw String with backslashes, quotes and whitespace.
*
* @return string Replaced with C backslashed tokens.
*/
public static function escapeVal($raw)
{
$needle = array('\\','"','/',"\b","\f","\n","\r","\t");
$replace = array('\\\\','\"','\/','\b','\f','\n','\r','\t');
return str_replace($needle, $replace, $raw);
}
/**
* Stash the first passing item. Clicking the test item goes to first pass.
*
* @param string $message Test message, but we only wnat the first.
*/
public function paintPass($message)
{
if (! $this->pass) {
$this->message = self::escapeVal($message);
}
$this->pass = true;
}
/**
* Stash the first failing item. Clicking the test item goes to first fail.
*
* @param string $message Test message, but we only wnat the first.
*/
public function paintFail($message)
{
//only get the first failure or error
if (! $this->fail && ! $this->error) {
$this->fail = true;
$this->message = self::escapeVal($message);
$this->listener->write(
'{status:"fail",message:"' . $this->message . '",group:"' . $this->group .
'",case:"' . $this->case . '",method:"' . $this->method . '"}'
);
}
}
/**
* Stash the first error. Clicking the test item goes to first error.
*
* @param string $message Test message, but we only wnat the first.
*/
public function paintError($message)
{
if (! $this->fail && ! $this->error) {
$this->error = true;
$this->message = self::escapeVal($message);
$this->listener->write(
'{status:"error",message:"' . $this->message . '",group:"' . $this->group .
'",case:"' . $this->case . '",method:"' . $this->method . '"}'
);
}
}
/**
* Stash the first exception. Clicking the test item goes to first message.
*
* @param string $message Test message, but we only wnat the first.
*/
public function paintException($exception)
{
if (! $this->fail && ! $this->error) {
$this->error = true;
$message = 'Unexpected exception of type[' . get_class($exception) .
'] with message [' . $exception->getMessage() . '] in [' .
$exception->getFile() . ' line ' . $exception->getLine() . ']';
$this->message = self::escapeVal($message);
$this->listener->write(
'{status:"error",message:"' . $this->message . '",group:"' . $this->group .
'",case:"' . $this->case . '",method:"' . $this->method. '"}'
);
}
}
/**
* We don't display any special header.
*
* @param string $test_name First test top level to start.
*/
public function paintHeader($test_name)
{
}
/**
* We don't display any special footer.
*
* @param string $test_name The top level test.
*/
public function paintFooter($test_name)
{
}
/**
* Paints nothing at the start of a test method, but stash the method name for later.
*
* @param string $test_name Name of test that is starting.
*/
public function paintMethodStart($method)
{
$this->pass = false;
$this->fail = false;
$this->error = false;
$this->method = self::escapeVal($method);
}
/**
* Only send one message if the test passes, after that suppress the message.
*
* @param string $test_name Name of test that is ending.
*/
public function paintMethodEnd($method)
{
if ($this->fail || $this->error || ! $this->pass) {
} else {
$this->listener->write(
'{status:"pass",message:"' . $this->message . '",group:"' .
$this->group . '",case:"' . $this->case . '",method:"' .
$this->method . '"}');
}
}
/**
* Stashes the test case name for the later failure message.
*
* @param string $test_name Name of test or other label.
*/
public function paintCaseStart($case)
{
$this->case = self::escapeVal($case);
}
/**
* Drops the name.
*
* @param string $test_name Name of test or other label.
*/
public function paintCaseEnd($case)
{
$this->case = '';
}
/**
* Stashes the name of the test suite. Starts test coverage if enabled.
*
* @param string $group Name of test or other label.
* @param int $size Number of test cases starting.
*/
public function paintGroupStart($group, $size)
{
$this->group = self::escapeVal($group);
if ($this->cc) {
if (extension_loaded('xdebug')) {
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
}
}
}
/**
* Paints coverage report if enabled.
*
* @param string $group Name of test or other label.
*/
public function paintGroupEnd($group)
{
$this->group = '';
$cc = '';
if ($this->cc) {
if (extension_loaded('xdebug')) {
$arrfiles = xdebug_get_code_coverage();
xdebug_stop_code_coverage();
$thisdir = dirname(__FILE__);
$thisdirlen = strlen($thisdir);
foreach ($arrfiles as $index=>$file) {
if (substr($index, 0, $thisdirlen)===$thisdir) {
continue;
}
$lcnt = 0;
$ccnt = 0;
foreach ($file as $line) {
if ($line == -2) {
continue;
}
$lcnt++;
if ($line==1) {
$ccnt++;
}
}
if ($lcnt > 0) {
$cc .= round(($ccnt/$lcnt) * 100, 2) . '%';
} else {
$cc .= '0.00%';
}
$cc.= "\t" . $index . "\n";
}
}
}
$this->listener->write(
'{status:"coverage",message:"' . self::escapeVal($cc) . '"}'
);
}
}
/**
* Invoker decorator for Eclipse. Captures output until the end of the test.
*/
class EclipseInvoker extends SimpleInvokerDecorator
{
public function __construct(&$invoker, &$listener)
{
$this->listener = $listener;
$this->SimpleInvokerDecorator($invoker);
}
/**
* Starts output buffering.
*
* @param string $method Test method to call.
*/
public function before($method)
{
ob_start();
$this->invoker->before($method);
}
/**
* Stops output buffering and send the captured output to the listener.
*
* @param string $method Test method to call.
*/
public function after($method)
{
$this->invoker->after($method);
$output = ob_get_contents();
ob_end_clean();
if ($output !== '') {
$result = $this->listener->write(
'{status:"info",message:"' . EclipseReporter::escapeVal($output) . '"}'
);
}
}
}