home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / PHPUnit2 / Framework / Assert.php next >
Encoding:
PHP Script  |  2008-07-02  |  17.0 KB  |  627 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * PHP Version 5
  6.  *
  7.  * Copyright (c) 2002-2006, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  *
  14.  *   * Redistributions of source code must retain the above copyright
  15.  *     notice, this list of conditions and the following disclaimer.
  16.  * 
  17.  *   * Redistributions in binary form must reproduce the above copyright
  18.  *     notice, this list of conditions and the following disclaimer in
  19.  *     the documentation and/or other materials provided with the
  20.  *     distribution.
  21.  *
  22.  *   * Neither the name of Sebastian Bergmann nor the names of his
  23.  *     contributors may be used to endorse or promote products derived
  24.  *     from this software without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29.  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30.  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
  35.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37.  * POSSIBILITY OF SUCH DAMAGE.
  38.  *
  39.  * @category   Testing
  40.  * @package    PHPUnit2
  41.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  42.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  43.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  44.  * @version    CVS: $Id: Assert.php,v 1.45.2.4 2005/12/17 16:04:56 sebastian Exp $
  45.  * @link       http://pear.php.net/package/PHPUnit2
  46.  * @since      File available since Release 2.0.0
  47.  */
  48.  
  49. require_once 'PHPUnit2/Framework/AssertionFailedError.php';
  50. require_once 'PHPUnit2/Framework/ComparisonFailure.php';
  51.  
  52. /**
  53.  * A set of assert methods.
  54.  *
  55.  * @category   Testing
  56.  * @package    PHPUnit2
  57.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  58.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  59.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  60.  * @version    Release: 2.3.6
  61.  * @link       http://pear.php.net/package/PHPUnit2
  62.  * @since      Class available since Release 2.0.0
  63.  * @static
  64.  */
  65. class PHPUnit2_Framework_Assert {
  66.     /**
  67.      * @var    boolean
  68.      * @access private
  69.      * @static
  70.      */
  71.     private static $looselyTyped = FALSE;
  72.  
  73.     /**
  74.      * Protect constructor since it is a static only class.
  75.      *
  76.      * @access protected
  77.      */
  78.     protected function __construct() {
  79.     }
  80.  
  81.     /**
  82.      * Asserts that a haystack contains a needle.
  83.      *
  84.      * @param  mixed   $needle
  85.      * @param  mixed   $haystack
  86.      * @param  string  $message
  87.      * @access public
  88.      * @static
  89.      * @since  Method available since Release 2.1.0
  90.      */
  91.     public static function assertContains($needle, $haystack, $message = '') {
  92.         self::doAssertContains($needle, $haystack, TRUE, $message);
  93.     }
  94.  
  95.     /**
  96.      * Asserts that a haystack does not contain a needle.
  97.      *
  98.      * @param  mixed   $needle
  99.      * @param  mixed   $haystack
  100.      * @param  string  $message
  101.      * @access public
  102.      * @static
  103.      * @since  Method available since Release 2.1.0
  104.      */
  105.     public static function assertNotContains($needle, $haystack, $message = '') {
  106.         self::doAssertContains($needle, $haystack, FALSE, $message);
  107.     }
  108.  
  109.     /**
  110.      * @param  mixed   $needle
  111.      * @param  mixed   $haystack
  112.      * @param  boolean $condition
  113.      * @param  string  $message
  114.      * @throws Exception
  115.      * @access private
  116.      * @static
  117.      * @since  Method available since Release 2.2.0
  118.      */
  119.     private static function doAssertContains($needle, $haystack, $condition, $message) {
  120.         $found = FALSE;
  121.  
  122.         if (is_array($haystack) ||
  123.            (is_object($haystack) && $haystack instanceof Iterator)) {
  124.             foreach ($haystack as $straw) {
  125.                 if ($straw === $needle) {
  126.                     $found = TRUE;
  127.                     break;
  128.                 }
  129.             }
  130.         }
  131.  
  132.         else if (is_string($needle) && is_string($haystack)) {
  133.             if (strpos($haystack, $needle) !== FALSE) {
  134.                 $found = TRUE;
  135.             }
  136.         }
  137.  
  138.         else {
  139.             throw new Exception;
  140.         }
  141.  
  142.         if ($condition && !$found) {
  143.             self::fail(
  144.               sprintf(
  145.                 '%s%s"%s" does not contain "%s"',
  146.  
  147.                 $message,
  148.                 ($message != '') ? ' ' : '',
  149.                 self::objectToString($haystack),
  150.                 self::objectToString($needle)
  151.               )
  152.             );
  153.         }
  154.  
  155.         else if (!$condition && $found) {
  156.             self::fail(
  157.               sprintf(
  158.                 '%s%s"%s" contains "%s"',
  159.  
  160.                 $message,
  161.                 ($message != '') ? ' ' : '',
  162.                 self::objectToString($haystack),
  163.                 self::objectToString($needle)
  164.               )
  165.             );
  166.         }
  167.     }
  168.  
  169.     /**
  170.      * Asserts that two variables are equal.
  171.      *
  172.      * @param  mixed  $expected
  173.      * @param  mixed  $actual
  174.      * @param  string $message
  175.      * @param  mixed  $delta
  176.      * @access public
  177.      * @static
  178.      */
  179.     public static function assertEquals($expected, $actual, $message = '', $delta = 0) {
  180.         self::doAssertEquals($expected, $actual, $delta, TRUE, $message);
  181.     }
  182.  
  183.     /**
  184.      * Asserts that two variables are not equal.
  185.      *
  186.      * @param  mixed  $expected
  187.      * @param  mixed  $actual
  188.      * @param  string $message
  189.      * @param  mixed  $delta
  190.      * @access public
  191.      * @static
  192.      * @since  Method available since Release 2.3.0
  193.      */
  194.     public static function assertNotEquals($expected, $actual, $message = '', $delta = 0) {
  195.         self::doAssertEquals($expected, $actual, $delta, FALSE, $message);
  196.     }
  197.  
  198.     /**
  199.      * @param  mixed   $expected
  200.      * @param  mixed   $actual
  201.      * @param  mixed   $delta
  202.      * @param  boolean $condition
  203.      * @param  string  $message
  204.      * @access private
  205.      * @static
  206.      * @since  Method available since Release 2.3.0
  207.      */
  208.     private static function doAssertEquals($expected, $actual, $delta, $condition, $message) {
  209.         $equal = FALSE;
  210.  
  211.         if (is_array($expected)) {
  212.             if (is_array($actual)) {
  213.                 self::sortArrayRecursively($actual);
  214.                 self::sortArrayRecursively($expected);
  215.  
  216.                 if (self::$looselyTyped) {
  217.                     $actual   = self::convertToString($actual);
  218.                     $expected = self::convertToString($expected);
  219.                 }
  220.  
  221.                 $equal = (serialize($expected) == serialize($actual));
  222.             }
  223.         }
  224.  
  225.         else if (is_float($expected) && is_float($actual) && is_float($delta)) {
  226.             $equal = (abs($expected - $actual) <= $delta);
  227.         }
  228.  
  229.         else {
  230.             $equal = (serialize($expected) == serialize($actual));
  231.         }
  232.  
  233.         if ($condition && !$equal) {
  234.             self::failNotSame(
  235.               $expected,
  236.               $actual,
  237.               $message
  238.             );
  239.         }
  240.  
  241.         else if (!$condition && $equal) {
  242.             self::failSame(
  243.               $expected,
  244.               $actual,
  245.               $message
  246.             );
  247.         }
  248.     }
  249.  
  250.     /**
  251.      * Asserts that a condition is true.
  252.      *
  253.      * @param  boolean $condition
  254.      * @param  string  $message
  255.      * @throws Exception
  256.      * @access public
  257.      * @static
  258.      */
  259.     public static function assertTrue($condition, $message = '') {
  260.         if (is_bool($condition)) {
  261.             if (!$condition) {
  262.                 self::fail($message);
  263.             }
  264.         } else {
  265.             throw new Exception;
  266.         }
  267.     }
  268.  
  269.     /**
  270.      * Asserts that a condition is false.
  271.      *
  272.      * @param  boolean  $condition
  273.      * @param  string   $message
  274.      * @throws Exception
  275.      * @access public
  276.      * @static
  277.      */
  278.     public static function assertFalse($condition, $message = '') {
  279.         if (is_bool($condition)) {
  280.             self::assertTrue(!$condition, $message);
  281.         } else {
  282.             throw new Exception;
  283.         }
  284.     }
  285.  
  286.     /**
  287.      * Asserts that a variable is not NULL.
  288.      *
  289.      * @param  mixed  $actual
  290.      * @param  string $message
  291.      * @access public
  292.      * @static
  293.      */
  294.     public static function assertNotNull($actual, $message = '') {
  295.         if (is_null($actual)) {
  296.             self::fail(self::format('NOT NULL', 'NULL', $message));
  297.         }
  298.     }
  299.  
  300.     /**
  301.      * Asserts that a variable is NULL.
  302.      *
  303.      * @param  mixed  $actual
  304.      * @param  string $message
  305.      * @access public
  306.      * @static
  307.      */
  308.     public static function assertNull($actual, $message = '') {
  309.         if (!is_null($actual)) {
  310.             self::fail(self::format('NULL', 'NOT NULL', $message));
  311.         }
  312.     }
  313.  
  314.     /**
  315.      * Asserts that two variables have the same type and value.
  316.      * Used on objects, it asserts that two variables reference
  317.      * the same object.
  318.      *
  319.      * @param  mixed  $expected
  320.      * @param  mixed  $actual
  321.      * @param  string $message
  322.      * @access public
  323.      * @static
  324.      */
  325.     public static function assertSame($expected, $actual, $message = '') {
  326.         if ($expected !== $actual) {
  327.             self::failNotSame($expected, $actual, $message);
  328.         }
  329.     }
  330.  
  331.     /**
  332.      * Asserts that two variables do not have the same type and value.
  333.      * Used on objects, it asserts that two variables do not reference
  334.      * the same object.
  335.      *
  336.      * @param  mixed  $expected
  337.      * @param  mixed  $actual
  338.      * @param  string $message
  339.      * @access public
  340.      * @static
  341.      */
  342.     public static function assertNotSame($expected, $actual, $message = '') {
  343.         if ($expected === $actual) {
  344.             self::failSame($expected, $actual, $message);
  345.         }
  346.     }
  347.  
  348.     /**
  349.      * Asserts that a variable is of a given type.
  350.      *
  351.      * @param  string $expected
  352.      * @param  mixed  $actual
  353.      * @param  string $message
  354.      * @access public
  355.      * @static
  356.      */
  357.     public static function assertType($expected, $actual, $message = '') {
  358.         self::doAssertType($expected, $actual, TRUE, $message);
  359.     }
  360.  
  361.     /**
  362.      * Asserts that a variable is not of a given type.
  363.      *
  364.      * @param  string $expected
  365.      * @param  mixed  $actual
  366.      * @param  string $message
  367.      * @access public
  368.      * @static
  369.      * @since  Method available since Release 2.2.0
  370.      */
  371.     public static function assertNotType($expected, $actual, $message = '') {
  372.         self::doAssertType($expected, $actual, FALSE, $message);
  373.     }
  374.  
  375.     /**
  376.      * @param  string  $expected
  377.      * @param  mixed   $actual
  378.      * @param  boolean $condition
  379.      * @param  string  $message
  380.      * @access private
  381.      * @static
  382.      * @since  Method available since Release 2.2.0
  383.      */
  384.     private static function doAssertType($expected, $actual, $condition, $message) {
  385.         if (!is_string($expected)) {
  386.             throw new Exception;
  387.         }
  388.  
  389.         if (is_object($actual)) {
  390.             $result = $actual instanceof $expected;
  391.         } else {
  392.             $result = (gettype($actual) == $expected);
  393.         }
  394.  
  395.         if ($condition && !$result) {
  396.             self::failNotSame(
  397.               $expected,
  398.               $actual,
  399.               $message
  400.             );
  401.         }
  402.  
  403.         else if (!$condition && $result) {
  404.             self::failSame(
  405.               $expected,
  406.               $actual,
  407.               $message
  408.             );
  409.         }
  410.     }
  411.  
  412.     /**
  413.      * Asserts that a string matches a given regular expression.
  414.      *
  415.      * @param  string $pattern
  416.      * @param  string $string
  417.      * @param  string $message
  418.      * @access public
  419.      * @static
  420.      */
  421.     public static function assertRegExp($pattern, $string, $message = '') {
  422.         self::doAssertRegExp($pattern, $string, TRUE, $message);
  423.     }
  424.  
  425.     /**
  426.      * Asserts that a string does not match a given regular expression.
  427.      *
  428.      * @param  string $pattern
  429.      * @param  string $string
  430.      * @param  string $message
  431.      * @access public
  432.      * @static
  433.      * @since  Method available since Release 2.1.0
  434.      */
  435.     public static function assertNotRegExp($pattern, $string, $message = '') {
  436.         self::doAssertRegExp($pattern, $string, FALSE, $message);
  437.     }
  438.  
  439.     /**
  440.      * @param  mixed   $pattern
  441.      * @param  mixed   $string
  442.      * @param  boolean $condition
  443.      * @param  string  $message
  444.      * @access private
  445.      * @static
  446.      * @since  Method available since Release 2.2.0
  447.      */
  448.     private static function doAssertRegExp($pattern, $string, $condition, $message) {
  449.         if (!is_string($pattern) || !is_string($string)) {
  450.             throw new Exception;
  451.         }
  452.  
  453.         $result = preg_match($pattern, $string);
  454.  
  455.         if ($condition && !$result) {
  456.             self::fail(
  457.               sprintf(
  458.                 '%s%s"%s" does not match pattern "%s"',
  459.  
  460.                 $message,
  461.                 ($message != '') ? ' ' : '',
  462.                 $string,
  463.                 $pattern
  464.               )
  465.             );
  466.         }
  467.  
  468.         else if (!$condition && $result) {
  469.             self::fail(
  470.               sprintf(
  471.                 '%s%s"%s" matches pattern "%s"',
  472.  
  473.                 $message,
  474.                 ($message != '') ? ' ' : '',
  475.                 $string,
  476.                 $pattern
  477.               )
  478.             );
  479.         }
  480.     }
  481.  
  482.     /**
  483.      * Fails a test with the given message.
  484.      *
  485.      * @param  string $message
  486.      * @throws PHPUnit2_Framework_AssertionFailedError
  487.      * @access public
  488.      * @static
  489.      */
  490.     public static function fail($message = '') {
  491.         throw new PHPUnit2_Framework_AssertionFailedError($message);
  492.     }
  493.  
  494.     /**
  495.      * @param  string  $message
  496.      * @throws PHPUnit2_Framework_AssertionFailedError
  497.      * @access private
  498.      * @static
  499.      */
  500.     private static function failSame($message) {
  501.         self::fail(
  502.           sprintf(
  503.             '%s%sexpected not same',
  504.  
  505.             $message,
  506.             ($message != '') ? ' ' : ''
  507.           )
  508.         );
  509.     }
  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 failNotSame($expected, $actual, $message) {
  520.         if (is_string($expected) && is_string($actual)) {
  521.             throw new PHPUnit2_Framework_ComparisonFailure($expected, $actual, $message);
  522.         }
  523.  
  524.         self::fail(
  525.           sprintf(
  526.             '%s%sexpected same: <%s> was not: <%s>',
  527.  
  528.             $message,
  529.             ($message != '') ? ' ' : '',
  530.             self::objectToString($expected),
  531.             self::objectToString($actual)
  532.           )
  533.         );
  534.     }
  535.  
  536.     /**
  537.      * @param  mixed   $expected
  538.      * @param  mixed   $actual
  539.      * @param  string  $message
  540.      * @access public
  541.      * @static
  542.      */
  543.     public static function format($expected, $actual, $message) {
  544.         return sprintf(
  545.           '%s%sexpected: <%s> but was: <%s>',
  546.  
  547.           $message,
  548.           ($message != '') ? ' ' : '',
  549.           self::objectToString($expected),
  550.           self::objectToString($actual)
  551.         );
  552.     }
  553.  
  554.     /**
  555.      * @param  boolean $looselyTyped
  556.      * @access public
  557.      * @static
  558.      */
  559.     public static function setLooselyTyped($looselyTyped) {
  560.         if (is_bool($looselyTyped)) {
  561.             self::$looselyTyped = $looselyTyped;
  562.         }
  563.     }
  564.  
  565.     /**
  566.      * Converts a value to a string.
  567.      *
  568.      * @param  mixed   $value
  569.      * @access private
  570.      * @static
  571.      */
  572.     private static function convertToString($value) {
  573.         foreach ($value as $k => $v) {
  574.             if (is_array($v)) {
  575.                 $value[$k] = self::convertToString($value[$k]);
  576.             } else if (is_object($v)) {
  577.                 $value[$k] = self::objectToString($value[$k]);
  578.             } else {
  579.                 settype($value[$k], 'string');
  580.             }
  581.         }
  582.  
  583.         return $value;
  584.     }
  585.  
  586.     /**
  587.      * @param  mixed   $object
  588.      * @return string
  589.      * @access private
  590.      * @static
  591.      */
  592.     private static function objectToString($object) {
  593.         if (is_array($object) || is_object($object)) {
  594.             $object = serialize($object);
  595.         }
  596.  
  597.         return $object;
  598.     }
  599.  
  600.     /**
  601.      * Sorts an array recursively by its keys.
  602.      *
  603.      * @param  array $array
  604.      * @access private
  605.      * @static
  606.      * @author Adam Maccabee Trachtenberg <adam@trachtenberg.com>
  607.      */
  608.     private static function sortArrayRecursively(&$array) {
  609.         ksort($array);
  610.  
  611.         foreach($array as $k => $v) {
  612.             if (is_array($v)) {
  613.                 self::sortArrayRecursively($array[$k]);
  614.             }
  615.         }
  616.     }
  617. }
  618.  
  619. /*
  620.  * Local variables:
  621.  * tab-width: 4
  622.  * c-basic-offset: 4
  623.  * c-hanging-comment-ender-p: nil
  624.  * End:
  625.  */
  626. ?>
  627.