|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00011 include_once 'unit_tester.php'; 00012 include_once 'test_case.php'; 00013 include_once 'invoker.php'; 00014 include_once 'socket.php'; 00015 include_once 'mock_objects.php'; 00023 class EclipseReporter extends SimpleScorer { 00024 00030 function EclipseReporter(&$listener, $cc=false){ 00031 $this->_listener = &$listener; 00032 $this->SimpleScorer(); 00033 $this->_case = ""; 00034 $this->_group = ""; 00035 $this->_method = ""; 00036 $this->_cc = $cc; 00037 $this->_error = false; 00038 $this->_fail = false; 00039 } 00040 00045 function getDumper() { 00046 return new SimpleDumper(); 00047 } 00048 00055 function &createListener($port, $host="127.0.0.1"){ 00056 $tmplistener = new SimpleSocket($host, $port, 5); 00057 return $tmplistener; 00058 } 00059 00066 function &createInvoker(&$invoker){ 00067 $eclinvoker = new EclipseInvoker($invoker, $this->_listener); 00068 return $eclinvoker; 00069 } 00070 00076 function escapeVal($raw){ 00077 $needle = array("\\","\"","/","\b","\f","\n","\r","\t"); 00078 $replace = array('\\\\','\"','\/','\b','\f','\n','\r','\t'); 00079 return str_replace($needle, $replace, $raw); 00080 } 00081 00088 function paintPass($message){ 00089 if (! $this->_pass){ 00090 $this->_message = $this->escapeVal($message); 00091 } 00092 $this->_pass = true; 00093 } 00094 00101 function paintFail($message){ 00102 //only get the first failure or error 00103 if (! $this->_fail && ! $this->_error){ 00104 $this->_fail = true; 00105 $this->_message = $this->escapeVal($message); 00106 $this->_listener->write('{status:"fail",message:"'.$this->_message.'",group:"'.$this->_group.'",case:"'.$this->_case.'",method:"'.$this->_method.'"}'); 00107 } 00108 } 00109 00116 function paintError($message){ 00117 if (! $this->_fail && ! $this->_error){ 00118 $this->_error = true; 00119 $this->_message = $this->escapeVal($message); 00120 $this->_listener->write('{status:"error",message:"'.$this->_message.'",group:"'.$this->_group.'",case:"'.$this->_case.'",method:"'.$this->_method.'"}'); 00121 } 00122 } 00123 00124 00131 function paintException($exception){ 00132 if (! $this->_fail && ! $this->_error){ 00133 $this->_error = true; 00134 $message = 'Unexpected exception of type[' . get_class($exception) . 00135 '] with message [' . $exception->getMessage() . '] in [' . 00136 $exception->getFile() .' line '. $exception->getLine() . ']'; 00137 $this->_message = $this->escapeVal($message); 00138 $this->_listener->write( 00139 '{status:"error",message:"' . $this->_message . '",group:"' . 00140 $this->_group . '",case:"' . $this->_case . '",method:"' . $this->_method 00141 . '"}'); 00142 } 00143 } 00144 00145 00152 function paintHeader($test_name) { 00153 } 00154 00160 function paintFooter($test_name) { 00161 } 00162 00169 function paintMethodStart($method) { 00170 $this->_pass = false; 00171 $this->_fail = false; 00172 $this->_error = false; 00173 $this->_method = $this->escapeVal($method); 00174 } 00175 00182 function paintMethodEnd($method){ 00183 if ($this->_fail || $this->_error || ! $this->_pass){ 00184 } else { 00185 $this->_listener->write( 00186 '{status:"pass",message:"' . $this->_message . '",group:"' . 00187 $this->_group . '",case:"' . $this->_case . '",method:"' . 00188 $this->_method . '"}'); 00189 } 00190 } 00191 00197 function paintCaseStart($case){ 00198 $this->_case = $this->escapeVal($case); 00199 } 00200 00206 function paintCaseEnd($case){ 00207 $this->_case = ""; 00208 } 00209 00217 function paintGroupStart($group, $size){ 00218 $this->_group = $this->escapeVal($group); 00219 if ($this->_cc){ 00220 if (extension_loaded('xdebug')){ 00221 xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE); 00222 } 00223 } 00224 } 00225 00231 function paintGroupEnd($group){ 00232 $this->_group = ""; 00233 $cc = ""; 00234 if ($this->_cc){ 00235 if (extension_loaded('xdebug')){ 00236 $arrfiles = xdebug_get_code_coverage(); 00237 xdebug_stop_code_coverage(); 00238 $thisdir = dirname(__FILE__); 00239 $thisdirlen = strlen($thisdir); 00240 foreach ($arrfiles as $index=>$file){ 00241 if (substr($index, 0, $thisdirlen)===$thisdir){ 00242 continue; 00243 } 00244 $lcnt = 0; 00245 $ccnt = 0; 00246 foreach ($file as $line){ 00247 if ($line == -2){ 00248 continue; 00249 } 00250 $lcnt++; 00251 if ($line==1){ 00252 $ccnt++; 00253 } 00254 } 00255 if ($lcnt > 0){ 00256 $cc .= round(($ccnt/$lcnt) * 100, 2) . '%'; 00257 }else{ 00258 $cc .= "0.00%"; 00259 } 00260 $cc.= "\t". $index . "\n"; 00261 } 00262 } 00263 } 00264 $this->_listener->write('{status:"coverage",message:"' . 00265 EclipseReporter::escapeVal($cc) . '"}'); 00266 } 00267 } 00268 00275 class EclipseInvoker extends SimpleInvokerDecorator{ 00276 function EclipseInvoker(&$invoker, &$listener) { 00277 $this->_listener = &$listener; 00278 $this->SimpleInvokerDecorator($invoker); 00279 } 00280 00286 function before($method){ 00287 ob_start(); 00288 $this->_invoker->before($method); 00289 } 00290 00297 function after($method) { 00298 $this->_invoker->after($method); 00299 $output = ob_get_contents(); 00300 ob_end_clean(); 00301 if ($output !== ""){ 00302 $result = $this->_listener->write('{status:"info",message:"' . 00303 EclipseReporter::escapeVal($output) . '"}'); 00304 } 00305 } 00306 } 00307 ?>