/branches/lazy-helpers/vendor/simpletest/errors.php

https://github.com/akelos/v1 · PHP · 182 lines · 80 code · 14 blank · 88 comment · 7 complexity · e876a39f3c85bb13c5f2e0603ca0230d MD5 · raw file

  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id: errors.php,v 1.14 2006/02/06 06:05:18 lastcraft Exp $
  7. */
  8. /** @ignore - PHP5 compatibility fix. */
  9. if (! defined('E_STRICT')) {
  10. define('E_STRICT', 2048);
  11. }
  12. /**#@+
  13. * Includes SimpleTest files.
  14. */
  15. require_once(dirname(__FILE__) . '/invoker.php');
  16. /**
  17. * Extension that traps errors into an error queue.
  18. * @package SimpleTest
  19. * @subpackage UnitTester
  20. */
  21. class SimpleErrorTrappingInvoker extends SimpleInvokerDecorator {
  22. /**
  23. * Stores the invoker to wrap.
  24. * @param SimpleInvoker $invoker Test method runner.
  25. */
  26. function SimpleErrorTrappingInvoker(&$invoker) {
  27. $this->SimpleInvokerDecorator($invoker);
  28. }
  29. /**
  30. * Invokes a test method and dispatches any
  31. * untrapped errors. Called back from
  32. * the visiting runner.
  33. * @param string $method Test method to call.
  34. * @access public
  35. */
  36. function invoke($method) {
  37. set_error_handler('simpleTestErrorHandler');
  38. parent::invoke($method);
  39. $queue = &SimpleErrorQueue::instance();
  40. while (list($severity, $message, $file, $line, $globals) = $queue->extract()) {
  41. $severity = SimpleErrorQueue::getSeverityAsString($severity);
  42. $test_case = &$this->getTestCase();
  43. $test_case->error($severity, $message, $file, $line);
  44. }
  45. restore_error_handler();
  46. }
  47. }
  48. /**
  49. * Singleton error queue used to record trapped
  50. * errors.
  51. * @package SimpleTest
  52. * @subpackage UnitTester
  53. */
  54. class SimpleErrorQueue {
  55. var $_queue;
  56. /**
  57. * Starts with an empty queue.
  58. * @access public
  59. */
  60. function SimpleErrorQueue() {
  61. $this->clear();
  62. }
  63. /**
  64. * Adds an error to the front of the queue.
  65. * @param $severity PHP error code.
  66. * @param $message Text of error.
  67. * @param $filename File error occoured in.
  68. * @param $line Line number of error.
  69. * @param $super_globals Hash of PHP super global arrays.
  70. * @access public
  71. */
  72. function add($severity, $message, $filename, $line, $super_globals) {
  73. array_push(
  74. $this->_queue,
  75. array($severity, $message, $filename, $line, $super_globals));
  76. }
  77. /**
  78. * Pulls the earliest error from the queue.
  79. * @return False if none, or a list of error
  80. * information. Elements are: severity
  81. * as the PHP error code, the error message,
  82. * the file with the error, the line number
  83. * and a list of PHP super global arrays.
  84. * @access public
  85. */
  86. function extract() {
  87. if (count($this->_queue)) {
  88. return array_shift($this->_queue);
  89. }
  90. return false;
  91. }
  92. /**
  93. * Discards the contents of the error queue.
  94. * @access public
  95. */
  96. function clear() {
  97. $this->_queue = array();
  98. }
  99. /**
  100. * Tests to see if the queue is empty.
  101. * @return True if empty.
  102. */
  103. function isEmpty() {
  104. return (count($this->_queue) == 0);
  105. }
  106. /**
  107. * Global access to a single error queue.
  108. * @return Global error queue object.
  109. * @access public
  110. * @static
  111. */
  112. function &instance() {
  113. static $queue = false;
  114. if (! $queue) {
  115. $queue = new SimpleErrorQueue();
  116. }
  117. return $queue;
  118. }
  119. /**
  120. * Converst an error code into it's string
  121. * representation.
  122. * @param $severity PHP integer error code.
  123. * @return String version of error code.
  124. * @access public
  125. * @static
  126. */
  127. function getSeverityAsString($severity) {
  128. static $map = array(
  129. E_STRICT => 'E_STRICT',
  130. E_ERROR => 'E_ERROR',
  131. E_WARNING => 'E_WARNING',
  132. E_PARSE => 'E_PARSE',
  133. E_NOTICE => 'E_NOTICE',
  134. E_CORE_ERROR => 'E_CORE_ERROR',
  135. E_CORE_WARNING => 'E_CORE_WARNING',
  136. E_COMPILE_ERROR => 'E_COMPILE_ERROR',
  137. E_COMPILE_WARNING => 'E_COMPILE_WARNING',
  138. E_USER_ERROR => 'E_USER_ERROR',
  139. E_USER_WARNING => 'E_USER_WARNING',
  140. E_USER_NOTICE => 'E_USER_NOTICE');
  141. return $map[$severity];
  142. }
  143. }
  144. /**
  145. * Error handler that simply stashes any errors into the global
  146. * error queue. Simulates the existing behaviour with respect to
  147. * logging errors, but this feature may be removed in future.
  148. * @param $severity PHP error code.
  149. * @param $message Text of error.
  150. * @param $filename File error occoured in.
  151. * @param $line Line number of error.
  152. * @param $super_globals Hash of PHP super global arrays.
  153. * @static
  154. * @access public
  155. */
  156. function simpleTestErrorHandler($severity, $message, $filename, $line, $super_globals) {
  157. if ($severity = $severity & error_reporting()) {
  158. restore_error_handler();
  159. if (ini_get('log_errors')) {
  160. $label = SimpleErrorQueue::getSeverityAsString($severity);
  161. error_log("$label: $message in $filename on line $line");
  162. }
  163. $queue = &SimpleErrorQueue::instance();
  164. $queue->add($severity, $message, $filename, $line, $super_globals);
  165. set_error_handler('simpleTestErrorHandler');
  166. }
  167. }
  168. ?>