Source for file unit_tester.php

Documentation is available at unit_tester.php

  1. <?php
  2. /**
  3.  *  base include file for SimpleTest
  4.  *  @package    SimpleTest
  5.  *  @subpackage UnitTester
  6.  *  @version    $Id: unit_tester.php 1723 2008-04-08 00:34:10Z lastcraft $
  7.  */
  8.  
  9. /**#@+
  10.  *  include other SimpleTest class files
  11.  */
  12. require_once(dirname(__FILE__'/test_case.php');
  13. require_once(dirname(__FILE__'/dumper.php');
  14. /**#@-*/
  15.  
  16.  *    Standard unit test class for day to day testing
  17.  *    of PHP code XP style. Adds some useful standard
  18.  *    assertions.
  19.  *    @package  SimpleTest
  20.  *    @subpackage   UnitTester
  21.  */
  22. class UnitTestCase extends SimpleTestCase {
  23.  
  24.     /**
  25.      *    Creates an empty test case. Should be subclassed
  26.      *    with test methods for a functional test case.
  27.      *    @param string $label     Name of test case. Will use
  28.      *                              the class name if none specified.
  29.      *    @access public
  30.      */
  31.     function UnitTestCase($label false{
  32.         if ($label{
  33.             $label get_class($this);
  34.         }
  35.         $this->SimpleTestCase($label);
  36.     }
  37.  
  38.     /**
  39.      *    Called from within the test methods to register
  40.      *    passes and failures.
  41.      *    @param boolean $result    Pass on true.
  42.      *    @param string $message    Message to display describing
  43.      *                               the test state.
  44.      *    @return boolean           True on pass
  45.      *    @access public
  46.      */
  47.     function assertTrue($result$message false{
  48.         return $this->assert(new TrueExpectation()$result$message);
  49.     }
  50.  
  51.     /**
  52.      *    Will be true on false and vice versa. False
  53.      *    is the PHP definition of false, so that null,
  54.      *    empty strings, zero and an empty array all count
  55.      *    as false.
  56.      *    @param boolean $result    Pass on false.
  57.      *    @param string $message    Message to display.
  58.      *    @return boolean           True on pass
  59.      *    @access public
  60.      */
  61.     function assertFalse($result$message '%s'{
  62.         return $this->assert(new FalseExpectation()$result$message);
  63.     }
  64.  
  65.     /**
  66.      *    Will be true if the value is null.
  67.      *    @param null $value       Supposedly null value.
  68.      *    @param string $message   Message to display.
  69.      *    @return boolean                        True on pass
  70.      *    @access public
  71.      */
  72.     function assertNull($value$message '%s'{
  73.         $dumper &new SimpleDumper();
  74.         $message sprintf(
  75.                 $message,
  76.                 '[' $dumper->describeValue($value'] should be null');
  77.         return $this->assertTrue(isset($value)$message);
  78.     }
  79.  
  80.     /**
  81.      *    Will be true if the value is set.
  82.      *    @param mixed $value           Supposedly set value.
  83.      *    @param string $message        Message to display.
  84.      *    @return boolean               True on pass.
  85.      *    @access public
  86.      */
  87.     function assertNotNull($value$message '%s'{
  88.         $dumper &new SimpleDumper();
  89.         $message sprintf(
  90.                 $message,
  91.                 '[' $dumper->describeValue($value'] should not be null');
  92.         return $this->assertTrue(isset($value)$message);
  93.     }
  94.  
  95.     /**
  96.      *    Type and class test. Will pass if class
  97.      *    matches the type name or is a subclass or
  98.      *    if not an object, but the type is correct.
  99.      *    @param mixed $object         Object to test.
  100.      *    @param string $type          Type name as string.
  101.      *    @param string $message       Message to display.
  102.      *    @return boolean              True on pass.
  103.      *    @access public
  104.      */
  105.     function assertIsA($object$type$message '%s'{
  106.         return $this->assert(
  107.                 new IsAExpectation($type),
  108.                 $object,
  109.                 $message);
  110.     }
  111.  
  112.     /**
  113.      *    Type and class mismatch test. Will pass if class
  114.      *    name or underling type does not match the one
  115.      *    specified.
  116.      *    @param mixed $object         Object to test.
  117.      *    @param string $type          Type name as string.
  118.      *    @param string $message       Message to display.
  119.      *    @return boolean              True on pass.
  120.      *    @access public
  121.      */
  122.     function assertNotA($object$type$message '%s'{
  123.         return $this->assert(
  124.                 new NotAExpectation($type),
  125.                 $object,
  126.                 $message);
  127.     }
  128.  
  129.     /**
  130.      *    Will trigger a pass if the two parameters have
  131.      *    the same value only. Otherwise a fail.
  132.      *    @param mixed $first          Value to compare.
  133.      *    @param mixed $second         Value to compare.
  134.      *    @param string $message       Message to display.
  135.      *    @return boolean              True on pass
  136.      *    @access public
  137.      */
  138.     function assertEqual($first$second$message '%s'{
  139.         return $this->assert(
  140.                 new EqualExpectation($first),
  141.                 $second,
  142.                 $message);
  143.     }
  144.  
  145.     /**
  146.      *    Will trigger a pass if the two parameters have
  147.      *    a different value. Otherwise a fail.
  148.      *    @param mixed $first           Value to compare.
  149.      *    @param mixed $second          Value to compare.
  150.      *    @param string $message        Message to display.
  151.      *    @return boolean               True on pass
  152.      *    @access public
  153.      */
  154.     function assertNotEqual($first$second$message '%s'{
  155.         return $this->assert(
  156.                 new NotEqualExpectation($first),
  157.                 $second,
  158.                 $message);
  159.     }
  160.  
  161.     /**
  162.      *    Will trigger a pass if the if the first parameter
  163.      *    is near enough to the second by the margin.
  164.      *    @param mixed $first          Value to compare.
  165.      *    @param mixed $second         Value to compare.
  166.      *    @param mixed $margin         Fuzziness of match.
  167.      *    @param string $message       Message to display.
  168.      *    @return boolean              True on pass
  169.      *    @access public
  170.      */
  171.     function assertWithinMargin($first$second$margin$message '%s'{
  172.         return $this->assert(
  173.                 new WithinMarginExpectation($first$margin),
  174.                 $second,
  175.                 $message);
  176.     }
  177.  
  178.     /**
  179.      *    Will trigger a pass if the two parameters differ
  180.      *    by more than the margin.
  181.      *    @param mixed $first          Value to compare.
  182.      *    @param mixed $second         Value to compare.
  183.      *    @param mixed $margin         Fuzziness of match.
  184.      *    @param string $message       Message to display.
  185.      *    @return boolean              True on pass
  186.      *    @access public
  187.      */
  188.     function assertOutsideMargin($first$second$margin$message '%s'{
  189.         return $this->assert(
  190.                 new OutsideMarginExpectation($first$margin),
  191.                 $second,
  192.                 $message);
  193.     }
  194.  
  195.     /**
  196.      *    Will trigger a pass if the two parameters have
  197.      *    the same value and same type. Otherwise a fail.
  198.      *    @param mixed $first           Value to compare.
  199.      *    @param mixed $second          Value to compare.
  200.      *    @param string $message        Message to display.
  201.      *    @return boolean               True on pass
  202.      *    @access public
  203.      */
  204.     function assertIdentical($first$second$message '%s'{
  205.         return $this->assert(
  206.                 new IdenticalExpectation($first),
  207.                 $second,
  208.                 $message);
  209.     }
  210.  
  211.     /**
  212.      *    Will trigger a pass if the two parameters have
  213.      *    the different value or different type.
  214.      *    @param mixed $first           Value to compare.
  215.      *    @param mixed $second          Value to compare.
  216.      *    @param string $message        Message to display.
  217.      *    @return boolean               True on pass
  218.      *    @access public
  219.      */
  220.     function assertNotIdentical($first$second$message '%s'{
  221.         return $this->assert(
  222.                 new NotIdenticalExpectation($first),
  223.                 $second,
  224.                 $message);
  225.     }
  226.  
  227.     /**
  228.      *    Will trigger a pass if both parameters refer
  229.      *    to the same object. Fail otherwise.
  230.      *    @param mixed $first           Object reference to check.
  231.      *    @param mixed $second          Hopefully the same object.
  232.      *    @param string $message        Message to display.
  233.      *    @return boolean               True on pass
  234.      *    @access public
  235.      */
  236.     function assertReference(&$first&$second$message '%s'{
  237.         $dumper &new SimpleDumper();
  238.         $message sprintf(
  239.                 $message,
  240.                 '[' $dumper->describeValue($first.
  241.                         '] and [' $dumper->describeValue($second.
  242.                         '] should reference the same object');
  243.         return $this->assertTrue(
  244.                 SimpleTestCompatibility::isReference($first$second),
  245.                 $message);
  246.     }
  247.  
  248.     /**
  249.      *    Will trigger a pass if both parameters refer
  250.      *    to different objects. Fail otherwise. The objects
  251.      *    have to be identical though.
  252.      *    @param mixed $first           Object reference to check.
  253.      *    @param mixed $second          Hopefully not the same object.
  254.      *    @param string $message        Message to display.
  255.      *    @return boolean               True on pass
  256.      *    @access public
  257.      */
  258.     function assertClone(&$first&$second$message '%s'{
  259.         $dumper &new SimpleDumper();
  260.         $message sprintf(
  261.                 $message,
  262.                 '[' $dumper->describeValue($first.
  263.                         '] and [' $dumper->describeValue($second.
  264.                         '] should not be the same object');
  265.         $identical &new IdenticalExpectation($first);
  266.         return $this->assertTrue(
  267.                 $identical->test($second&&
  268.                         SimpleTestCompatibility::isReference($first$second),
  269.                 $message);
  270.     }
  271.  
  272.     /**
  273.      *    @deprecated
  274.      */
  275.     function assertCopy(&$first&$second$message "%s"{
  276.         $dumper &new SimpleDumper();
  277.         $message sprintf(
  278.                 $message,
  279.                 "[" $dumper->describeValue($first.
  280.                         "] and [" $dumper->describeValue($second.
  281.                         "] should not be the same object");
  282.         return $this->assertFalse(
  283.                 SimpleTestCompatibility::isReference($first$second),
  284.                 $message);
  285.     }
  286.  
  287.     /**
  288.      *    Will trigger a pass if the Perl regex pattern
  289.      *    is found in the subject. Fail otherwise.
  290.      *    @param string $pattern    Perl regex to look for including
  291.      *                               the regex delimiters.
  292.      *    @param string $subject    String to search in.
  293.      *    @param string $message    Message to display.
  294.      *    @return boolean           True on pass
  295.      *    @access public
  296.      */
  297.     function assertPattern($pattern$subject$message '%s'{
  298.         return $this->assert(
  299.                 new PatternExpectation($pattern),
  300.                 $subject,
  301.                 $message);
  302.     }
  303.  
  304.     /**
  305.      *    @deprecated
  306.      */
  307.     function assertWantedPattern($pattern$subject$message '%s'{
  308.         return $this->assertPattern($pattern$subject$message);
  309.     }
  310.  
  311.     /**
  312.      *    Will trigger a pass if the perl regex pattern
  313.      *    is not present in subject. Fail if found.
  314.      *    @param string $pattern    Perl regex to look for including
  315.      *                               the regex delimiters.
  316.      *    @param string $subject    String to search in.
  317.      *    @param string $message    Message to display.
  318.      *    @return boolean           True on pass
  319.      *    @access public
  320.      */
  321.     function assertNoPattern($pattern$subject$message '%s'{
  322.         return $this->assert(
  323.                 new NoPatternExpectation($pattern),
  324.                 $subject,
  325.                 $message);
  326.     }
  327.  
  328.     /**
  329.      *    @deprecated
  330.      */
  331.     function assertNoUnwantedPattern($pattern$subject$message '%s'{
  332.         return $this->assertNoPattern($pattern$subject$message);
  333.     }
  334.  
  335.     /**
  336.      *    @deprecated
  337.      */
  338.     function swallowErrors({
  339.         $context &SimpleTest::getContext();
  340.         $queue &$context->get('SimpleErrorQueue');
  341.         $queue->clear();
  342.     }
  343.  
  344.     /**
  345.      *    @deprecated
  346.      */
  347.     function assertNoErrors($message '%s'{
  348.         $context &SimpleTest::getContext();
  349.         $queue &$context->get('SimpleErrorQueue');
  350.         return $queue->assertNoErrors($message);
  351.     }
  352.  
  353.     /**
  354.      *    @deprecated
  355.      */
  356.     function assertError($expected false$message '%s'{
  357.         $context &SimpleTest::getContext();
  358.         $queue &$context->get('SimpleErrorQueue');
  359.         return $queue->assertError($this->_coerceExpectation($expected)$message);
  360.     }
  361.  
  362.     /**
  363.      *    Prepares for an error. If the error mismatches it
  364.      *    passes through, otherwise it is swallowed. Any
  365.      *    left over errors trigger failures.
  366.      *    @param SimpleExpectation/string $expected   The error to match.
  367.      *    @param string $message                      Message on failure.
  368.      *    @access public
  369.      */
  370.     function expectError($expected false$message '%s'{
  371.         $context &SimpleTest::getContext();
  372.         $queue &$context->get('SimpleErrorQueue');
  373.         $queue->expectError($this->_coerceExpectation($expected)$message);
  374.     }
  375.  
  376.     /**
  377.      *    Prepares for an exception. If the error mismatches it
  378.      *    passes through, otherwise it is swallowed. Any
  379.      *    left over errors trigger failures.
  380.      *    @param SimpleExpectation/Exception $expected  The error to match.
  381.      *    @param string $message                        Message on failure.
  382.      *    @access public
  383.      */
  384.     function expectException($expected false$message '%s'{
  385.         $context &SimpleTest::getContext();
  386.         $queue &$context->get('SimpleExceptionTrap');
  387.         // :HACK: Directly substituting in seems to cause a segfault with
  388.         // Zend Optimizer on some systems
  389.         $line $this->getAssertionLine();
  390.         $queue->expectException($expected$message $line);
  391.     }
  392.  
  393.     /**
  394.      *    Creates an equality expectation if the
  395.      *    object/value is not already some type
  396.      *    of expectation.
  397.      *    @param mixed $expected      Expected value.
  398.      *    @return SimpleExpectation   Expectation object.
  399.      *    @access private
  400.      */
  401.     function _coerceExpectation($expected{
  402.         if ($expected == false{
  403.             return new TrueExpectation();
  404.         }
  405.         if (SimpleTestCompatibility::isA($expected'SimpleExpectation')) {
  406.             return $expected;
  407.         }
  408.         return new EqualExpectation(
  409.                 is_string($expectedstr_replace('%''%%'$expected$expected);
  410.     }
  411.  
  412.     /**
  413.      *    @deprecated
  414.      */
  415.     function assertErrorPattern($pattern$message '%s'{
  416.         return $this->assertError(new PatternExpectation($pattern)$message);
  417.     }
  418. }
  419. ?>

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