PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/simpletest/errors.php

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