PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/simpletest/eclipse.php

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