Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / web / public_php / webtt / vendors / simpletest / eclipse.php
blob20bd4530bb68f990242f1a23c24f01adc987e765
1 <?php
2 /**
3 * base include file for eclipse plugin
4 * @package SimpleTest
5 * @subpackage Eclipse
6 * @version $Id: eclipse.php 2011 2011-04-29 08:22:48Z pp11 $
7 */
8 /**#@+
9 * simpletest include files
11 include_once 'unit_tester.php';
12 include_once 'test_case.php';
13 include_once 'invoker.php';
14 include_once 'socket.php';
15 include_once 'mock_objects.php';
16 /**#@-*/
18 /**
19 * base reported class for eclipse plugin
20 * @package SimpleTest
21 * @subpackage Eclipse
23 class EclipseReporter extends SimpleScorer {
25 /**
26 * Reporter to be run inside of Eclipse interface.
27 * @param object $listener Eclipse listener (?).
28 * @param boolean $cc Whether to include test coverage.
30 function __construct(&$listener, $cc=false){
31 $this->listener = &$listener;
32 $this->SimpleScorer();
33 $this->case = "";
34 $this->group = "";
35 $this->method = "";
36 $this->cc = $cc;
37 $this->error = false;
38 $this->fail = false;
41 /**
42 * Means to display human readable object comparisons.
43 * @return SimpleDumper Visual comparer.
45 function getDumper() {
46 return new SimpleDumper();
49 /**
50 * Localhost connection from Eclipse.
51 * @param integer $port Port to connect to Eclipse.
52 * @param string $host Normally localhost.
53 * @return SimpleSocket Connection to Eclipse.
55 function &createListener($port, $host="127.0.0.1"){
56 $tmplistener = &new SimpleSocket($host, $port, 5);
57 return $tmplistener;
60 /**
61 * Wraps the test in an output buffer.
62 * @param SimpleInvoker $invoker Current test runner.
63 * @return EclipseInvoker Decorator with output buffering.
64 * @access public
66 function &createInvoker(&$invoker){
67 $eclinvoker = &new EclipseInvoker($invoker, $this->listener);
68 return $eclinvoker;
71 /**
72 * C style escaping.
73 * @param string $raw String with backslashes, quotes and whitespace.
74 * @return string Replaced with C backslashed tokens.
76 function escapeVal($raw){
77 $needle = array("\\","\"","/","\b","\f","\n","\r","\t");
78 $replace = array('\\\\','\"','\/','\b','\f','\n','\r','\t');
79 return str_replace($needle, $replace, $raw);
82 /**
83 * Stash the first passing item. Clicking the test
84 * item goes to first pass.
85 * @param string $message Test message, but we only wnat the first.
86 * @access public
88 function paintPass($message){
89 if (! $this->pass){
90 $this->message = $this->escapeVal($message);
92 $this->pass = true;
95 /**
96 * Stash the first failing item. Clicking the test
97 * item goes to first fail.
98 * @param string $message Test message, but we only wnat the first.
99 * @access public
101 function paintFail($message){
102 //only get the first failure or error
103 if (! $this->fail && ! $this->error){
104 $this->fail = true;
105 $this->message = $this->escapeVal($message);
106 $this->listener->write('{status:"fail",message:"'.$this->message.'",group:"'.$this->group.'",case:"'.$this->case.'",method:"'.$this->method.'"}');
111 * Stash the first error. Clicking the test
112 * item goes to first error.
113 * @param string $message Test message, but we only wnat the first.
114 * @access public
116 function paintError($message){
117 if (! $this->fail && ! $this->error){
118 $this->error = true;
119 $this->message = $this->escapeVal($message);
120 $this->listener->write('{status:"error",message:"'.$this->message.'",group:"'.$this->group.'",case:"'.$this->case.'",method:"'.$this->method.'"}');
126 * Stash the first exception. Clicking the test
127 * item goes to first message.
128 * @param string $message Test message, but we only wnat the first.
129 * @access public
131 function paintException($exception){
132 if (! $this->fail && ! $this->error){
133 $this->error = true;
134 $message = 'Unexpected exception of type[' . get_class($exception) .
135 '] with message [' . $exception->getMessage() . '] in [' .
136 $exception->getFile() .' line '. $exception->getLine() . ']';
137 $this->message = $this->escapeVal($message);
138 $this->listener->write(
139 '{status:"error",message:"' . $this->message . '",group:"' .
140 $this->group . '",case:"' . $this->case . '",method:"' . $this->method
141 . '"}');
147 * We don't display any special header.
148 * @param string $test_name First test top level
149 * to start.
150 * @access public
152 function paintHeader($test_name) {
156 * We don't display any special footer.
157 * @param string $test_name The top level test.
158 * @access public
160 function paintFooter($test_name) {
164 * Paints nothing at the start of a test method, but stash
165 * the method name for later.
166 * @param string $test_name Name of test that is starting.
167 * @access public
169 function paintMethodStart($method) {
170 $this->pass = false;
171 $this->fail = false;
172 $this->error = false;
173 $this->method = $this->escapeVal($method);
177 * Only send one message if the test passes, after that
178 * suppress the message.
179 * @param string $test_name Name of test that is ending.
180 * @access public
182 function paintMethodEnd($method){
183 if ($this->fail || $this->error || ! $this->pass){
184 } else {
185 $this->listener->write(
186 '{status:"pass",message:"' . $this->message . '",group:"' .
187 $this->group . '",case:"' . $this->case . '",method:"' .
188 $this->method . '"}');
193 * Stashes the test case name for the later failure message.
194 * @param string $test_name Name of test or other label.
195 * @access public
197 function paintCaseStart($case){
198 $this->case = $this->escapeVal($case);
202 * Drops the name.
203 * @param string $test_name Name of test or other label.
204 * @access public
206 function paintCaseEnd($case){
207 $this->case = "";
211 * Stashes the name of the test suite. Starts test coverage
212 * if enabled.
213 * @param string $group Name of test or other label.
214 * @param integer $size Number of test cases starting.
215 * @access public
217 function paintGroupStart($group, $size){
218 $this->group = $this->escapeVal($group);
219 if ($this->cc){
220 if (extension_loaded('xdebug')){
221 xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
227 * Paints coverage report if enabled.
228 * @param string $group Name of test or other label.
229 * @access public
231 function paintGroupEnd($group){
232 $this->group = "";
233 $cc = "";
234 if ($this->cc){
235 if (extension_loaded('xdebug')){
236 $arrfiles = xdebug_get_code_coverage();
237 xdebug_stop_code_coverage();
238 $thisdir = dirname(__FILE__);
239 $thisdirlen = strlen($thisdir);
240 foreach ($arrfiles as $index=>$file){
241 if (substr($index, 0, $thisdirlen)===$thisdir){
242 continue;
244 $lcnt = 0;
245 $ccnt = 0;
246 foreach ($file as $line){
247 if ($line == -2){
248 continue;
250 $lcnt++;
251 if ($line==1){
252 $ccnt++;
255 if ($lcnt > 0){
256 $cc .= round(($ccnt/$lcnt) * 100, 2) . '%';
257 }else{
258 $cc .= "0.00%";
260 $cc.= "\t". $index . "\n";
264 $this->listener->write('{status:"coverage",message:"' .
265 EclipseReporter::escapeVal($cc) . '"}');
270 * Invoker decorator for Eclipse. Captures output until
271 * the end of the test.
272 * @package SimpleTest
273 * @subpackage Eclipse
275 class EclipseInvoker extends SimpleInvokerDecorator{
276 function __construct(&$invoker, &$listener) {
277 $this->listener = &$listener;
278 $this->SimpleInvokerDecorator($invoker);
282 * Starts output buffering.
283 * @param string $method Test method to call.
284 * @access public
286 function before($method){
287 ob_start();
288 $this->invoker->before($method);
292 * Stops output buffering and send the captured output
293 * to the listener.
294 * @param string $method Test method to call.
295 * @access public
297 function after($method) {
298 $this->invoker->after($method);
299 $output = ob_get_contents();
300 ob_end_clean();
301 if ($output !== ""){
302 $result = $this->listener->write('{status:"info",message:"' .
303 EclipseReporter::escapeVal($output) . '"}');