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

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