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

Documentation generated on Sun, 31 Oct 2010 16:31:23 -0500 by phpDocumentor 1.4.3