3
* base include file for SimpleTest
5
* @subpackage UnitTester
6
* @version $Id: exceptions.php 1672 2008-03-02 04:47:34Z edwardzyang $
10
* Include required SimpleTest files
12
require_once dirname(__FILE__) . '/invoker.php';
13
require_once dirname(__FILE__) . '/expectation.php';
17
* Extension that traps exceptions and turns them into
18
* an error message. PHP5 only.
20
* @subpackage UnitTester
22
class SimpleExceptionTrappingInvoker extends SimpleInvokerDecorator {
25
* Stores the invoker to be wrapped.
26
* @param SimpleInvoker $invoker Test method runner.
28
function SimpleExceptionTrappingInvoker($invoker) {
29
$this->SimpleInvokerDecorator($invoker);
33
* Invokes a test method whilst trapping expected
34
* exceptions. Any left over unthrown exceptions
35
* are then reported as failures.
36
* @param string $method Test method to call.
38
function invoke($method) {
39
$trap = SimpleTest::getContext()->get('SimpleExceptionTrap');
43
parent::invoke($method);
44
} catch (Exception $exception) {
46
if (! $trap->isExpected($this->getTestCase(), $exception)) {
47
$this->getTestCase()->exception($exception);
51
if ($message = $trap->getOutstanding()) {
52
$this->getTestCase()->fail($message);
56
parent::getTestCase()->tearDown();
57
} catch (Exception $e) { }
63
* Tests exceptions either by type or the exact
64
* exception. This could be improved to accept
65
* a pattern expectation to test the error
66
* message, but that will have to come later.
68
* @subpackage UnitTester
70
class ExceptionExpectation extends SimpleExpectation {
74
* Sets up the conditions to test against.
75
* If the expected value is a string, then
76
* it will act as a test of the class name.
77
* An exception as the comparison will
78
* trigger an identical match. Writing this
79
* down now makes it look doubly dumb. I hope
80
* come up with a better scheme later.
81
* @param mixed $expected A class name or an actual
82
* exception to compare with.
83
* @param string $message Message to display.
85
function __construct($expected, $message = '%s') {
86
$this->expected = $expected;
87
parent::__construct($message);
92
* @param Exception $compare Value to check.
93
* @return boolean True if matched.
95
function test($compare) {
96
if (is_string($this->expected)) {
97
return ($compare instanceof $this->expected);
99
if (get_class($compare) != get_class($this->expected)) {
102
return $compare->getMessage() == $this->expected->getMessage();
106
* Create the message to display describing the test.
107
* @param Exception $compare Exception to match.
108
* @return string Final message.
110
function testMessage($compare) {
111
if (is_string($this->expected)) {
112
return "Exception [" . $this->describeException($compare) .
113
"] should be type [" . $this->expected . "]";
115
return "Exception [" . $this->describeException($compare) .
117
$this->describeException($this->expected) . "]";
121
* Summary of an Exception object.
122
* @param Exception $compare Exception to describe.
123
* @return string Text description.
125
protected function describeException($exception) {
126
return get_class($exception) . ": " . $exception->getMessage();
131
* Stores expected exceptions for when they
132
* get thrown. Saves the irritating try...catch
134
* @package SimpleTest
135
* @subpackage UnitTester
137
class SimpleExceptionTrap {
142
* Clears down the queue ready for action.
144
function __construct() {
149
* Sets up an expectation of an exception.
150
* This has the effect of intercepting an
151
* exception that matches.
152
* @param SimpleExpectation $expected Expected exception to match.
153
* @param string $message Message to display.
156
function expectException($expected = false, $message = '%s') {
157
if ($expected === false) {
158
$expected = new AnythingExpectation();
160
if (! SimpleExpectation::isExpectation($expected)) {
161
$expected = new ExceptionExpectation($expected);
163
$this->expected = $expected;
164
$this->message = $message;
168
* Compares the expected exception with any
169
* in the queue. Issues a pass or fail and
170
* returns the state of the test.
171
* @param SimpleTestCase $test Test case to send messages to.
172
* @param Exception $exception Exception to compare.
173
* @return boolean False on no match.
175
function isExpected($test, $exception) {
176
if ($this->expected) {
177
return $test->assert($this->expected, $exception, $this->message);
183
* Tests for any left over exception.
184
* @return string/false The failure message or false if none.
186
function getOutstanding() {
187
return sprintf($this->message, 'Failed to trap exception');
191
* Discards the contents of the error queue.
194
$this->expected = false;
195
$this->message = false;
b'\\ No newline at end of file'