home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Assert.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  7.4 KB  |  299 lines

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit                                                        |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2003 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.10.2.2 2003/06/21 05:05:14 sebastian Exp $
  16. //
  17.  
  18. /**
  19.  * A set of assert methods.
  20.  *
  21.  * @package PHPUnit
  22.  * @author  Sebastian Bergmann <sb@sebastian-bergmann.de>
  23.  *          Based upon JUnit, see http://www.junit.org/ for details.
  24.  */
  25. class PHPUnit_Assert {
  26.     /**
  27.     * @var    boolean
  28.     * @access private
  29.     */
  30.     var $_looselyTyped = false;
  31.  
  32.     /**
  33.     * Asserts that two variables are equal.
  34.     *
  35.     * @param  mixed
  36.     * @param  mixed
  37.     * @param  string
  38.     * @param  mixed
  39.     * @access public
  40.     */
  41.     function assertEquals($expected, $actual, $message = '', $delta = 0) {
  42.         if ((is_array($actual)  && is_array($expected)) ||
  43.             (is_object($actual) && is_object($expected))) {
  44.             if (is_array($actual) && is_array($expected)) {
  45.                 ksort($actual);
  46.                 ksort($expected);
  47.             }
  48.  
  49.             if ($this->_looselyTyped) {
  50.                 $actual   = $this->_convertToString($actual);
  51.                 $expected = $this->_convertToString($expected);
  52.             }
  53.  
  54.             $actual   = serialize($actual);
  55.             $expected = serialize($expected);
  56.  
  57.             $message = sprintf(
  58.               '%sexpected %s, actual %s',
  59.  
  60.               !empty($message) ? $message . ' ' : '',
  61.               $expected,
  62.               $actual
  63.             );
  64.  
  65.             if ($actual !== $expected) {
  66.                 return $this->fail($message);
  67.             }
  68.         }
  69.  
  70.         elseif (is_numeric($actual) && is_numeric($expected)) {
  71.             $message = sprintf(
  72.               '%sexpected %s%s, actual %s',
  73.  
  74.               !empty($message) ? $message . ' ' : '',
  75.               $expected,
  76.               ($delta != 0) ? ('+/- ' . $delta) : '',
  77.               $actual
  78.             );
  79.  
  80.             if (!($actual >= ($expected - $delta) && $actual <= ($expected + $delta))) {
  81.                 return $this->fail($message);
  82.             }
  83.         }
  84.  
  85.         else {
  86.             $message = sprintf(
  87.               '%sexpected %s, actual %s',
  88.  
  89.               !empty($message) ? $message . ' ' : '',
  90.               $expected,
  91.               $actual
  92.             );
  93.  
  94.             if ($actual != $expected) {
  95.                 return $this->fail($message);
  96.             }
  97.         }
  98.     }
  99.  
  100.     /**
  101.     * Asserts that an object isn't null.
  102.     *
  103.     * @param  object
  104.     * @param  string
  105.     * @access public
  106.     */
  107.     function assertNotNull($object, $message = '') {
  108.         $message = sprintf(
  109.           '%sexpected NOT NULL, actual NULL',
  110.  
  111.           !empty($message) ? $message . ' ' : ''
  112.         );
  113.  
  114.         if ($object === null) {
  115.             return $this->fail($message);
  116.         }
  117.     }
  118.  
  119.     /**
  120.     * Asserts that an object is null.
  121.     *
  122.     * @param  object
  123.     * @param  string
  124.     * @access public
  125.     */
  126.     function assertNull($object, $message = '') {
  127.         $message = sprintf(
  128.           '%sexpected NULL, actual NOT NULL',
  129.  
  130.           !empty($message) ? $message . ' ' : ''
  131.         );
  132.  
  133.         if ($object !== null) {
  134.             return $this->fail($message);
  135.         }
  136.     }
  137.  
  138.     /**
  139.     * Asserts that two objects refer to the same object.
  140.     * This requires the Zend Engine 2 (to work properly).
  141.     *
  142.     * @param  object
  143.     * @param  object
  144.     * @param  string
  145.     * @access public
  146.     */
  147.     function assertSame($expected, $actual, $message = '') {
  148.         $message = sprintf(
  149.           '%sexpected two variables to refer to the same object',
  150.  
  151.           !empty($message) ? $message . ' ' : ''
  152.         );
  153.  
  154.         if ($actual !== $expected) {
  155.             return $this->fail($message);
  156.         }
  157.     }
  158.  
  159.     /**
  160.     * Asserts that two objects refer not to the same object.
  161.     * This requires the Zend Engine 2 (to work properly).
  162.     *
  163.     * @param  object
  164.     * @param  object
  165.     * @param  string
  166.     * @access public
  167.     */
  168.     function assertNotSame($expected, $actual, $message = '') {
  169.         $message = sprintf(
  170.           '%sexpected two variables to refer to different objects',
  171.  
  172.           !empty($message) ? $message . ' ' : ''
  173.         );
  174.  
  175.         if ($actual === $expected) {
  176.             return $this->fail($message);
  177.         }
  178.     }
  179.  
  180.     /**
  181.     * Asserts that a condition is true.
  182.     *
  183.     * @param  boolean
  184.     * @param  string
  185.     * @access public
  186.     */
  187.     function assertTrue($condition, $message = '') {
  188.         $message = sprintf(
  189.           '%sexpected true, actual false',
  190.  
  191.           !empty($message) ? $message . ' ' : ''
  192.         );
  193.  
  194.         if (!$condition) {
  195.             return $this->fail($message);
  196.         }
  197.     }
  198.  
  199.     /**
  200.     * Asserts that a condition is false.
  201.     *
  202.     * @param  boolean
  203.     * @param  string
  204.     * @access public
  205.     */
  206.     function assertFalse($condition, $message = '') {
  207.         $message = sprintf(
  208.           '%sexpected false, actual true',
  209.  
  210.           !empty($message) ? $message . ' ' : ''
  211.         );
  212.  
  213.         if ($condition) {
  214.             return $this->fail($message);
  215.         }
  216.     }
  217.  
  218.     /**
  219.     * Asserts that a string matches a given
  220.     * regular expression.
  221.     *
  222.     * @param string
  223.     * @param string
  224.     * @param string
  225.     * @access public
  226.     * @author SΘbastien Hordeaux <marms@marms.com>
  227.     */
  228.     function assertRegExp($expected, $actual, $message = '') {
  229.         $message = sprintf(
  230.           '%sexpected %s, actual %s',
  231.  
  232.           !empty($message) ? $message . ' ' : '',
  233.           $expected,
  234.           $actual
  235.         );
  236.  
  237.         if (!preg_match($expected, $actual)) {
  238.             return $this->fail($message);
  239.         }
  240.     }
  241.         
  242.     /**
  243.     * Asserts that a variable is of a given type.
  244.     *
  245.     * @param  string          $expected
  246.     * @param  mixed           $actual
  247.     * @param  optional string $message
  248.     * @access public
  249.     * @static
  250.     */
  251.     function assertType($expected, $actual, $message = '') {
  252.         return $this->assertEquals(
  253.           $expected,
  254.           gettype($actual),
  255.           $message
  256.         );
  257.     }
  258.  
  259.     /**
  260.     * Converts a value to a string.
  261.     *
  262.     * @param  mixed   $value
  263.     * @access private
  264.     * @static
  265.     */
  266.     function _convertToString($value) {
  267.         foreach ($value as $k => $v) {
  268.             if (is_array($v)) {
  269.                 $value[$k] = $this->_convertToString($value[$k]);
  270.             } else {
  271.                 settype($value[$k], 'string');
  272.             }
  273.         }
  274.  
  275.         return $value;
  276.     }
  277.  
  278.     /**
  279.     * @param  boolean $looselyTyped
  280.     * @access public
  281.     * @static
  282.     */
  283.     function setLooselyTyped($looselyTyped) {
  284.         if (is_bool($looselyTyped)) {
  285.             $this->_looselyTyped = $looselyTyped;
  286.         }
  287.     }
  288.  
  289.     /**
  290.     * Fails a test with the given message.
  291.     *
  292.     * @param  string
  293.     * @access protected
  294.     * @abstract
  295.     */
  296.     function fail($message = '') { /* abstract */ }
  297. }
  298. ?>
  299.