PHPUnit2
[ class tree: PHPUnit2 ] [ index: PHPUnit2 ] [ all elements ]

Source for file ExceptionTestCase.php

Documentation is available at ExceptionTestCase.php

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit2                                                       |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
  7. // +------------------------------------------------------------------------+
  8. // | This source file is subject to version 3.00 of the PHP License,        |
  9. // | that is available at http://www.php.net/license/3_0.txt.               |
  10. // | If you did not receive a copy of the PHP license and are unable to     |
  11. // | obtain it through the world-wide-web, please send a note to            |
  12. // | license@php.net so we can mail you a copy immediately.                 |
  13. // +------------------------------------------------------------------------+
  14. //
  15. // $Id: ExceptionTestCase.php,v 1.8.2.1 2004/12/22 08:06:06 sebastian Exp $
  16. //
  17.  
  18. require_once 'PHPUnit2/Framework/TestCase.php';
  19.  
  20. /**
  21.  * A TestCase that expects an Exception of class
  22.  * fExpected to be thrown.
  23.  *
  24.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  25.  * @copyright   Copyright &copy; 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
  26.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  27.  * @category    Testing
  28.  * @package     PHPUnit2
  29.  * @subpackage  Extensions
  30.  */
  31.     // {{{ Instance Variables
  32.  
  33.     /**
  34.     * The name of the expected Exception.
  35.     *
  36.     * @var    string 
  37.     * @access private
  38.     */
  39.     private $expectedException '';
  40.  
  41.     // }}}
  42.     // {{{ public function __construct($name, $exceptionName = '')
  43.  
  44.     /**
  45.     * @param  string  $name 
  46.     * @param  string  $exceptionName 
  47.     * @access public
  48.     */
  49.     public function __construct($name$exceptionName ''{
  50.         parent::__construct($name);
  51.  
  52.         if (!empty($exceptionName)) {
  53.             $this->setExpectedException($exceptionName);
  54.         }
  55.     }
  56.  
  57.     // }}}
  58.     // {{{ public function getExpectedException()
  59.  
  60.     /**
  61.     * @return string 
  62.     * @access public
  63.     * @since  2.2.0
  64.     */
  65.     public function getExpectedException({
  66.         return $this->expectedException;
  67.     }
  68.  
  69.     // }}}
  70.     // {{{ public function setExpectedException($exceptionName)
  71.  
  72.     /**
  73.     * @param  string  $exceptionName 
  74.     * @access public
  75.     * @since  2.2.0
  76.     */
  77.     public function setExpectedException($exceptionName{
  78.         if (is_string($exceptionName&& class_exists($exceptionName)) {
  79.             $this->expectedException $exceptionName;
  80.         else {
  81.             throw new Exception(
  82.               sprintf(
  83.                 'Exception %s does not exist.',
  84.                 $exceptionName
  85.               )
  86.             );
  87.         }
  88.     }
  89.  
  90.     // }}}
  91.     // {{{ protected function runTest()
  92.  
  93.     /**
  94.     * @access protected
  95.     */
  96.     protected function runTest({
  97.         try {
  98.             parent::runTest();
  99.         }
  100.  
  101.         catch (Exception $e{
  102.             if ($e instanceof $this->expectedException{
  103.                 return;
  104.             else {
  105.                 throw $e;
  106.             }
  107.         }
  108.  
  109.         $this->fail('Expected exception ' $this->expectedException);
  110.     }
  111.  
  112.     // }}}
  113. }
  114.  
  115. /*
  116.  * vim600:  et sw=2 ts=2 fdm=marker
  117.  * vim<600: et sw=2 ts=2
  118.  */
  119. ?>

Documentation generated on Mon, 11 Mar 2019 14:19:18 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.