|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00012 require_once dirname(__FILE__) . '/invoker.php'; 00013 require_once dirname(__FILE__) . '/expectation.php'; 00022 class SimpleExceptionTrappingInvoker extends SimpleInvokerDecorator { 00023 00028 function SimpleExceptionTrappingInvoker($invoker) { 00029 $this->SimpleInvokerDecorator($invoker); 00030 } 00031 00038 function invoke($method) { 00039 $trap = SimpleTest::getContext()->get('SimpleExceptionTrap'); 00040 $trap->clear(); 00041 try { 00042 $has_thrown = false; 00043 parent::invoke($method); 00044 } catch (Exception $exception) { 00045 $has_thrown = true; 00046 if (! $trap->isExpected($this->getTestCase(), $exception)) { 00047 $this->getTestCase()->exception($exception); 00048 } 00049 $trap->clear(); 00050 } 00051 if ($message = $trap->getOutstanding()) { 00052 $this->getTestCase()->fail($message); 00053 } 00054 if ($has_thrown) { 00055 try { 00056 parent::getTestCase()->tearDown(); 00057 } catch (Exception $e) { } 00058 } 00059 } 00060 } 00061 00070 class ExceptionExpectation extends SimpleExpectation { 00071 private $expected; 00072 00085 function __construct($expected, $message = '%s') { 00086 $this->expected = $expected; 00087 parent::__construct($message); 00088 } 00089 00095 function test($compare) { 00096 if (is_string($this->expected)) { 00097 return ($compare instanceof $this->expected); 00098 } 00099 if (get_class($compare) != get_class($this->expected)) { 00100 return false; 00101 } 00102 return $compare->getMessage() == $this->expected->getMessage(); 00103 } 00104 00110 function testMessage($compare) { 00111 if (is_string($this->expected)) { 00112 return "Exception [" . $this->describeException($compare) . 00113 "] should be type [" . $this->expected . "]"; 00114 } 00115 return "Exception [" . $this->describeException($compare) . 00116 "] should match [" . 00117 $this->describeException($this->expected) . "]"; 00118 } 00119 00125 protected function describeException($exception) { 00126 return get_class($exception) . ": " . $exception->getMessage(); 00127 } 00128 } 00129 00137 class SimpleExceptionTrap { 00138 private $expected; 00139 private $message; 00140 00144 function __construct() { 00145 $this->clear(); 00146 } 00147 00156 function expectException($expected = false, $message = '%s') { 00157 if ($expected === false) { 00158 $expected = new AnythingExpectation(); 00159 } 00160 if (! SimpleExpectation::isExpectation($expected)) { 00161 $expected = new ExceptionExpectation($expected); 00162 } 00163 $this->expected = $expected; 00164 $this->message = $message; 00165 } 00166 00175 function isExpected($test, $exception) { 00176 if ($this->expected) { 00177 return $test->assert($this->expected, $exception, $this->message); 00178 } 00179 return false; 00180 } 00181 00186 function getOutstanding() { 00187 return sprintf($this->message, 'Failed to trap exception'); 00188 } 00189 00193 function clear() { 00194 $this->expected = false; 00195 $this->message = false; 00196 } 00197 } 00198 ?>