Source for file exceptions.php

Documentation is available at exceptions.php

  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.  *    Extension that traps exceptions and turns them into
  17.  *    an error message. PHP5 only.
  18.  *    @package SimpleTest
  19.  *    @subpackage UnitTester
  20.  */
  21.  
  22.     /**
  23.      *    Stores the invoker to be wrapped.
  24.      *    @param SimpleInvoker $invoker   Test method runner.
  25.      */
  26.     function SimpleExceptionTrappingInvoker($invoker{
  27.         $this->SimpleInvokerDecorator($invoker);
  28.     }
  29.  
  30.     /**
  31.      *    Invokes a test method whilst trapping expected
  32.      *    exceptions. Any left over unthrown exceptions
  33.      *    are then reported as failures.
  34.      *    @param string $method    Test method to call.
  35.      */
  36.     function invoke($method{
  37.         $trap SimpleTest::getContext()->get('SimpleExceptionTrap');
  38.         $trap->clear();
  39.         try {
  40.             $has_thrown false;
  41.             parent::invoke($method);
  42.         catch (Exception $exception{
  43.             $has_thrown true;
  44.             if ($trap->isExpected($this->getTestCase()$exception)) {
  45.                 $this->getTestCase()->exception($exception);
  46.             }
  47.             $trap->clear();
  48.         }
  49.         if ($message $trap->getOutstanding()) {
  50.             $this->getTestCase()->fail($message);
  51.         }
  52.         if ($has_thrown{
  53.             try {
  54.                 parent::getTestCase()->tearDown();
  55.             catch (Exception $e}
  56.         }
  57.     }
  58. }
  59.  
  60. /**
  61.  *    Tests exceptions either by type or the exact
  62.  *    exception. This could be improved to accept
  63.  *    a pattern expectation to test the error
  64.  *    message, but that will have to come later.
  65.  *    @package SimpleTest
  66.  *    @subpackage UnitTester
  67.  */
  68.     private $expected;
  69.  
  70.     /**
  71.      *    Sets up the conditions to test against.
  72.      *    If the expected value is a string, then
  73.      *    it will act as a test of the class name.
  74.      *    An exception as the comparison will
  75.      *    trigger an identical match. Writing this
  76.      *    down now makes it look doubly dumb. I hope
  77.      *    come up with a better scheme later.
  78.      *    @param mixed $expected   A class name or an actual
  79.      *                              exception to compare with.
  80.      *    @param string $message   Message to display.
  81.      */
  82.     function __construct($expected$message '%s'{
  83.         $this->expected $expected;
  84.         parent::__construct($message);
  85.     }
  86.  
  87.     /**
  88.      *    Carry out the test.
  89.      *    @param Exception $compare    Value to check.
  90.      *    @return boolean              True if matched.
  91.      */
  92.     function test($compare{
  93.         if (is_string($this->expected)) {
  94.             return ($compare instanceof $this->expected);
  95.         }
  96.         if (get_class($compare!= get_class($this->expected)) {
  97.             return false;
  98.         }
  99.         return $compare->getMessage(== $this->expected->getMessage();
  100.     }
  101.  
  102.     /**
  103.      *    Create the message to display describing the test.
  104.      *    @param Exception $compare     Exception to match.
  105.      *    @return string                Final message.
  106.      */
  107.     function testMessage($compare{
  108.         if (is_string($this->expected)) {
  109.             return "Exception [" $this->describeException($compare.
  110.                     "] should be type [" $this->expected "]";
  111.         }
  112.         return "Exception [" $this->describeException($compare.
  113.                 "] should match [" .
  114.                 $this->describeException($this->expected"]";
  115.     }
  116.  
  117.     /**
  118.      *    Summary of an Exception object.
  119.      *    @param Exception $compare     Exception to describe.
  120.      *    @return string                Text description.
  121.      */
  122.     protected function describeException($exception{
  123.         return get_class($exception": " $exception->getMessage();
  124.     }
  125. }
  126.  
  127. /**
  128.  *    Stores expected exceptions for when they
  129.  *    get thrown. Saves the irritating try...catch
  130.  *    block.
  131.  *    @package  SimpleTest
  132.  *    @subpackage   UnitTester
  133.  */
  134.     private $expected;
  135.     private $message;
  136.  
  137.     /**
  138.      *    Clears down the queue ready for action.
  139.      */
  140.     function __construct({
  141.         $this->clear();
  142.     }
  143.  
  144.     /**
  145.      *    Sets up an expectation of an exception.
  146.      *    This has the effect of intercepting an
  147.      *    exception that matches.
  148.      *    @param SimpleExpectation $expected    Expected exception to match.
  149.      *    @param string $message                Message to display.
  150.      *    @access public
  151.      */
  152.     function expectException($expected false$message '%s'{
  153.         if ($expected === false{
  154.             $expected new AnythingExpectation();
  155.         }
  156.         if (SimpleExpectation::isExpectation($expected)) {
  157.             $expected new ExceptionExpectation($expected);
  158.         }
  159.         $this->expected $expected;
  160.         $this->message $message;
  161.     }
  162.  
  163.     /**
  164.      *    Compares the expected exception with any
  165.      *    in the queue. Issues a pass or fail and
  166.      *    returns the state of the test.
  167.      *    @param SimpleTestCase $test    Test case to send messages to.
  168.      *    @param Exception $exception    Exception to compare.
  169.      *    @return boolean                False on no match.
  170.      */
  171.     function isExpected($test$exception{
  172.         if ($this->expected{
  173.             return $test->assert($this->expected$exception$this->message);
  174.         }
  175.         return false;
  176.     }
  177.  
  178.     /**
  179.      *    Tests for any left over exception.
  180.      *    @return string/false     The failure message or false if none.
  181.      */
  182.     function getOutstanding({
  183.         return sprintf($this->message'Failed to trap exception');
  184.     }
  185.  
  186.     /**
  187.      *    Discards the contents of the error queue.
  188.      */
  189.     function clear({
  190.         $this->expected false;
  191.         $this->message false;
  192.     }
  193. }
  194. ?>

Documentation generated on Sun, 04 May 2008 09:21:33 -0500 by phpDocumentor 1.3.0