Source for file expectation.php

Documentation is available at expectation.php

  1. <?php
  2. /**
  3.  *    base include file for SimpleTest
  4.  *    @package    SimpleTest
  5.  *    @subpackage    UnitTester
  6.  *    @version    $Id: expectation.php 1905 2009-07-29 13:54:00Z dgheath $
  7.  */
  8.  
  9. /**#@+
  10.  *    include other SimpleTest class files
  11.  */
  12. require_once(dirname(__FILE__'/dumper.php');
  13. require_once(dirname(__FILE__'/compatibility.php');
  14. /**#@-*/
  15.  
  16. /**
  17.  *    Assertion that can display failure information.
  18.  *    Also includes various helper methods.
  19.  *    @package SimpleTest
  20.  *    @subpackage UnitTester
  21.  *    @abstract
  22.  */
  23.     protected $dumper = false;
  24.     private $message;
  25.  
  26.     /**
  27.      *    Creates a dumper for displaying values and sets
  28.      *    the test message.
  29.      *    @param string $message    Customised message on failure.
  30.      */
  31.     function __construct($message '%s'{
  32.         $this->message $message;
  33.     }
  34.  
  35.     /**
  36.      *    Tests the expectation. True if correct.
  37.      *    @param mixed $compare        Comparison value.
  38.      *    @return boolean              True if correct.
  39.      *    @access public
  40.      *    @abstract
  41.      */
  42.     function test($compare{
  43.     }
  44.  
  45.     /**
  46.      *    Returns a human readable test message.
  47.      *    @param mixed $compare      Comparison value.
  48.      *    @return string             Description of success
  49.      *                                or failure.
  50.      *    @access public
  51.      *    @abstract
  52.      */
  53.     function testMessage($compare{
  54.     }
  55.  
  56.     /**
  57.      *    Overlays the generated message onto the stored user
  58.      *    message. An additional message can be interjected.
  59.      *    @param mixed $compare        Comparison value.
  60.      *    @param SimpleDumper $dumper  For formatting the results.
  61.      *    @return string               Description of success
  62.      *                                  or failure.
  63.      *    @access public
  64.      */
  65.     function overlayMessage($compare$dumper{
  66.         $this->dumper = $dumper;
  67.         return sprintf($this->message$this->testMessage($compare));
  68.     }
  69.  
  70.     /**
  71.      *    Accessor for the dumper.
  72.      *    @return SimpleDumper    Current value dumper.
  73.      *    @access protected
  74.      */
  75.     protected function getDumper({
  76.         if ($this->dumper{
  77.             $dumper new SimpleDumper();
  78.             return $dumper;
  79.         }
  80.         return $this->dumper;
  81.     }
  82.  
  83.     /**
  84.      *    Test to see if a value is an expectation object.
  85.      *    A useful utility method.
  86.      *    @param mixed $expectation    Hopefully an Expectation
  87.      *                                  class.
  88.      *    @return boolean              True if descended from
  89.      *                                  this class.
  90.      *    @access public
  91.      */
  92.     static function isExpectation($expectation{
  93.         return is_object($expectation&&
  94.                 SimpleTestCompatibility::isA($expectation'SimpleExpectation');
  95.     }
  96. }
  97.  
  98. /**
  99.  *    A wildcard expectation always matches.
  100.  *    @package SimpleTest
  101.  *    @subpackage MockObjects
  102.  */
  103.  
  104.     /**
  105.      *    Tests the expectation. Always true.
  106.      *    @param mixed $compare  Ignored.
  107.      *    @return boolean        True.
  108.      *    @access public
  109.      */
  110.     function test($compare{
  111.         return true;
  112.     }
  113.  
  114.     /**
  115.      *    Returns a human readable test message.
  116.      *    @param mixed $compare      Comparison value.
  117.      *    @return string             Description of success
  118.      *                                or failure.
  119.      *    @access public
  120.      */
  121.     function testMessage($compare{
  122.         $dumper $this->getDumper();
  123.         return 'Anything always matches [' $dumper->describeValue($compare']';
  124.     }
  125. }
  126.  
  127. /**
  128.  *    An expectation that never matches.
  129.  *    @package SimpleTest
  130.  *    @subpackage MockObjects
  131.  */
  132.  
  133.     /**
  134.      *    Tests the expectation. Always false.
  135.      *    @param mixed $compare  Ignored.
  136.      *    @return boolean        True.
  137.      *    @access public
  138.      */
  139.     function test($compare{
  140.         return false;
  141.     }
  142.  
  143.     /**
  144.      *    Returns a human readable test message.
  145.      *    @param mixed $compare      Comparison value.
  146.      *    @return string             Description of failure.
  147.      *    @access public
  148.      */
  149.     function testMessage($compare{
  150.         $dumper $this->getDumper();
  151.         return 'Failed expectation never matches [' $dumper->describeValue($compare']';
  152.     }
  153. }
  154.  
  155. /**
  156.  *    An expectation that passes on boolean true.
  157.  *    @package SimpleTest
  158.  *    @subpackage MockObjects
  159.  */
  160.  
  161.     /**
  162.      *    Tests the expectation.
  163.      *    @param mixed $compare  Should be true.
  164.      *    @return boolean        True on match.
  165.      *    @access public
  166.      */
  167.     function test($compare{
  168.         return (boolean)$compare;
  169.     }
  170.  
  171.     /**
  172.      *    Returns a human readable test message.
  173.      *    @param mixed $compare      Comparison value.
  174.      *    @return string             Description of success
  175.      *                                or failure.
  176.      *    @access public
  177.      */
  178.     function testMessage($compare{
  179.         $dumper $this->getDumper();
  180.         return 'Expected true, got [' $dumper->describeValue($compare']';
  181.     }
  182. }
  183.  
  184. /**
  185.  *    An expectation that passes on boolean false.
  186.  *    @package SimpleTest
  187.  *    @subpackage MockObjects
  188.  */
  189.  
  190.     /**
  191.      *    Tests the expectation.
  192.      *    @param mixed $compare  Should be false.
  193.      *    @return boolean        True on match.
  194.      *    @access public
  195.      */
  196.     function test($compare{
  197.         return (boolean)$compare;
  198.     }
  199.  
  200.     /**
  201.      *    Returns a human readable test message.
  202.      *    @param mixed $compare      Comparison value.
  203.      *    @return string             Description of success
  204.      *                                or failure.
  205.      *    @access public
  206.      */
  207.     function testMessage($compare{
  208.         $dumper $this->getDumper();
  209.         return 'Expected false, got [' $dumper->describeValue($compare']';
  210.     }
  211. }
  212.  
  213. /**
  214.  *    Test for equality.
  215.  *    @package SimpleTest
  216.  *    @subpackage UnitTester
  217.  */
  218.     private $value;
  219.  
  220.     /**
  221.      *    Sets the value to compare against.
  222.      *    @param mixed $value        Test value to match.
  223.      *    @param string $message     Customised message on failure.
  224.      *    @access public
  225.      */
  226.     function __construct($value$message '%s'{
  227.         parent::__construct($message);
  228.         $this->value $value;
  229.     }
  230.  
  231.     /**
  232.      *    Tests the expectation. True if it matches the
  233.      *    held value.
  234.      *    @param mixed $compare        Comparison value.
  235.      *    @return boolean              True if correct.
  236.      *    @access public
  237.      */
  238.     function test($compare{
  239.         return (($this->value == $compare&& ($compare == $this->value));
  240.     }
  241.  
  242.     /**
  243.      *    Returns a human readable test message.
  244.      *    @param mixed $compare      Comparison value.
  245.      *    @return string             Description of success
  246.      *                                or failure.
  247.      *    @access public
  248.      */
  249.     function testMessage($compare{
  250.         if ($this->test($compare)) {
  251.             return "Equal expectation [" $this->dumper->describeValue($this->value"]";
  252.         else {
  253.             return "Equal expectation fails " .
  254.                     $this->dumper->describeDifference($this->value$compare);
  255.         }
  256.     }
  257.  
  258.     /**
  259.      *    Accessor for comparison value.
  260.      *    @return mixed       Held value to compare with.
  261.      *    @access protected
  262.      */
  263.     protected function getValue({
  264.         return $this->value;
  265.     }
  266. }
  267.  
  268. /**
  269.  *    Test for inequality.
  270.  *    @package SimpleTest
  271.  *    @subpackage UnitTester
  272.  */
  273.  
  274.     /**
  275.      *    Sets the value to compare against.
  276.      *    @param mixed $value       Test value to match.
  277.      *    @param string $message    Customised message on failure.
  278.      *    @access public
  279.      */
  280.     function __construct($value$message '%s'{
  281.         parent::__construct($value$message);
  282.     }
  283.  
  284.     /**
  285.      *    Tests the expectation. True if it differs from the
  286.      *    held value.
  287.      *    @param mixed $compare        Comparison value.
  288.      *    @return boolean              True if correct.
  289.      *    @access public
  290.      */
  291.     function test($compare{
  292.         return parent::test($compare);
  293.     }
  294.  
  295.     /**
  296.      *    Returns a human readable test message.
  297.      *    @param mixed $compare      Comparison value.
  298.      *    @return string             Description of success
  299.      *                                or failure.
  300.      *    @access public
  301.      */
  302.     function testMessage($compare{
  303.         $dumper $this->getDumper();
  304.         if ($this->test($compare)) {
  305.             return "Not equal expectation passes " .
  306.                     $dumper->describeDifference($this->getValue()$compare);
  307.         else {
  308.             return "Not equal expectation fails [" .
  309.                     $dumper->describeValue($this->getValue()) .
  310.                     "] matches";
  311.         }
  312.     }
  313. }
  314.  
  315. /**
  316.  *    Test for being within a range.
  317.  *    @package SimpleTest
  318.  *    @subpackage UnitTester
  319.  */
  320.     private $upper;
  321.     private $lower;
  322.  
  323.     /**
  324.      *    Sets the value to compare against and the fuzziness of
  325.      *    the match. Used for comparing floating point values.
  326.      *    @param mixed $value        Test value to match.
  327.      *    @param mixed $margin       Fuzziness of match.
  328.      *    @param string $message     Customised message on failure.
  329.      *    @access public
  330.      */
  331.     function __construct($value$margin$message '%s'{
  332.         parent::__construct($message);
  333.         $this->upper $value $margin;
  334.         $this->lower $value $margin;
  335.     }
  336.  
  337.     /**
  338.      *    Tests the expectation. True if it matches the
  339.      *    held value.
  340.      *    @param mixed $compare        Comparison value.
  341.      *    @return boolean              True if correct.
  342.      *    @access public
  343.      */
  344.     function test($compare{
  345.         return (($compare <= $this->upper&& ($compare >= $this->lower));
  346.     }
  347.  
  348.     /**
  349.      *    Returns a human readable test message.
  350.      *    @param mixed $compare      Comparison value.
  351.      *    @return string             Description of success
  352.      *                                or failure.
  353.      *    @access public
  354.      */
  355.     function testMessage($compare{
  356.         if ($this->test($compare)) {
  357.             return $this->withinMessage($compare);
  358.         else {
  359.             return $this->outsideMessage($compare);
  360.         }
  361.     }
  362.  
  363.     /**
  364.      *    Creates a the message for being within the range.
  365.      *    @param mixed $compare        Value being tested.
  366.      *    @access private
  367.      */
  368.     protected function withinMessage($compare{
  369.         return "Within expectation [" $this->dumper->describeValue($this->lower"] and [" .
  370.                 $this->dumper->describeValue($this->upper"]";
  371.     }
  372.  
  373.     /**
  374.      *    Creates a the message for being within the range.
  375.      *    @param mixed $compare        Value being tested.
  376.      *    @access private
  377.      */
  378.     protected function outsideMessage($compare{
  379.         if ($compare $this->upper{
  380.             return "Outside expectation " .
  381.                     $this->dumper->describeDifference($compare$this->upper);
  382.         else {
  383.             return "Outside expectation " .
  384.                     $this->dumper->describeDifference($compare$this->lower);
  385.         }
  386.     }
  387. }
  388.  
  389. /**
  390.  *    Test for being outside of a range.
  391.  *    @package SimpleTest
  392.  *    @subpackage UnitTester
  393.  */
  394.  
  395.     /**
  396.      *    Sets the value to compare against and the fuzziness of
  397.      *    the match. Used for comparing floating point values.
  398.      *    @param mixed $value        Test value to not match.
  399.      *    @param mixed $margin       Fuzziness of match.
  400.      *    @param string $message     Customised message on failure.
  401.      *    @access public
  402.      */
  403.     function __construct($value$margin$message '%s'{
  404.         parent::__construct($value$margin$message);
  405.     }
  406.  
  407.     /**
  408.      *    Tests the expectation. True if it matches the
  409.      *    held value.
  410.      *    @param mixed $compare        Comparison value.
  411.      *    @return boolean              True if correct.
  412.      *    @access public
  413.      */
  414.     function test($compare{
  415.         return parent::test($compare);
  416.     }
  417.  
  418.     /**
  419.      *    Returns a human readable test message.
  420.      *    @param mixed $compare      Comparison value.
  421.      *    @return string             Description of success
  422.      *                                or failure.
  423.      *    @access public
  424.      */
  425.     function testMessage($compare{
  426.         if ($this->test($compare)) {
  427.             return $this->withinMessage($compare);
  428.         else {
  429.             return $this->outsideMessage($compare);
  430.         }
  431.     }
  432. }
  433.  
  434. /**
  435.  *    Test for reference.
  436.  *    @package SimpleTest
  437.  *    @subpackage UnitTester
  438.  */
  439.     private $value;
  440.  
  441.     /**
  442.      *    Sets the reference value to compare against.
  443.      *    @param mixed $value       Test reference to match.
  444.      *    @param string $message    Customised message on failure.
  445.      *    @access public
  446.      */
  447.     function __construct(&$value$message '%s'{
  448.         $this->message $message;
  449.         $this->value &$value;
  450.     }
  451.  
  452.     /**
  453.      *    Tests the expectation. True if it exactly
  454.      *    references the held value.
  455.      *    @param mixed $compare        Comparison reference.
  456.      *    @return boolean              True if correct.
  457.      *    @access public
  458.      */
  459.     function test(&$compare{
  460.         return SimpleTestCompatibility::isReference($this->value$compare);
  461.     }
  462.  
  463.     /**
  464.      *    Returns a human readable test message.
  465.      *    @param mixed $compare      Comparison value.
  466.      *    @return string             Description of success
  467.      *                                or failure.
  468.      *    @access public
  469.      */
  470.     function testMessage($compare{
  471.         if ($this->test($compare)) {
  472.             return "Reference expectation [" $this->dumper->describeValue($this->value"]";
  473.         else {
  474.             return "Reference expectation fails " .
  475.                     $this->dumper->describeDifference($this->value$compare);
  476.         }
  477.     }
  478.  
  479.     /**
  480.      *    Overlays the generated message onto the stored user
  481.      *    message. An additional message can be interjected.
  482.      *    @param mixed $compare        Comparison value.
  483.      *    @param SimpleDumper $dumper  For formatting the results.
  484.      *    @return string               Description of success
  485.      *                                  or failure.
  486.      *    @access public
  487.      */
  488.     function overlayMessage($compare$dumper{
  489.         $this->dumper $dumper;
  490.         return sprintf($this->message$this->testMessage($compare));
  491.     }
  492.  
  493.     /**
  494.      *    Accessor for the dumper.
  495.      *    @return SimpleDumper    Current value dumper.
  496.      *    @access protected
  497.      */
  498.     protected function getDumper({
  499.         if ($this->dumper{
  500.             $dumper new SimpleDumper();
  501.             return $dumper;
  502.         }
  503.         return $this->dumper;
  504.     }
  505. }
  506.  
  507. /**
  508.  *    Test for identity.
  509.  *    @package SimpleTest
  510.  *    @subpackage UnitTester
  511.  */
  512.  
  513.     /**
  514.      *    Sets the value to compare against.
  515.      *    @param mixed $value       Test value to match.
  516.      *    @param string $message    Customised message on failure.
  517.      *    @access public
  518.      */
  519.     function __construct($value$message '%s'{
  520.         parent::__construct($value$message);
  521.     }
  522.  
  523.     /**
  524.      *    Tests the expectation. True if it exactly
  525.      *    matches the held value.
  526.      *    @param mixed $compare        Comparison value.
  527.      *    @return boolean              True if correct.
  528.      *    @access public
  529.      */
  530.     function test($compare{
  531.         return SimpleTestCompatibility::isIdentical($this->getValue()$compare);
  532.     }
  533.  
  534.     /**
  535.      *    Returns a human readable test message.
  536.      *    @param mixed $compare      Comparison value.
  537.      *    @return string             Description of success
  538.      *                                or failure.
  539.      *    @access public
  540.      */
  541.     function testMessage($compare{
  542.         $dumper $this->getDumper();
  543.         if ($this->test($compare)) {
  544.             return "Identical expectation [" $dumper->describeValue($this->getValue()) "]";
  545.         else {
  546.             return "Identical expectation [" $dumper->describeValue($this->getValue()) .
  547.                     "] fails with [" .
  548.                     $dumper->describeValue($compare"] " .
  549.                     $dumper->describeDifference($this->getValue()$compareTYPE_MATTERS);
  550.         }
  551.     }
  552. }
  553.  
  554. /**
  555.  *    Test for non-identity.
  556.  *    @package SimpleTest
  557.  *    @subpackage UnitTester
  558.  */
  559.  
  560.     /**
  561.      *    Sets the value to compare against.
  562.      *    @param mixed $value        Test value to match.
  563.      *    @param string $message     Customised message on failure.
  564.      *    @access public
  565.      */
  566.     function __construct($value$message '%s'{
  567.         parent::__construct($value$message);
  568.     }
  569.  
  570.     /**
  571.      *    Tests the expectation. True if it differs from the
  572.      *    held value.
  573.      *    @param mixed $compare        Comparison value.
  574.      *    @return boolean              True if correct.
  575.      *    @access public
  576.      */
  577.     function test($compare{
  578.         return parent::test($compare);
  579.     }
  580.  
  581.     /**
  582.      *    Returns a human readable test message.
  583.      *    @param mixed $compare      Comparison value.
  584.      *    @return string             Description of success
  585.      *                                or failure.
  586.      *    @access public
  587.      */
  588.     function testMessage($compare{
  589.         $dumper $this->getDumper();
  590.         if ($this->test($compare)) {
  591.             return "Not identical expectation passes " .
  592.                     $dumper->describeDifference($this->getValue()$compareTYPE_MATTERS);
  593.         else {
  594.             return "Not identical expectation [" $dumper->describeValue($this->getValue()) "] matches";
  595.         }
  596.     }
  597. }
  598.  
  599. /**
  600.  *    Test for a pattern using Perl regex rules.
  601.  *    @package SimpleTest
  602.  *    @subpackage UnitTester
  603.  */
  604.     private $pattern;
  605.  
  606.     /**
  607.      *    Sets the value to compare against.
  608.      *    @param string $pattern    Pattern to search for.
  609.      *    @param string $message    Customised message on failure.
  610.      *    @access public
  611.      */
  612.     function __construct($pattern$message '%s'{
  613.         parent::__construct($message);
  614.         $this->pattern $pattern;
  615.     }
  616.  
  617.     /**
  618.      *    Accessor for the pattern.
  619.      *    @return string       Perl regex as string.
  620.      *    @access protected
  621.      */
  622.     protected function getPattern({
  623.         return $this->pattern;
  624.     }
  625.  
  626.     /**
  627.      *    Tests the expectation. True if the Perl regex
  628.      *    matches the comparison value.
  629.      *    @param string $compare        Comparison value.
  630.      *    @return boolean               True if correct.
  631.      *    @access public
  632.      */
  633.     function test($compare{
  634.         return (boolean)preg_match($this->getPattern()$compare);
  635.     }
  636.  
  637.     /**
  638.      *    Returns a human readable test message.
  639.      *    @param mixed $compare      Comparison value.
  640.      *    @return string             Description of success
  641.      *                                or failure.
  642.      *    @access public
  643.      */
  644.     function testMessage($compare{
  645.         if ($this->test($compare)) {
  646.             return $this->describePatternMatch($this->getPattern()$compare);
  647.         else {
  648.             $dumper $this->getDumper();
  649.             return "Pattern [" $this->getPattern(.
  650.                     "] not detected in [" .
  651.                     $dumper->describeValue($compare"]";
  652.         }
  653.     }
  654.  
  655.     /**
  656.      *    Describes a pattern match including the string
  657.      *    found and it's position.
  658.      *    @param string $pattern        Regex to match against.
  659.      *    @param string $subject        Subject to search.
  660.      *    @access protected
  661.      */
  662.     protected function describePatternMatch($pattern$subject{
  663.         preg_match($pattern$subject$matches);
  664.         $position strpos($subject$matches[0]);
  665.         $dumper $this->getDumper();
  666.         return "Pattern [$pattern] detected at character [$position] in [.
  667.                 $dumper->describeValue($subject"] as [" .
  668.                 $matches[0"] in region [" .
  669.                 $dumper->clipString($subject100$position"]";
  670.     }
  671. }
  672.  
  673. /**
  674.  *    Fail if a pattern is detected within the
  675.  *    comparison.
  676.  *    @package SimpleTest
  677.  *    @subpackage UnitTester
  678.  */
  679.  
  680.     /**
  681.      *    Sets the reject pattern
  682.      *    @param string $pattern    Pattern to search for.
  683.      *    @param string $message    Customised message on failure.
  684.      *    @access public
  685.      */
  686.     function __construct($pattern$message '%s'{
  687.         parent::__construct($pattern$message);
  688.     }
  689.  
  690.     /**
  691.      *    Tests the expectation. False if the Perl regex
  692.      *    matches the comparison value.
  693.      *    @param string $compare        Comparison value.
  694.      *    @return boolean               True if correct.
  695.      *    @access public
  696.      */
  697.     function test($compare{
  698.         return parent::test($compare);
  699.     }
  700.  
  701.     /**
  702.      *    Returns a human readable test message.
  703.      *    @param string $compare      Comparison value.
  704.      *    @return string              Description of success
  705.      *                                 or failure.
  706.      *    @access public
  707.      */
  708.     function testMessage($compare{
  709.         if ($this->test($compare)) {
  710.             $dumper $this->getDumper();
  711.             return "Pattern [" $this->getPattern(.
  712.                     "] not detected in [" .
  713.                     $dumper->describeValue($compare"]";
  714.         else {
  715.             return $this->describePatternMatch($this->getPattern()$compare);
  716.         }
  717.     }
  718. }
  719.  
  720. /**
  721.  *    Tests either type or class name if it's an object.
  722.  *      @package SimpleTest
  723.  *      @subpackage UnitTester
  724.  */
  725. class IsAExpectation extends SimpleExpectation {
  726.     private $type;
  727.  
  728.     /**
  729.      *    Sets the type to compare with.
  730.      *    @param string $type       Type or class name.
  731.      *    @param string $message    Customised message on failure.
  732.      *    @access public
  733.      */
  734.     function __construct($type$message '%s'{
  735.         parent::__construct($message);
  736.         $this->type $type;
  737.     }
  738.  
  739.     /**
  740.      *    Accessor for type to check against.
  741.      *    @return string    Type or class name.
  742.      *    @access protected
  743.      */
  744.     protected function getType({
  745.         return $this->type;
  746.     }
  747.  
  748.     /**
  749.      *    Tests the expectation. True if the type or
  750.      *    class matches the string value.
  751.      *    @param string $compare        Comparison value.
  752.      *    @return boolean               True if correct.
  753.      *    @access public
  754.      */
  755.     function test($compare{
  756.         if (is_object($compare)) {
  757.             return SimpleTestCompatibility::isA($compare$this->type);
  758.         else {
  759.             return (strtolower(gettype($compare)) == $this->canonicalType($this->type));
  760.         }
  761.     }
  762.  
  763.     /**
  764.      *    Coerces type name into a gettype() match.
  765.      *    @param string $type        User type.
  766.      *    @return string             Simpler type.
  767.      *    @access private
  768.      */
  769.     protected function canonicalType($type{
  770.         $type strtolower($type);
  771.         $map array(
  772.                 'bool' => 'boolean',
  773.                 'float' => 'double',
  774.                 'real' => 'double',
  775.                 'int' => 'integer');
  776.         if (isset($map[$type])) {
  777.             $type $map[$type];
  778.         }
  779.         return $type;
  780.     }
  781.  
  782.     /**
  783.      *    Returns a human readable test message.
  784.      *    @param mixed $compare      Comparison value.
  785.      *    @return string             Description of success
  786.      *                                or failure.
  787.      *    @access public
  788.      */
  789.     function testMessage($compare{
  790.         $dumper $this->getDumper();
  791.         return "Value [" $dumper->describeValue($compare.
  792.                 "] should be type [" $this->type "]";
  793.     }
  794. }
  795.  
  796. /**
  797.  *    Tests either type or class name if it's an object.
  798.  *    Will succeed if the type does not match.
  799.  *      @package SimpleTest
  800.  *      @subpackage UnitTester
  801.  */
  802. class NotAExpectation extends IsAExpectation {
  803.     private $type;
  804.  
  805.     /**
  806.      *    Sets the type to compare with.
  807.      *    @param string $type       Type or class name.
  808.      *    @param string $message    Customised message on failure.
  809.      *    @access public
  810.      */
  811.     function __construct($type$message '%s'{
  812.         parent::__construct($type$message);
  813.     }
  814.  
  815.     /**
  816.      *    Tests the expectation. False if the type or
  817.      *    class matches the string value.
  818.      *    @param string $compare        Comparison value.
  819.      *    @return boolean               True if different.
  820.      *    @access public
  821.      */
  822.     function test($compare{
  823.         return parent::test($compare);
  824.     }
  825.  
  826.     /**
  827.      *    Returns a human readable test message.
  828.      *    @param mixed $compare      Comparison value.
  829.      *    @return string             Description of success
  830.      *                                or failure.
  831.      *    @access public
  832.      */
  833.     function testMessage($compare{
  834.         $dumper $this->getDumper();
  835.         return "Value [" $dumper->describeValue($compare.
  836.                 "] should not be type [" $this->getType("]";
  837.     }
  838. }
  839.  
  840. /**
  841.  *    Tests for existance of a method in an object
  842.  *    @package SimpleTest
  843.  *    @subpackage UnitTester
  844.  */
  845.     private $method;
  846.  
  847.     /**
  848.      *    Sets the value to compare against.
  849.      *    @param string $method     Method to check.
  850.      *    @param string $message    Customised message on failure.
  851.      *    @return void 
  852.      */
  853.     function __construct($method$message '%s'{
  854.         parent::__construct($message);
  855.         $this->method &$method;
  856.     }
  857.  
  858.     /**
  859.      *    Tests the expectation. True if the method exists in the test object.
  860.      *    @param string $compare        Comparison method name.
  861.      *    @return boolean               True if correct.
  862.      */
  863.     function test($compare{
  864.         return (boolean)(is_object($compare&& method_exists($compare$this->method));
  865.     }
  866.  
  867.     /**
  868.      *    Returns a human readable test message.
  869.      *    @param mixed $compare      Comparison value.
  870.      *    @return string             Description of success
  871.      *                                or failure.
  872.      */
  873.     function testMessage($compare{
  874.         $dumper $this->getDumper();
  875.         if (is_object($compare)) {
  876.             return 'No method on non-object [' $dumper->describeValue($compare']';
  877.         }
  878.         $method $this->method;
  879.         return "Object [" $dumper->describeValue($compare.
  880.                 "] should contain method [$method]";
  881.     }
  882. }
  883.  
  884. /**
  885.  *    Compares an object member's value even if private.
  886.  *    @package SimpleTest
  887.  *    @subpackage UnitTester
  888.  */
  889.     private $name;
  890.  
  891.     /**
  892.      *    Sets the value to compare against.
  893.      *    @param string $method     Method to check.
  894.      *    @param string $message    Customised message on failure.
  895.      *    @return void 
  896.      */
  897.     function __construct($name$expected{
  898.         $this->name $name;
  899.         parent::__construct($expected);
  900.     }
  901.  
  902.     /**
  903.      *    Tests the expectation. True if the property value is identical.
  904.      *    @param object $actual         Comparison object.
  905.      *    @return boolean               True if identical.
  906.      */
  907.     function test($actual{
  908.         if (is_object($actual)) {
  909.             return false;
  910.         }
  911.         return parent::test($this->getProperty($this->name$actual));
  912.     }
  913.  
  914.     /**
  915.      *    Returns a human readable test message.
  916.      *    @param mixed $compare      Comparison value.
  917.      *    @return string             Description of success
  918.      *                                or failure.
  919.      */
  920.     function testMessage($actual{
  921.         return parent::testMessage($this->getProperty($this->name$actual));
  922.     }
  923.  
  924.     /**
  925.      *    Extracts the member value even if private using reflection.
  926.      *    @param string $name        Property name.
  927.      *    @param object $object      Object to read.
  928.      *    @return mixed              Value of property.
  929.      */
  930.     private function getProperty($name$object{
  931.         $reflection new ReflectionObject($object);
  932.         $property $reflection->getProperty($name);
  933.         if (method_exists($property'setAccessible')) {
  934.             $property->setAccessible(true);
  935.         }
  936.         try {
  937.             return $property->getValue($object);
  938.         catch (ReflectionException $e{
  939.             return $this->getPrivatePropertyNoMatterWhat($name$object);
  940.         }
  941.     }
  942.  
  943.     /**
  944.      *    Extracts a private member's value when reflection won't play ball.
  945.      *    @param string $name        Property name.
  946.      *    @param object $object      Object to read.
  947.      *    @return mixed              Value of property.
  948.      */
  949.     private function getPrivatePropertyNoMatterWhat($name$object{
  950.         foreach ((array)$object as $mangled_name => $value{
  951.             if ($this->unmangle($mangled_name== $name{
  952.                 return $value;
  953.             }
  954.         }
  955.     }
  956.  
  957.     /**
  958.      *    Removes crud from property name after it's been converted
  959.      *    to an array.
  960.      *    @param string $mangled     Name from array cast.
  961.      *    @return string             Cleaned up name.
  962.      */
  963.     function unmangle($mangled{
  964.         $parts preg_split('/[^a-zA-Z0-9_\x7f-\xff]+/'$mangled);
  965.         return array_pop($parts);
  966.     }
  967. }
  968. ?>

Documentation generated on Thu, 01 Oct 2009 20:54:49 -0500 by phpDocumentor 1.4.2