PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/simpletestlib/eclipse.php

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