123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- require_once dirname(__FILE__) . '/invoker.php';
- require_once dirname(__FILE__) . '/expectation.php';
- class SimpleExceptionTrappingInvoker extends SimpleInvokerDecorator {
-
- function SimpleExceptionTrappingInvoker($invoker) {
- $this->SimpleInvokerDecorator($invoker);
- }
-
- function invoke($method) {
- $trap = SimpleTest::getContext()->get('SimpleExceptionTrap');
- $trap->clear();
- try {
- $has_thrown = false;
- parent::invoke($method);
- } catch (Exception $exception) {
- $has_thrown = true;
- if (! $trap->isExpected($this->getTestCase(), $exception)) {
- $this->getTestCase()->exception($exception);
- }
- $trap->clear();
- }
- if ($message = $trap->getOutstanding()) {
- $this->getTestCase()->fail($message);
- }
- if ($has_thrown) {
- try {
- parent::getTestCase()->tearDown();
- } catch (Exception $e) { }
- }
- }
- }
- class ExceptionExpectation extends SimpleExpectation {
- private $expected;
-
- function __construct($expected, $message = '%s') {
- $this->expected = $expected;
- parent::__construct($message);
- }
-
- function test($compare) {
- if (is_string($this->expected)) {
- return ($compare instanceof $this->expected);
- }
- if (get_class($compare) != get_class($this->expected)) {
- return false;
- }
- return $compare->getMessage() == $this->expected->getMessage();
- }
-
- function testMessage($compare) {
- if (is_string($this->expected)) {
- return "Exception [" . $this->describeException($compare) .
- "] should be type [" . $this->expected . "]";
- }
- return "Exception [" . $this->describeException($compare) .
- "] should match [" .
- $this->describeException($this->expected) . "]";
- }
-
- protected function describeException($exception) {
- return get_class($exception) . ": " . $exception->getMessage();
- }
- }
- class SimpleExceptionTrap {
- private $expected;
- private $message;
-
- function __construct() {
- $this->clear();
- }
-
- function expectException($expected = false, $message = '%s') {
- if ($expected === false) {
- $expected = new AnythingExpectation();
- }
- if (! SimpleExpectation::isExpectation($expected)) {
- $expected = new ExceptionExpectation($expected);
- }
- $this->expected = $expected;
- $this->message = $message;
- }
-
- function isExpected($test, $exception) {
- if ($this->expected) {
- return $test->assert($this->expected, $exception, $this->message);
- }
- return false;
- }
-
- function getOutstanding() {
- return sprintf($this->message, 'Failed to trap exception');
- }
-
- function clear() {
- $this->expected = false;
- $this->message = false;
- }
- }
- ?>
|