[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/tests_runner/lib/simpletest/ -> exceptions.php (source)

   1  <?php
   2  /**

   3   *    base include file for SimpleTest

   4   *    @package    SimpleTest

   5   *    @subpackage    UnitTester

   6   *    @version    $Id: exceptions.php 5999 2007-06-18 13:13:08Z pachanga $

   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) {
  29          $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) {
  39          $trap = SimpleTest::getContext()->get('SimpleExceptionTrap');
  40          $trap->clear();
  41          try {
  42              parent::invoke($method);
  43          } catch (Exception $exception) {
  44              if (! $trap->isExpected($this->getTestCase(), $exception)) {
  45                  $this->getTestCase()->exception($exception);
  46              }
  47              $trap->clear();
  48              $this->_invoker->getTestCase()->tearDown();
  49          }
  50          if ($message = $trap->getOutstanding()) {
  51              $this->getTestCase()->fail($message);
  52          }
  53      }
  54  }
  55  
  56  /**

  57   *    Tests exceptions either by type or the exact

  58   *    exception. This could be improved to accept

  59   *    a pattern expectation to test the error

  60   *    message, but that will have to come later.

  61   *      @package SimpleTest

  62   *      @subpackage UnitTester

  63   */
  64  class ExceptionExpectation extends SimpleExpectation {
  65      private $expected;
  66  
  67      /**

  68       *    Sets up the conditions to test against.

  69       *    If the expected value is a string, then

  70       *    it will act as a test of the class name.

  71       *    An exception as the comparison will

  72       *    trigger an identical match. Writing this

  73       *    down now makes it look doubly dumb. I hope

  74       *    come up with a better scheme later.

  75       *    @param mixed $expected   A class name or an actual

  76       *                             exception to compare with.

  77       *    @param string $message   Message to display.

  78       */
  79  	function __construct($expected, $message = '%s') {
  80          $this->expected = $expected;
  81          parent::__construct($message);
  82      }
  83  
  84      /**

  85       *    Carry out the test.

  86       *    @param Exception $compare    Value to check.

  87       *    @return boolean              True if matched.

  88       */
  89  	function test($compare) {
  90          if (is_string($this->expected)) {
  91              return ($compare instanceof $this->expected);
  92          }
  93          if (get_class($compare) != get_class($this->expected)) {
  94              return false;
  95          }
  96          return $compare->getMessage() == $this->expected->getMessage();
  97      }
  98  
  99      /**

 100       *    Create the message to display describing the test.

 101       *    @param Exception $compare     Exception to match.

 102       *    @return string                Final message.

 103       */
 104  	function testMessage($compare) {
 105          if (is_string($this->expected)) {
 106              return "Exception [" . $this->describeException($compare) .
 107                      "] should be type [" . $this->expected . "]";
 108          }
 109          return "Exception [" . $this->describeException($compare) .
 110                  "] should match [" .
 111                  $this->describeException($this->expected) . "]";
 112      }
 113  
 114      /**

 115       *    Summary of an Exception object.

 116       *    @param Exception $compare     Exception to describe.

 117       *    @return string                Text description.

 118       */
 119  	protected function describeException($exception) {
 120          return get_class($exception) . ": " . $exception->getMessage();
 121      }
 122  }
 123  
 124  /**

 125   *    Stores expected exceptions for when they

 126   *    get thrown. Saves the irritating try...catch

 127   *    block.

 128   *      @package    SimpleTest

 129   *      @subpackage    UnitTester

 130   */
 131  class SimpleExceptionTrap {
 132      private $expected;
 133      private $message;
 134  
 135      /**

 136       *    Clears down the queue ready for action.

 137       */
 138  	function __construct() {
 139          $this->clear();
 140      }
 141  
 142      /**

 143       *    Sets up an expectation of an exception.

 144       *    This has the effect of intercepting an

 145       *    exception that matches.

 146       *    @param SimpleExpectation $expected    Expected exception to match.

 147       *    @param string $message                Message to display.

 148       *    @access public

 149       */
 150  	function expectException($expected = false, $message = '%s') {
 151          if ($expected === false) {
 152              $expected = new AnythingExpectation();
 153          }
 154          if (! SimpleExpectation::isExpectation($expected)) {
 155              $expected = new ExceptionExpectation($expected);
 156          }
 157          $this->expected = $expected;
 158          $this->message = $message;
 159      }
 160  
 161      /**

 162       *    Compares the expected exception with any

 163       *    in the queue. Issues a pass or fail and

 164       *    returns the state of the test.

 165       *    @param SimpleTestCase $test    Test case to send messages to.

 166       *    @param Exception $exception    Exception to compare.

 167       *    @return boolean                False on no match.

 168       */
 169  	function isExpected($test, $exception) {
 170          if ($this->expected) {
 171              return $test->assert($this->expected, $exception, $this->message);
 172          }
 173          return false;
 174      }
 175  
 176      /**

 177       *    Tests for any left over exception.

 178       *    @return string/false     The failure message or false if none.

 179       */
 180  	function getOutstanding() {
 181          return sprintf($this->message, 'Failed to trap exception');
 182      }
 183  
 184      /**

 185       *    Discards the contents of the error queue.

 186       */
 187  	function clear() {
 188          $this->expected = false;
 189          $this->message = false;
 190      }
 191  }
 192  ?>


Generated: Sat Nov 22 03:48:54 2008 Cross-referenced by PHPXref 0.7