errors.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id$
  7. */
  8. /**#@+
  9. * Includes SimpleTest files.
  10. */
  11. require_once dirname(__FILE__) . '/invoker.php';
  12. require_once dirname(__FILE__) . '/test_case.php';
  13. require_once dirname(__FILE__) . '/expectation.php';
  14. /**#@-*/
  15. /**
  16. * Extension that traps errors into an error queue.
  17. * @package SimpleTest
  18. * @subpackage UnitTester
  19. */
  20. class SimpleErrorTrappingInvoker extends SimpleInvokerDecorator {
  21. /**
  22. * Stores the invoker to wrap.
  23. * @param SimpleInvoker $invoker Test method runner.
  24. */
  25. function __construct($invoker) {
  26. parent::__construct($invoker);
  27. }
  28. /**
  29. * Invokes a test method and dispatches any
  30. * untrapped errors. Called back from
  31. * the visiting runner.
  32. * @param string $method Test method to call.
  33. * @access public
  34. */
  35. function invoke($method) {
  36. $queue = $this->createErrorQueue();
  37. set_error_handler('SimpleTestErrorHandler');
  38. parent::invoke($method);
  39. restore_error_handler();
  40. $queue->tally();
  41. }
  42. /**
  43. * Wires up the error queue for a single test.
  44. * @return SimpleErrorQueue Queue connected to the test.
  45. * @access private
  46. */
  47. protected function createErrorQueue() {
  48. $context = SimpleTest::getContext();
  49. $test = $this->getTestCase();
  50. $queue = $context->get('SimpleErrorQueue');
  51. $queue->setTestCase($test);
  52. return $queue;
  53. }
  54. }
  55. /**
  56. * Error queue used to record trapped
  57. * errors.
  58. * @package SimpleTest
  59. * @subpackage UnitTester
  60. */
  61. class SimpleErrorQueue {
  62. private $queue;
  63. private $expectation_queue;
  64. private $test;
  65. private $using_expect_style = false;
  66. /**
  67. * Starts with an empty queue.
  68. */
  69. function __construct() {
  70. $this->clear();
  71. }
  72. /**
  73. * Discards the contents of the error queue.
  74. * @access public
  75. */
  76. function clear() {
  77. $this->queue = array();
  78. $this->expectation_queue = array();
  79. }
  80. /**
  81. * Sets the currently running test case.
  82. * @param SimpleTestCase $test Test case to send messages to.
  83. * @access public
  84. */
  85. function setTestCase($test) {
  86. $this->test = $test;
  87. }
  88. /**
  89. * Sets up an expectation of an error. If this is
  90. * not fulfilled at the end of the test, a failure
  91. * will occour. If the error does happen, then this
  92. * will cancel it out and send a pass message.
  93. * @param SimpleExpectation $expected Expected error match.
  94. * @param string $message Message to display.
  95. * @access public
  96. */
  97. function expectError($expected, $message) {
  98. array_push($this->expectation_queue, array($expected, $message));
  99. }
  100. /**
  101. * Adds an error to the front of the queue.
  102. * @param integer $severity PHP error code.
  103. * @param string $content Text of error.
  104. * @param string $filename File error occoured in.
  105. * @param integer $line Line number of error.
  106. * @access public
  107. */
  108. function add($severity, $content, $filename, $line) {
  109. $content = str_replace('%', '%%', $content);
  110. $this->testLatestError($severity, $content, $filename, $line);
  111. }
  112. /**
  113. * Any errors still in the queue are sent to the test
  114. * case. Any unfulfilled expectations trigger failures.
  115. * @access public
  116. */
  117. function tally() {
  118. while (list($severity, $message, $file, $line) = $this->extract()) {
  119. $severity = $this->getSeverityAsString($severity);
  120. $this->test->error($severity, $message, $file, $line);
  121. }
  122. while (list($expected, $message) = $this->extractExpectation()) {
  123. $this->test->assert($expected, false, "%s -> Expected error not caught");
  124. }
  125. }
  126. /**
  127. * Tests the error against the most recent expected
  128. * error.
  129. * @param integer $severity PHP error code.
  130. * @param string $content Text of error.
  131. * @param string $filename File error occoured in.
  132. * @param integer $line Line number of error.
  133. * @access private
  134. */
  135. protected function testLatestError($severity, $content, $filename, $line) {
  136. if ($expectation = $this->extractExpectation()) {
  137. list($expected, $message) = $expectation;
  138. $this->test->assert($expected, $content, sprintf(
  139. $message,
  140. "%s -> PHP error [$content] severity [" .
  141. $this->getSeverityAsString($severity) .
  142. "] in [$filename] line [$line]"));
  143. } else {
  144. $this->test->error($severity, $content, $filename, $line);
  145. }
  146. }
  147. /**
  148. * Pulls the earliest error from the queue.
  149. * @return mixed False if none, or a list of error
  150. * information. Elements are: severity
  151. * as the PHP error code, the error message,
  152. * the file with the error, the line number
  153. * and a list of PHP super global arrays.
  154. * @access public
  155. */
  156. function extract() {
  157. if (count($this->queue)) {
  158. return array_shift($this->queue);
  159. }
  160. return false;
  161. }
  162. /**
  163. * Pulls the earliest expectation from the queue.
  164. * @return SimpleExpectation False if none.
  165. * @access private
  166. */
  167. protected function extractExpectation() {
  168. if (count($this->expectation_queue)) {
  169. return array_shift($this->expectation_queue);
  170. }
  171. return false;
  172. }
  173. /**
  174. * Converts an error code into it's string
  175. * representation.
  176. * @param $severity PHP integer error code.
  177. * @return String version of error code.
  178. * @access public
  179. */
  180. static function getSeverityAsString($severity) {
  181. static $map = array(
  182. E_STRICT => 'E_STRICT',
  183. E_ERROR => 'E_ERROR',
  184. E_WARNING => 'E_WARNING',
  185. E_PARSE => 'E_PARSE',
  186. E_NOTICE => 'E_NOTICE',
  187. E_CORE_ERROR => 'E_CORE_ERROR',
  188. E_CORE_WARNING => 'E_CORE_WARNING',
  189. E_COMPILE_ERROR => 'E_COMPILE_ERROR',
  190. E_COMPILE_WARNING => 'E_COMPILE_WARNING',
  191. E_USER_ERROR => 'E_USER_ERROR',
  192. E_USER_WARNING => 'E_USER_WARNING',
  193. E_USER_NOTICE => 'E_USER_NOTICE');
  194. if (defined('E_RECOVERABLE_ERROR')) {
  195. $map[E_RECOVERABLE_ERROR] = 'E_RECOVERABLE_ERROR';
  196. }
  197. if (defined('E_DEPRECATED')) {
  198. $map[E_DEPRECATED] = 'E_DEPRECATED';
  199. $map[E_USER_DEPRECATED] = 'E_USER_DEPRECATED';
  200. }
  201. return $map[$severity];
  202. }
  203. }
  204. /**
  205. * Error handler that simply stashes any errors into the global
  206. * error queue. Simulates the existing behaviour with respect to
  207. * logging errors, but this feature may be removed in future.
  208. * @param $severity PHP error code.
  209. * @param $message Text of error.
  210. * @param $filename File error occoured in.
  211. * @param $line Line number of error.
  212. * @param $super_globals Hash of PHP super global arrays.
  213. * @access public
  214. */
  215. function SimpleTestErrorHandler($severity, $message, $filename = null, $line = null, $super_globals = null, $mask = null) {
  216. $severity = $severity & error_reporting();
  217. if ($severity) {
  218. restore_error_handler();
  219. if (IsNotCausedBySimpleTest($message) && IsNotTimeZoneNag($message)) {
  220. if (ini_get('log_errors')) {
  221. $label = SimpleErrorQueue::getSeverityAsString($severity);
  222. error_log("$label: $message in $filename on line $line");
  223. }
  224. $queue = SimpleTest::getContext()->get('SimpleErrorQueue');
  225. $queue->add($severity, $message, $filename, $line);
  226. }
  227. set_error_handler('SimpleTestErrorHandler');
  228. }
  229. return true;
  230. }
  231. /**
  232. * Certain messages can be caused by the unit tester itself.
  233. * These have to be filtered.
  234. * @param string $message Message to filter.
  235. * @return boolean True if genuine failure.
  236. */
  237. function IsNotCausedBySimpleTest($message) {
  238. return ! preg_match('/returned by reference/', $message);
  239. }
  240. /**
  241. * Certain messages caused by PHP are just noise.
  242. * These have to be filtered.
  243. * @param string $message Message to filter.
  244. * @return boolean True if genuine failure.
  245. */
  246. function IsNotTimeZoneNag($message) {
  247. return ! preg_match('/not safe to rely .* timezone settings/', $message);
  248. }
  249. ?>