Source for file pear_test_case.php

Documentation is available at pear_test_case.php

  1. <?php
  2.     /**
  3.      *    adapter for SimpleTest to use PEAR PHPUnit test cases
  4.      *    @package    SimpleTest
  5.      *    @subpackage Extensions
  6.      *    @version    $Id: pear_test_case.php 1836 2008-12-21 00:02:26Z edwardzyang $
  7.      */
  8.     
  9.     /**#@+
  10.      * include SimpleTest files
  11.      */
  12.     require_once(dirname(__FILE__'/../dumper.php');
  13.     require_once(dirname(__FILE__'/../compatibility.php');
  14.     require_once(dirname(__FILE__'/../test_case.php');
  15.     require_once(dirname(__FILE__'/../expectation.php');
  16.     /**#@-*/
  17.    
  18.     /**
  19.      *    Adapter for PEAR PHPUnit test case to allow
  20.      *    legacy PEAR test cases to be used with SimpleTest.
  21.      *    @package      SimpleTest
  22.      *    @subpackage   Extensions
  23.      */
  24.     class PHPUnit_TestCase extends SimpleTestCase {
  25.         private $_loosely_typed;
  26.         
  27.         /**
  28.          *    Constructor. Sets the test name.
  29.          *    @param $label        Test name to display.
  30.          *    @public
  31.          */
  32.         function __construct($label false{
  33.             parent::__construct($label);
  34.             $this->_loosely_typed false;
  35.         }
  36.         
  37.         /**
  38.          *    Will test straight equality if set to loose
  39.          *    typing, or identity if not.
  40.          *    @param $first          First value.
  41.          *    @param $second         Comparison value.
  42.          *    @param $message        Message to display.
  43.          *    @public
  44.          */
  45.         function assertEquals($first$second$message "%s"$delta 0{
  46.             if ($this->_loosely_typed{
  47.                 $expectation new EqualExpectation($first);
  48.             else {
  49.                 $expectation new IdenticalExpectation($first);
  50.             }
  51.             $this->assert($expectation$second$message);
  52.         }
  53.         
  54.         /**
  55.          *    Passes if the value tested is not null.
  56.          *    @param $value          Value to test against.
  57.          *    @param $message        Message to display.
  58.          *    @public
  59.          */
  60.         function assertNotNull($value$message "%s"{
  61.             parent::assert(new TrueExpectation()isset($value)$message);
  62.         }
  63.         
  64.         /**
  65.          *    Passes if the value tested is null.
  66.          *    @param $value          Value to test against.
  67.          *    @param $message        Message to display.
  68.          *    @public
  69.          */
  70.         function assertNull($value$message "%s"{
  71.             parent::assert(new TrueExpectation()!isset($value)$message);
  72.         }
  73.         
  74.         /**
  75.          *    Identity test tests for the same object.
  76.          *    @param $first          First object handle.
  77.          *    @param $second         Hopefully the same handle.
  78.          *    @param $message        Message to display.
  79.          *    @public
  80.          */
  81.         function assertSame($first$second$message "%s"{
  82.             $dumper new SimpleDumper();
  83.             $message sprintf(
  84.                     $message,
  85.                     "[" $dumper->describeValue($first.
  86.                             "] and [" $dumper->describeValue($second.
  87.                             "] should reference the same object");
  88.             return $this->assert(
  89.                     new TrueExpectation(),
  90.                     SimpleTestCompatibility::isReference($first$second),
  91.                     $message);
  92.         }
  93.         
  94.         /**
  95.          *    Inverted identity test.
  96.          *    @param $first          First object handle.
  97.          *    @param $second         Hopefully a different handle.
  98.          *    @param $message        Message to display.
  99.          *    @public
  100.          */
  101.         function assertNotSame($first$second$message "%s"{
  102.             $dumper new SimpleDumper();
  103.             $message sprintf(
  104.                     $message,
  105.                     "[" $dumper->describeValue($first.
  106.                             "] and [" $dumper->describeValue($second.
  107.                             "] should not be the same object");
  108.             return $this->assert(
  109.                     new falseExpectation(),
  110.                     SimpleTestCompatibility::isReference($first$second),
  111.                     $message);
  112.         }
  113.         
  114.         /**
  115.          *    Sends pass if the test condition resolves true,
  116.          *    a fail otherwise.
  117.          *    @param $condition      Condition to test true.
  118.          *    @param $message        Message to display.
  119.          *    @public
  120.          */
  121.         function assertTrue($condition$message "%s"{
  122.             parent::assert(new TrueExpectation()$condition$message);
  123.         }
  124.         
  125.         /**
  126.          *    Sends pass if the test condition resolves false,
  127.          *    a fail otherwise.
  128.          *    @param $condition      Condition to test false.
  129.          *    @param $message        Message to display.
  130.          *    @public
  131.          */
  132.         function assertFalse($condition$message "%s"{
  133.             parent::assert(new FalseExpectation()$condition$message);
  134.         }
  135.         
  136.         /**
  137.          *    Tests a regex match. Needs refactoring.
  138.          *    @param $pattern        Regex to match.
  139.          *    @param $subject        String to search in.
  140.          *    @param $message        Message to display.
  141.          *    @public
  142.          */
  143.         function assertRegExp($pattern$subject$message "%s"{
  144.             $this->assert(new PatternExpectation($pattern)$subject$message);
  145.         }
  146.         
  147.         /**
  148.          *    Tests the type of a value.
  149.          *    @param $value          Value to take type of.
  150.          *    @param $type           Hoped for type.
  151.          *    @param $message        Message to display.
  152.          *    @public
  153.          */
  154.         function assertType($value$type$message "%s"{
  155.             parent::assert(new TrueExpectation()gettype($value== strtolower($type)$message);
  156.         }
  157.         
  158.         /**
  159.          *    Sets equality operation to act as a simple equal
  160.          *    comparison only, allowing a broader range of
  161.          *    matches.
  162.          *    @param $loosely_typed     True for broader comparison.
  163.          *    @public
  164.          */
  165.         function setLooselyTyped($loosely_typed{
  166.             $this->_loosely_typed $loosely_typed;
  167.         }
  168.  
  169.         /**
  170.          *    For progress indication during
  171.          *    a test amongst other things.
  172.          *    @return            Usually one.
  173.          *    @public
  174.          */
  175.         function countTestCases({
  176.             return $this->getSize();
  177.         }
  178.         
  179.         /**
  180.          *    Accessor for name, normally just the class
  181.          *    name.
  182.          *    @public
  183.          */
  184.         function getName({
  185.             return $this->getLabel();
  186.         }
  187.         
  188.         /**
  189.          *    Does nothing. For compatibility only.
  190.          *    @param $name        Dummy
  191.          *    @public
  192.          */
  193.         function setName($name{
  194.         }
  195.     }
  196. ?>

Documentation generated on Thu, 01 Oct 2009 20:55:17 -0500 by phpDocumentor 1.4.2