PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/reporter.php

http://snaptest.googlecode.com/
PHP | 174 lines | 89 code | 27 blank | 58 comment | 8 complexity | f900a10198b6f4bf2454c685c81a6ce2 MD5 | raw file
  1. <?php
  2. abstract class Snap_UnitTestReporter {
  3. /**
  4. * contains the lines of the report as they occur
  5. * @var array $reports
  6. */
  7. protected $reports;
  8. /**
  9. * report constructor, initializes all the variables
  10. */
  11. public function __construct() {
  12. $this->reports = array();
  13. }
  14. public final function createReport() {
  15. $this->generateReport($this->reports);
  16. }
  17. /**
  18. * records a test passing and adds it to the queue
  19. * @param string Class name
  20. * @param string Method name
  21. **/
  22. public final function recordTestPass($class_name, $method_name) {
  23. $this->reports[] = array(
  24. 'type' => 'pass',
  25. 'function' => $method_name,
  26. 'class' => $class_name,
  27. );
  28. $this->announceTestPass();
  29. }
  30. public final function recordTestCaseComplete($class_name) {
  31. $this->reports[] = array(
  32. 'type' => 'case',
  33. 'class' => $class_name,
  34. );
  35. $this->announceTestCaseComplete();
  36. }
  37. /**
  38. * records a test exception and adds it to the report queue
  39. * @param UnitTestException $e
  40. */
  41. public final function recordTestException(Snap_UnitTestException $e) {
  42. $this->addReport($this->record('fail', $e->getUserMessage(), $this->cullTrace($e->getTrace())));
  43. $this->announceTestFail();
  44. }
  45. /**
  46. * records an unhandled exception and adds it to the report queue
  47. * @param Exception $e
  48. */
  49. public final function recordUnhandledException(Exception $e) {
  50. $this->addReport($this->record('exception', 'Unhandled exception of type '.get_class($e).' with message: '.$e->getMessage(), $this->cullTrace($e->getTrace())));
  51. $this->announceTestFail();
  52. }
  53. /**
  54. * records a test defect exception that occured in setup/teardown
  55. * @param UnitTestException $e
  56. */
  57. public final function recordTestDefect(Exception $e) {
  58. if (method_exists($e, 'getUserMessage')) {
  59. $this->addReport($this->record('defect', $e->getUserMessage(), $this->cullTrace($e->getTrace())));
  60. }
  61. else {
  62. $this->addReport($this->record('defect', $e->getMessage(), $this->cullTrace($e->getTrace())));
  63. }
  64. $this->announceTestDefect();
  65. }
  66. /**
  67. * records a PHP error encountered
  68. * @param string $errstr the php error string
  69. * @param string $errfile the php error file
  70. * @param int $errline the line of the php error
  71. * @param array $trace the backtrace of the error
  72. */
  73. public final function recordPHPError($errstr, $errfile, $errline, $trace) {
  74. $trace = $this->cullTrace($trace);
  75. // file trace is worthless
  76. $trace['file'] = $errfile;
  77. $this->addReport($this->record('phperr', $errstr, $trace, $errline));
  78. $this->announceTestFail();
  79. }
  80. /**
  81. * cull a trace, removing the unit test cruft and leaving the traced item
  82. * @param array $trace the trace array
  83. * @return array an array reduced to the occurance of the test/setup
  84. */
  85. protected final function cullTrace($trace) {
  86. $file = '';
  87. while (true) {
  88. if (!isset($trace[0])) {
  89. break;
  90. }
  91. // drill up until you find a unit test: testXXXXX or setUp or tearDown
  92. if (isset($trace[0]['function']) && (!preg_match('/^(test.*)|(setUp)|(tearDown)$/i', $trace[0]['function']))) {
  93. if (isset($trace[0]['file'])) {
  94. $file = $trace[0]['file'];
  95. }
  96. array_shift($trace);
  97. continue;
  98. }
  99. break;
  100. }
  101. if (!isset($trace[0])) {
  102. return array();
  103. }
  104. // restore the proper file
  105. $trace[0]['file'] = $file;
  106. return $trace[0];
  107. }
  108. /**
  109. * add a report to the output stack
  110. * @param string $output the output to add to the report
  111. */
  112. protected final function addReport($output) {
  113. $this->reports[] = $output;
  114. }
  115. /**
  116. * turn a trace and message into it's final output.
  117. * @param string $message the input message
  118. * @param array $origin the array origin for the message
  119. */
  120. protected function record($type, $message, $backtrace, $line = '') {
  121. $output = array(
  122. 'type' => $type,
  123. 'message' => $message,
  124. 'function' => (isset($backtrace['function'])) ? $backtrace['function'] : 'unknown',
  125. 'class' => (isset($backtrace['class'])) ? $backtrace['class'] : 'unknown',
  126. 'file' => (isset($backtrace['file'])) ? $backtrace['file'] : 'unknown',
  127. 'line' => $line,
  128. );
  129. return $output;
  130. }
  131. /**
  132. * abstract function, generates the final report
  133. */
  134. abstract public function generateReport($reports);
  135. /**
  136. * abstract function, handles announcing a test's completion to the output
  137. * @param int $passes the number of passes
  138. * @param int $defects the number of defects
  139. * @param int $tests the number of tests
  140. * @param string $classname the name of the test class
  141. */
  142. abstract public function announceTestPass();
  143. abstract public function announceTestFail();
  144. abstract public function announceTestDefect();
  145. abstract public function announceTestCaseComplete();
  146. }
  147. ?>