Spike PHPCoverage Details: exceptions.php

Line #FrequencySource Line
1 <?php
2 /**
3  *  base include file for SimpleTest
4  *  @package    SimpleTest
5  *  @subpackage UnitTester
6  *  @version    $Id: exceptions.php 1672 2008-03-02 04:47:34Z edwardzyang $
7  */
8 
9 /**#@+
10  * Include required SimpleTest files 
11  */
12 require_once dirname(__FILE__) . '/invoker.php';
13 require_once dirname(__FILE__) . '/expectation.php';
14 /**#@-*/
15 
16 /**
17  *    Extension that traps exceptions and turns them into
18  *    an error message. PHP5 only.
19  *    @package SimpleTest
20  *    @subpackage UnitTester
21  */
22 class SimpleExceptionTrappingInvoker extends SimpleInvokerDecorator {
23 
24     /**
25      *    Stores the invoker to be wrapped.
26      *    @param SimpleInvoker $invoker   Test method runner.
27      */
28     function SimpleExceptionTrappingInvoker($invoker) {
291        $this->SimpleInvokerDecorator($invoker);
30     }
31 
32     /**
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.
37      */
38     function invoke($method) {
391        $trap = SimpleTest::getContext()->get('SimpleExceptionTrap');
401        $trap->clear();
41         try {
421            $has_thrown = false;
431            parent::invoke($method);
441        } catch (Exception $exception) {
45             $has_thrown = true;
46             if (! $trap->isExpected($this->getTestCase(), $exception)) {
47                 $this->getTestCase()->exception($exception);
48             }
49             $trap->clear();
50         }
511        if ($message = $trap->getOutstanding()) {
52             $this->getTestCase()->fail($message);
53         }
541        if ($has_thrown) {
55             try {
56                 parent::getTestCase()->tearDown();
57             } catch (Exception $e) { }
58         }
59     }
60 }
61 
62 /**
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.
67  *    @package SimpleTest
68  *    @subpackage UnitTester
69  */
70 class ExceptionExpectation extends SimpleExpectation {
71     private $expected;
72 
73     /**
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.
84      */
85     function __construct($expected, $message = '%s') {
86         $this->expected = $expected;
87         parent::__construct($message);
88     }
89 
90     /**
91      *    Carry out the test.
92      *    @param Exception $compare    Value to check.
93      *    @return boolean              True if matched.
94      */
95     function test($compare) {
96         if (is_string($this->expected)) {
97             return ($compare instanceof $this->expected);
98         }
99         if (get_class($compare) != get_class($this->expected)) {
100             return false;
101         }
102         return $compare->getMessage() == $this->expected->getMessage();
103     }
104 
105     /**
106      *    Create the message to display describing the test.
107      *    @param Exception $compare     Exception to match.
108      *    @return string                Final message.
109      */
110     function testMessage($compare) {
111         if (is_string($this->expected)) {
112             return "Exception [" . $this->describeException($compare) .
113                     "] should be type [" . $this->expected . "]";
114         }
115         return "Exception [" . $this->describeException($compare) .
116                 "] should match [" .
117                 $this->describeException($this->expected) . "]";
118     }
119 
120     /**
121      *    Summary of an Exception object.
122      *    @param Exception $compare     Exception to describe.
123      *    @return string                Text description.
124      */
125     protected function describeException($exception) {
126         return get_class($exception) . ": " . $exception->getMessage();
127     }
128 }
129 
130 /**
131  *    Stores expected exceptions for when they
132  *    get thrown. Saves the irritating try...catch
133  *    block.
134  *    @package  SimpleTest
135  *    @subpackage   UnitTester
136  */
137 class SimpleExceptionTrap {
138     private $expected;
139     private $message;
140 
141     /**
142      *    Clears down the queue ready for action.
143      */
144     function __construct() {
1451        $this->clear();
146     }
147 
148     /**
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.
154      *    @access public
155      */
156     function expectException($expected = false, $message = '%s') {
157         if ($expected === false) {
158             $expected = new AnythingExpectation();
159         }
160         if (! SimpleExpectation::isExpectation($expected)) {
161             $expected = new ExceptionExpectation($expected);
162         }
163         $this->expected = $expected;
164         $this->message = $message;
165     }
166 
167     /**
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.
174      */
175     function isExpected($test, $exception) {
176         if ($this->expected) {
177             return $test->assert($this->expected, $exception, $this->message);
178         }
179         return false;
180     }
181 
182     /**
183      *    Tests for any left over exception.
184      *    @return string/false     The failure message or false if none.
185      */
186     function getOutstanding() {
1871        return sprintf($this->message, 'Failed to trap exception');
188     }
189 
190     /**
191      *    Discards the contents of the error queue.
192      */
193     function clear() {
1941        $this->expected = false;
1951        $this->message = false;
196     }
197 }
198 ?