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

Source for file Assert.php

Documentation is available at Assert.php

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit2                                                       |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2004 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: Assert.php,v 1.14.2.5 2004/10/07 09:05:55 sebastian Exp $
  16. //
  17.  
  18. require_once 'PHPUnit2/Framework/AssertionFailedError.php';
  19. require_once 'PHPUnit2/Framework/ComparisonFailure.php';
  20.  
  21. /**
  22.  * A set of assert methods.
  23.  *
  24.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  25.  * @copyright   Copyright &copy; 2002-2004 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  Framework
  30.  * @static
  31.  */
  32.     // {{{ Static Members
  33.  
  34.     /**
  35.     * @var    boolean 
  36.     * @access private
  37.     * @static
  38.     */
  39.     private static $looselyTyped = FALSE;
  40.  
  41.     // }}}
  42.     // {{{ protected function __construct()
  43.  
  44.     /**
  45.     * Protect constructor since it is a static only class.
  46.     *
  47.     * @access protected
  48.     */
  49.     protected function __construct({}
  50.  
  51.     // }}}
  52.     // {{{ public static function assertContains($needle, $haystack, $message = '')
  53.  
  54.     /**
  55.     * Asserts that a haystack contains a needle.
  56.     *
  57.     * @param  mixed   $needle 
  58.     * @param  mixed   $haystack 
  59.     * @param  string  $message 
  60.     * @access public
  61.     * @static
  62.     * @since  2.1.0
  63.     */
  64.     public static function assertContains($needle$haystack$message ''{
  65.         if (is_string($needle&& is_string($haystack)) {
  66.             if (strpos($haystack$needle=== FALSE{
  67.                 self::failNotContains($needle$haystack$message);
  68.             }
  69.         }
  70.  
  71.         else if (is_array($haystack)) {
  72.             if (is_object($needle)) {
  73.                 $found = FALSE;
  74.  
  75.                 foreach ($haystack as $straw{
  76.                     if ($straw === $needle{
  77.                         $found = TRUE;
  78.                         break;
  79.                     }
  80.                 }
  81.  
  82.                 if (!$found{
  83.                     self::failNotContains($needle$haystack$message);
  84.                 }
  85.             else {
  86.                 if (!in_array($needle$haystack)) {
  87.                     self::failNotContains($needle$haystack$message);
  88.                 }
  89.             }
  90.         }
  91.  
  92.         else {
  93.             throw new Exception('Unsupported parameter passed to assertContains().');
  94.         }
  95.     }
  96.  
  97.     // }}}
  98.     // {{{ public static function assertNotContains($needle, $haystack, $message = '')
  99.  
  100.     /**
  101.     * Asserts that a haystack does not contain a needle.
  102.     *
  103.     * @param  mixed   $needle 
  104.     * @param  mixed   $haystack 
  105.     * @param  string  $message 
  106.     * @access public
  107.     * @static
  108.     * @since  2.1.0
  109.     */
  110.     public static function assertNotContains($needle$haystack$message ''{
  111.         if (is_string($needle&& is_string($haystack)) {
  112.             if (strpos($haystack$needle!== FALSE{
  113.                 self::failContains($needle$haystack$message);
  114.             }
  115.         }
  116.  
  117.         else if (is_array($haystack)) {
  118.             if (is_object($needle)) {
  119.                 $found = FALSE;
  120.  
  121.                 foreach ($haystack as $straw{
  122.                     if ($straw === $needle{
  123.                         $found = TRUE;
  124.                         break;
  125.                     }
  126.                 }
  127.  
  128.                 if ($found{
  129.                     self::failContains($needle$haystack$message);
  130.                 }
  131.             else {
  132.                 if (in_array($needle$haystack)) {
  133.                     self::failContains($needle$haystack$message);
  134.                 }
  135.             }
  136.         }
  137.  
  138.         else {
  139.             throw new Exception('Unsupported parameter passed to assertContains().');
  140.         }
  141.     }
  142.  
  143.     // }}}
  144.     // {{{ public static function assertEquals($expected, $actual, $message = '', $delta = 0)
  145.  
  146.     /**
  147.     * Asserts that two variables are equal.
  148.     *
  149.     * @param  mixed  $expected 
  150.     * @param  mixed  $actual 
  151.     * @param  string $message 
  152.     * @param  mixed  $delta 
  153.     * @access public
  154.     * @static
  155.     */
  156.     public static function assertEquals($expected$actual$message ''$delta = 0{
  157.         if (is_null($expected&& is_null($actual)) {
  158.             return;
  159.         }
  160.  
  161.         if (is_object($expected)) {
  162.             if (!is_object($actual|| (serialize($expected!= serialize($actual))) {
  163.                 self::failNotEquals($expected$actual$message);
  164.             }
  165.  
  166.             return;
  167.         }
  168.  
  169.         if (is_array($expected)) {
  170.             if (!is_array($actual)) {
  171.                 self::failNotEquals($expected$actual$message);
  172.             }
  173.  
  174.             self::sortArrayRecursively($actual);
  175.             self::sortArrayRecursively($expected);
  176.  
  177.             if (self::$looselyTyped{
  178.                 $actual   = self::convertToString($actual);
  179.                 $expected = self::convertToString($expected);
  180.             }
  181.  
  182.             self::assertEquals(serialize($expected)serialize($actual)$message);
  183.  
  184.             return;
  185.         }
  186.  
  187.         if ((is_double($expected|| is_float($expected)) &&
  188.             (is_double($actual)   || is_float($actual)) &&
  189.             (is_double($delta)    || is_float($delta))) {
  190.             if (!(abs($expected $actual<= $delta)) {
  191.                 self::failNotEquals($expected$actual$message);
  192.             }
  193.  
  194.             return;
  195.         }
  196.  
  197.         if (self::$looselyTyped{
  198.             settype($actualgettype($expected));
  199.         }
  200.  
  201.         self::assertSame($expected$actual$message);
  202.     }
  203.  
  204.     // }}}
  205.     // {{{ public static function assertTrue($condition, $message = '')
  206.  
  207.     /**
  208.     * Asserts that a condition is true.
  209.     *
  210.     * @param  boolean $condition 
  211.     * @param  string  $message 
  212.     * @access public
  213.     * @static
  214.     */
  215.     public static function assertTrue($condition$message ''{
  216.         if (!$condition{
  217.             self::fail($message);
  218.         }
  219.     }
  220.  
  221.     // }}}
  222.     // {{{ public static function assertFalse($condition, $message = '')
  223.  
  224.     /**
  225.     * Asserts that a condition is false.
  226.     *
  227.     * @param  boolean  $condition 
  228.     * @param  string   $message 
  229.     * @access public
  230.     * @static
  231.     */
  232.     public static function assertFalse($condition$message ''{
  233.         self::assertTrue(!$condition$message);
  234.     }
  235.  
  236.     // }}}
  237.     // {{{ public static function assertNotNull($object, $message = '')
  238.  
  239.     /**
  240.     * Asserts that an object isn't null.
  241.     *
  242.     * @param  object $object 
  243.     * @param  string $message 
  244.     * @access public
  245.     * @static
  246.     */
  247.     public static function assertNotNull($object$message ''{
  248.         self::assertTrue($object !== NULL$message);
  249.     }
  250.  
  251.     // }}}
  252.     // {{{ public static function assertNull($object, $message = '')
  253.  
  254.     /**
  255.     * Asserts that an object is null.
  256.     *
  257.     * @param  object $object 
  258.     * @param  string $message 
  259.     * @access public
  260.     * @static
  261.     */
  262.     public static function assertNull($object$message ''{
  263.         self::assertTrue($object === NULL$message);
  264.     }
  265.  
  266.     // }}}
  267.     // {{{ public static function assertSame($expected, $actual, $message = '')
  268.  
  269.     /**
  270.     * Asserts that two objects refer to the same object.
  271.     *
  272.     * @param  object $object 
  273.     * @param  object $object 
  274.     * @param  string $message 
  275.     * @access public
  276.     * @static
  277.     */
  278.     public static function assertSame($expected$actual$message ''{
  279.         if ($expected !== $actual{
  280.             self::failNotSame($expected$actual$message);
  281.         }
  282.     }
  283.  
  284.     // }}}
  285.     // {{{ public static function assertNotSame($expected, $actual, $message = '')
  286.  
  287.     /**
  288.     * Asserts that two objects refer not to the same object.
  289.     *
  290.     * @param  object $object 
  291.     * @param  object $object 
  292.     * @param  string $message 
  293.     * @access public
  294.     * @static
  295.     */
  296.     public static function assertNotSame($expected$actual$message ''{
  297.         if ($expected === $actual{
  298.             self::failSame($message);
  299.         }
  300.     }
  301.  
  302.     // }}}
  303.     // {{{ public static function assertType($expected, $actual, $message = '')
  304.  
  305.     /**
  306.     * Asserts that a variable is of a given type.
  307.     *
  308.     * @param  string $expected 
  309.     * @param  mixed  $actual 
  310.     * @param  string $message 
  311.     * @access public
  312.     * @static
  313.     */
  314.  
  315.     public static function assertType($expected$actual$message ''{
  316.         if (is_object($actual)) {
  317.             $actual get_class($actual);
  318.         else {
  319.             $actual gettype($actual);
  320.         }
  321.  
  322.         if ($expected != $actual{
  323.             self::failNotEquals(
  324.               $expected,
  325.               $actual,
  326.               $message
  327.             );
  328.         }
  329.     }
  330.  
  331.     // }}}
  332.     // {{{ public static function assertRegExp($pattern, $string, $message = '')
  333.  
  334.     /**
  335.     * Asserts that a string matches a given regular expression.
  336.     *
  337.     * @param  string $pattern 
  338.     * @param  string $string 
  339.     * @param  string $message 
  340.     * @access public
  341.     * @static
  342.     */
  343.     public static function assertRegExp($pattern$string$message ''{
  344.         if (!preg_match($pattern$string)) {
  345.             self::fail(
  346.               sprintf(
  347.                 '%s%s"%s" does not match pattern "%s"',
  348.  
  349.                 $message,
  350.                 ($message != ''' ' '',
  351.                 $string,
  352.                 $pattern
  353.               )
  354.             );
  355.         }
  356.     }
  357.  
  358.     // }}}
  359.     // {{{ public static function assertNotRegExp($pattern, $string, $message = '')
  360.  
  361.     /**
  362.     * Asserts that a string does not match a given regular expression.
  363.     *
  364.     * @param  string $pattern 
  365.     * @param  string $string 
  366.     * @param  string $message 
  367.     * @access public
  368.     * @static
  369.     * @since  2.1.0
  370.     */
  371.     public static function assertNotRegExp($pattern$string$message ''{
  372.         if (preg_match($pattern$string)) {
  373.             self::fail(
  374.               sprintf(
  375.                 '%s%s"%s" matches pattern "%s"',
  376.  
  377.                 $message,
  378.                 ($message != ''' ' '',
  379.                 $string,
  380.                 $pattern
  381.               )
  382.             );
  383.         }
  384.     }
  385.  
  386.     // }}}
  387.     // {{{ public static function fail($message = '')
  388.  
  389.     /**
  390.     * Fails a test with the given message.
  391.     *
  392.     * @param  string $message 
  393.     * @throws PHPUnit2_Framework_AssertionFailedError
  394.     * @access public
  395.     * @static
  396.     */
  397.     public static function fail($message ''{
  398.         throw new PHPUnit2_Framework_AssertionFailedError($message);
  399.     }
  400.  
  401.     // }}}
  402.     // {{{ public static function format($expected, $actual, $message)
  403.  
  404.     /**
  405.     * @param  mixed   $expected 
  406.     * @param  mixed   $actual 
  407.     * @param  string  $message 
  408.     * @access public
  409.     * @static
  410.     */
  411.     public static function format($expected$actual$message{
  412.         return sprintf(
  413.           '%s%sexpected: <%s> but was: <%s>',
  414.  
  415.           $message,
  416.           ($message != ''' ' '',
  417.           self::objectToString($expected),
  418.           self::objectToString($actual)
  419.         );
  420.     }
  421.  
  422.     // }}}
  423.     // {{{ public static function setLooselyTyped($looselyTyped)
  424.  
  425.     /**
  426.     * @param  boolean $looselyTyped 
  427.     * @access public
  428.     * @static
  429.     */
  430.     public static function setLooselyTyped($looselyTyped{
  431.         if (is_bool($looselyTyped)) {
  432.             self::$looselyTyped $looselyTyped;
  433.         }
  434.     }
  435.  
  436.     // }}}
  437.     // {{{ private static function convertToString($value)
  438.  
  439.     /**
  440.     * Converts a value to a string.
  441.     *
  442.     * @param  mixed   $value 
  443.     * @access private
  444.     * @static
  445.     */
  446.     private static function convertToString($value{
  447.         foreach ($value as $k => $v{
  448.             if (is_array($v)) {
  449.                 $value[$k= self::convertToString($value[$k]);
  450.             else if (is_object($v)) {
  451.                 $value[$k= self::objectToString($value[$k]);
  452.             else {
  453.                 settype($value[$k]'string');
  454.             }
  455.         }
  456.  
  457.         return $value;
  458.     }
  459.  
  460.     // }}}
  461.     // {{{ private static function failSame($message)
  462.  
  463.     /**
  464.     * @param  string  $message 
  465.     * @throws PHPUnit2_Framework_AssertionFailedError
  466.     * @access private
  467.     * @static
  468.     */
  469.     private static function failSame($message{
  470.         self::fail(
  471.           sprintf(
  472.             '%s%sexpected not same',
  473.  
  474.             $message,
  475.             ($message != ''' ' ''
  476.           )
  477.         );
  478.     }
  479.  
  480.     // }}}
  481.     // {{{ private static function failNotSame($expected, $actual, $message)
  482.  
  483.     /**
  484.     * @param  mixed   $expected 
  485.     * @param  mixed   $actual 
  486.     * @param  string  $message 
  487.     * @throws PHPUnit2_Framework_AssertionFailedError
  488.     * @access private
  489.     * @static
  490.     */
  491.     private static function failNotSame($expected$actual$message{
  492.         if (is_string($expected&& is_string($actual)) {
  493.             throw new PHPUnit2_Framework_ComparisonFailure($expected$actual$message);
  494.         }
  495.  
  496.         self::fail(
  497.           sprintf(
  498.             '%s%sexpected same: <%s> was not: <%s>',
  499.  
  500.             $message,
  501.             ($message != ''' ' '',
  502.             self::objectToString($expected),
  503.             self::objectToString($actual)
  504.           )
  505.         );
  506.     }
  507.  
  508.     // }}}
  509.     // {{{ private static function failNotEquals($expected, $actual, $message)
  510.  
  511.     /**
  512.     * @param  mixed   $expected 
  513.     * @param  mixed   $actual 
  514.     * @param  string  $message 
  515.     * @throws PHPUnit2_Framework_AssertionFailedError
  516.     * @access private
  517.     * @static
  518.     */
  519.     private static function failNotEquals($expected$actual$message{
  520.         self::fail(self::format($expected$actual$message));
  521.     }
  522.  
  523.     // }}}
  524.     // {{{ private static function failContains($needle, $haystack, $message)
  525.  
  526.     /**
  527.     * @param  mixed   $needle 
  528.     * @param  mixed   $haystack 
  529.     * @param  string  $message 
  530.     * @throws PHPUnit2_Framework_AssertionFailedError
  531.     * @access private
  532.     * @static
  533.     * @since  2.1.2
  534.     */
  535.     private static function failContains($needle$haystack$message{
  536.         self::fail(
  537.           sprintf(
  538.             '%s%s"%s" contains "%s"',
  539.  
  540.             $message,
  541.             ($message != ''' ' '',
  542.             self::objectToString($haystack),
  543.             self::objectToString($needle)
  544.           )
  545.         );
  546.     }
  547.  
  548.     // }}}
  549.     // {{{ private static function failNotContains($needle, $haystack, $message)
  550.  
  551.     /**
  552.     * @param  mixed   $needle 
  553.     * @param  mixed   $haystack 
  554.     * @param  string  $message 
  555.     * @throws PHPUnit2_Framework_AssertionFailedError
  556.     * @access private
  557.     * @static
  558.     * @since  2.1.2
  559.     */
  560.     private static function failNotContains($needle$haystack$message{
  561.         self::fail(
  562.           sprintf(
  563.             '%s%s"%s" does not contain "%s"',
  564.  
  565.             $message,
  566.             ($message != ''' ' '',
  567.             self::objectToString($haystack),
  568.             self::objectToString($needle)
  569.           )
  570.         );
  571.     }
  572.  
  573.     // }}}
  574.     // {{{ private static function objectToString($object)
  575.  
  576.     /**
  577.     * @param  mixed   $object 
  578.     * @return string 
  579.     * @access private
  580.     * @static
  581.     */
  582.     private static function objectToString($object{
  583.         if (is_array($object|| is_object($object)) {
  584.             $object serialize($object);
  585.         }
  586.  
  587.         return $object;
  588.     }
  589.  
  590.     // }}}
  591.     // {{{ private static function sortArrayRecursively(&$array) {
  592.  
  593.     /**
  594.     * Sorts an array recursively by its keys.
  595.     *
  596.     * @param  array $array 
  597.     * @access private
  598.     * @static
  599.     * @author Adam Maccabee Trachtenberg <adam@trachtenberg.com>
  600.     */
  601.     private static function sortArrayRecursively(&$array{
  602.         ksort($array);
  603.  
  604.         foreach($array as $k => $v{
  605.             if (is_array($v)) {
  606.                 self::sortArrayRecursively($array[$k]);
  607.             }
  608.         }
  609.     }
  610.  
  611.     // }}}
  612. }
  613.  
  614. /*
  615.  * vim600:  et sw=2 ts=2 fdm=marker
  616.  * vim<600: et sw=2 ts=2
  617.  */
  618. ?>

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