|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00012 if (! defined('E_STRICT')) { 00013 define('E_STRICT', 2048); 00014 } 00015 00019 require_once dirname(__FILE__) . '/invoker.php'; 00020 require_once dirname(__FILE__) . '/test_case.php'; 00021 require_once dirname(__FILE__) . '/expectation.php'; 00029 class SimpleErrorTrappingInvoker extends SimpleInvokerDecorator { 00030 00035 function SimpleErrorTrappingInvoker(&$invoker) { 00036 $this->SimpleInvokerDecorator($invoker); 00037 } 00038 00046 function invoke($method) { 00047 $queue = &$this->_createErrorQueue(); 00048 set_error_handler('SimpleTestErrorHandler'); 00049 //moodle hack start 00050 // note: this breaks PHP4 compatibility! 00051 $rethrow = null; 00052 try { 00053 parent::invoke($method); 00054 } catch (Exception $e) { 00055 $rethrow = $e; 00056 } 00057 restore_error_handler(); 00058 $queue->tally(); 00059 if ($rethrow) { 00060 throw $rethrow; 00061 } 00062 //moodle hack end 00063 } 00064 00070 function &_createErrorQueue() { 00071 $context = &SimpleTest::getContext(); 00072 $test = &$this->getTestCase(); 00073 $queue = &$context->get('SimpleErrorQueue'); 00074 $queue->setTestCase($test); 00075 return $queue; 00076 } 00077 } 00078 00085 class SimpleErrorQueue { 00086 var $_queue; 00087 var $_expectation_queue; 00088 var $_test; 00089 var $_using_expect_style = false; 00090 00094 function SimpleErrorQueue() { 00095 $this->clear(); 00096 } 00097 00102 function clear() { 00103 $this->_queue = array(); 00104 $this->_expectation_queue = array(); 00105 } 00106 00112 function setTestCase(&$test) { 00113 $this->_test = &$test; 00114 } 00115 00125 function expectError($expected, $message) { 00126 $this->_using_expect_style = true; 00127 array_push($this->_expectation_queue, array($expected, $message)); 00128 } 00129 00138 function add($severity, $content, $filename, $line) { 00139 $content = str_replace('%', '%%', $content); 00140 if ($this->_using_expect_style) { 00141 $this->_testLatestError($severity, $content, $filename, $line); 00142 } else { 00143 array_push( 00144 $this->_queue, 00145 array($severity, $content, $filename, $line)); 00146 } 00147 } 00148 00154 function tally() { 00155 while (list($severity, $message, $file, $line) = $this->extract()) { 00156 $severity = $this->getSeverityAsString($severity); 00157 $this->_test->error($severity, $message, $file, $line); 00158 } 00159 while (list($expected, $message) = $this->_extractExpectation()) { 00160 $this->_test->assert($expected, false, "%s -> Expected error not caught"); 00161 } 00162 } 00163 00173 function _testLatestError($severity, $content, $filename, $line) { 00174 if ($expectation = $this->_extractExpectation()) { 00175 list($expected, $message) = $expectation; 00176 $this->_test->assert($expected, $content, sprintf( 00177 $message, 00178 "%s -> PHP error [$content] severity [" . 00179 $this->getSeverityAsString($severity) . 00180 "] in [$filename] line [$line]")); 00181 } else { 00182 $this->_test->error($severity, $content, $filename, $line); 00183 } 00184 } 00185 00195 function extract() { 00196 if (count($this->_queue)) { 00197 return array_shift($this->_queue); 00198 } 00199 return false; 00200 } 00201 00207 function _extractExpectation() { 00208 if (count($this->_expectation_queue)) { 00209 return array_shift($this->_expectation_queue); 00210 } 00211 return false; 00212 } 00213 00217 function assertNoErrors($message) { 00218 return $this->_test->assert( 00219 new TrueExpectation(), 00220 count($this->_queue) == 0, 00221 sprintf($message, 'Should be no errors')); 00222 } 00223 00227 function assertError($expected, $message) { 00228 if (count($this->_queue) == 0) { 00229 $this->_test->fail(sprintf($message, 'Expected error not found')); 00230 return false; 00231 } 00232 list($severity, $content, $file, $line) = $this->extract(); 00233 $severity = $this->getSeverityAsString($severity); 00234 return $this->_test->assert( 00235 $expected, 00236 $content, 00237 sprintf($message, "Expected PHP error [$content] severity [$severity] in [$file] line [$line]")); 00238 } 00239 00248 function getSeverityAsString($severity) { 00249 static $map = array( 00250 E_STRICT => 'E_STRICT', 00251 E_ERROR => 'E_ERROR', 00252 E_WARNING => 'E_WARNING', 00253 E_PARSE => 'E_PARSE', 00254 E_NOTICE => 'E_NOTICE', 00255 E_CORE_ERROR => 'E_CORE_ERROR', 00256 E_CORE_WARNING => 'E_CORE_WARNING', 00257 E_COMPILE_ERROR => 'E_COMPILE_ERROR', 00258 E_COMPILE_WARNING => 'E_COMPILE_WARNING', 00259 E_USER_ERROR => 'E_USER_ERROR', 00260 E_USER_WARNING => 'E_USER_WARNING', 00261 E_USER_NOTICE => 'E_USER_NOTICE'); 00262 if (defined('E_RECOVERABLE_ERROR')) { 00263 $map[E_RECOVERABLE_ERROR] = 'E_RECOVERABLE_ERROR'; 00264 } 00265 if (defined('E_DEPRECATED')) { 00266 $map[E_DEPRECATED] = 'E_DEPRECATED'; 00267 } 00268 return $map[$severity]; 00269 } 00270 } 00271 00284 function SimpleTestErrorHandler($severity, $message, $filename = null, $line = null, $super_globals = null, $mask = null) { 00285 $severity = $severity & error_reporting(); 00286 if ($severity) { 00287 restore_error_handler(); 00288 if (ini_get('log_errors')) { 00289 $label = SimpleErrorQueue::getSeverityAsString($severity); 00290 error_log("$label: $message in $filename on line $line"); 00291 } 00292 $context = &SimpleTest::getContext(); 00293 $queue = &$context->get('SimpleErrorQueue'); 00294 $queue->add($severity, $message, $filename, $line); 00295 set_error_handler('SimpleTestErrorHandler'); 00296 } 00297 return true; 00298 } 00299 ?>