PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/php-lib/source/php/api/Tests/util/Formatter.php

https://code.google.com/
PHP | 247 lines | 104 code | 16 blank | 127 comment | 12 complexity | 6ead0d38691a37b4a57c74d8d16dfe08 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * Gathers details about PHPUnit2 test suites as they
  4. * are been executed. This does not actually format any output
  5. * but simply gathers extended information about the overall
  6. * results of all suites & their tests for use elsewhere.
  7. *
  8. * Changelog:
  9. * 0.6 First created [David Spurr]
  10. * 0.7 Added fix to getTestException provided [Glen Ogilvie]
  11. *
  12. * @version 0.7 2006-03-12
  13. * @author David Spurr
  14. */
  15. /**#@+
  16. * @var int
  17. */
  18. /**
  19. * Failure test status constant
  20. */
  21. define('TEST_FAILURE', -1);
  22. /**
  23. * Error test status constant
  24. */
  25. define('TEST_ERROR', 0);
  26. /**
  27. * Success test status constant
  28. */
  29. define('TEST_SUCCESS', 1);
  30. /**
  31. * Incomplete test status constant
  32. */
  33. define('TEST_INCOMPLETE', 2);
  34. /**#@-*/
  35. class util_Formatter implements PHPUnit_Framework_TestListener
  36. {
  37. /**
  38. * Holds array of suites and total number of tests run
  39. * @var array
  40. */
  41. private $suiteResults;
  42. /**
  43. * Holds data of current suite that is been run
  44. * @var array
  45. */
  46. private $currentSuite;
  47. /**
  48. * Holds data of current test that is been run
  49. * @var array
  50. */
  51. private $currentTest;
  52. /**
  53. * Whether PEAR Benchmark_Timer is available for timing
  54. * @var boolean
  55. */
  56. private $hasTimer;
  57. /**
  58. * Holds the PEAR Benchmark_Timer object
  59. * @var obj Benchmark_Timer
  60. */
  61. private $timer;
  62. /**
  63. * Constructor, checks to see availability of PEAR Benchmark_Timer and
  64. * sets up basic properties
  65. */
  66. public function __construct() {
  67. try {
  68. require_once('Benchmark/Timer.php');
  69. $this->timer = new Benchmark_Timer();
  70. $this->hasTimer = true;
  71. } catch (Exception $e) {
  72. $this->hasTimer = false;
  73. }
  74. $this->suiteResults = array(
  75. 'suites' => array(), // array of suites run
  76. 'hasTimer' => $this->hasTimer, // availability of PEAR Benchmark_Timer
  77. 'totalTests' => 0 // total number of tests run
  78. );
  79. }
  80. /**
  81. * Returns the suite results
  82. *
  83. * @access public
  84. * @return array Suite results
  85. */
  86. public function getSuiteResults() {
  87. return $this->suiteResults;
  88. }
  89. /**
  90. * Sets up the container for result details of the current test suite when
  91. * each suite is first run
  92. *
  93. * @access public
  94. * @param obj PHPUnit2_Framework_TestSuite, the suite that is been run
  95. * @return void
  96. */
  97. public function startTestSuite( PHPUnit_Framework_TestSuite $suite) {
  98. if(strlen($suite->getName())) {
  99. $this->currentSuite = array(
  100. 'suite' => $suite, // the test suite
  101. 'tests' => array(), // the tests in the suite
  102. 'errors' => 0, // number of tests with errors
  103. 'failures' => 0, // number of tests which failed
  104. 'incomplete' => 0); // number of tests that were not completed correctly
  105. }
  106. }
  107. /**
  108. * Sets up the container for result details of the current test when each
  109. * test is first run
  110. *
  111. * @access public
  112. * @param obj PHPUnit2_Framework_Test, the test that is being run
  113. * @return void
  114. */
  115. public function startTest(PHPUnit_Framework_Test $test) {
  116. if($test instanceof PHPUnit_Framework_TestCase) {
  117. $this->currentTest = array(
  118. 'name' => preg_replace('(\(.*\))', '', $test->toString()), // the name of the test (without the suite name)
  119. 'timeElapsed' => 0, // execution time of the test
  120. 'status' => TEST_SUCCESS, // status of the test execution
  121. 'message' => '', // user message of test result
  122. 'exception' => NULL, // original caught exception thrown by test upon failure/error
  123. 'uid' => md5(microtime()) // a unique ID for this test (used for identification purposes in results)
  124. );
  125. if($this->hasTimer) $this->timer->start();
  126. }
  127. }
  128. /**
  129. * Adds the failure detail to the current test and increases the failure
  130. * count for the current suite
  131. *
  132. * @access public
  133. * @param obj PHPUnit2_Framework_Test, current test that is being run
  134. * @param obj PHPUnit2_Framework_AssertationFailedError, PHPUnit2 error
  135. * @return void
  136. */
  137. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {
  138. $this->currentSuite['failures']++;
  139. $this->currentTest['status'] = TEST_FAILURE;
  140. $this->currentTest['message'] = $e->toString();
  141. $this->currentTest['exception'] = $this->getTestException($test, $e);
  142. }
  143. /**
  144. * Adds the error detail to the current test and increases the error
  145. * count for the current suite
  146. *
  147. * @access public
  148. * @param obj PHPUnit2_Framework_Test, current test that is being run
  149. * @param obj PHPUnit2_Framework_AssertationFailedError, PHPUnit2 error
  150. * @return void
  151. */
  152. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
  153. $this->currentSuite['errors']++;
  154. $this->currentTest['status'] = TEST_ERROR;
  155. $this->currentTest['message'] = $e->getMessage();
  156. $this->currentTest['exception'] = $this->getTestException($test, $e);
  157. }
  158. /**
  159. * Adds the test incomplete detail to the current test and increases the incomplete
  160. * count for the current suite
  161. *
  162. * @access public
  163. * @param obj PHPUnit2_Framework_Test, current test that is being run
  164. * @param obj PHPUnit2_Framework_AssertationFailedError, PHPUnit2 error
  165. * @return void
  166. */
  167. public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  168. {
  169. $this->currentSuite['incomplete']++;
  170. $this->currentTest['status'] = TEST_INCOMPLETE;
  171. $this->currentTest['message'] = $e->toString();
  172. $this->currentTest['exception'] = $this->getTestException($test, $e);
  173. }
  174. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  175. {
  176. $this->currentSuite['skipped']++;
  177. $this->currentTest['status'] = TEST_INCOMPLETE;
  178. $this->currentTest['message'] = $e->toString();
  179. $this->currentTest['exception'] = $this->getTestException($test, $e);
  180. }
  181. /**
  182. * Upon completion of a test, records the execution time (if available) and adds the test to
  183. * the tests performed in the current suite.
  184. *
  185. * @access public
  186. * @param obj PHPUnit2_Framework_Test, current test that is being run
  187. * @return void
  188. */
  189. public function endTest( PHPUnit_Framework_Test $test, $time) {
  190. if($this->hasTimer) {
  191. $this->timer->stop();
  192. $this->currentTest['timeElapsed'] = $this->timer->timeElapsed();
  193. }
  194. array_push($this->currentSuite['tests'], $this->currentTest);
  195. }
  196. /**
  197. * Upon completion of a test suite adds the suite to the suties performed
  198. *
  199. * @acces public
  200. * @param obj PHPUnit2_Framework_TestSuite, current suite that is being run
  201. * @return void
  202. */
  203. public function endTestSuite( PHPUnit_Framework_TestSuite $suite) {
  204. if(strlen($suite->getName())) {
  205. array_push($this->suiteResults['suites'], $this->currentSuite);
  206. }
  207. }
  208. /**
  209. * Tries to get the original exception thrown by the test on failure/error
  210. * to enable us to give a bit more detail about the failure/error
  211. *
  212. * @access private
  213. * @param obj PHPUnit2_Framework_Test, current test that is being run
  214. * @param obj PHPUnit2_Framework_AssertationFailedError, PHPUnit2 error
  215. */
  216. private function getTestException(PHPUnit_Framework_Test $test, Exception $e) {
  217. // get the name of the testFile from the test
  218. $testName = ereg_replace('(.*)\((.*[^)])\)', '\\2', $test->toString());
  219. $trace = $e->getTrace();
  220. // loop through the exception trace to find the original exception
  221. for ($i = 0; $i < count($trace); $i++) {
  222. if(array_key_exists('file', $trace[$i])) {
  223. if(stristr($trace[$i]['file'], $testName.'.php') != false) return $trace[$i];
  224. }
  225. if(array_key_exists('file:protected', $trace[$i])) {
  226. if(stristr($trace[$i]['file:protected'], $testName.'.php') != false) return $trace[$i];
  227. }
  228. }
  229. }
  230. }
  231. ?>