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

Source for file Assert.php

Documentation is available at Assert.php

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit                                                        |
  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.25 2005/01/31 04:57:16 sebastian Exp $
  16. //
  17.  
  18. /**
  19.  * A set of assert methods.
  20.  *
  21.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  22.  * @copyright   Copyright &copy; 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
  23.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  24.  * @category    Testing
  25.  * @package     PHPUnit
  26.  */
  27. class PHPUnit_Assert {
  28.     /**
  29.     * @var    boolean 
  30.     * @access private
  31.     */
  32.     var $_looselyTyped = FALSE;
  33.  
  34.     /**
  35.     * Asserts that a haystack contains a needle.
  36.     *
  37.     * @param  mixed 
  38.     * @param  mixed 
  39.     * @param  string 
  40.     * @access public
  41.     * @since  1.1.0
  42.     */
  43.     function assertContains($needle$haystack$message ''{
  44.         if (is_string($needle&& is_string($haystack)) {
  45.             $this->assertTrue(strpos($haystack$needle!== FALSE ? TRUE : FALSE);
  46.         }
  47.  
  48.         else if (is_array($haystack&& !is_object($needle)) {
  49.             $this->assertTrue(in_array($needle$haystack)$message);
  50.         }
  51.  
  52.         else {
  53.             $this->fail('Unsupported parameter passed to assertContains().');
  54.         }
  55.     }
  56.  
  57.     /**
  58.     * Asserts that a haystack does not contain a needle.
  59.     *
  60.     * @param  mixed 
  61.     * @param  mixed 
  62.     * @param  string 
  63.     * @access public
  64.     * @since  1.1.0
  65.     */
  66.     function assertNotContains($needle$haystack$message ''{
  67.         if (is_string($needle&& is_string($haystack)) {
  68.             $this->assertFalse(strpos($haystack$needle!== FALSE ? TRUE : FALSE);
  69.         }
  70.  
  71.         else if (is_array($haystack&& !is_object($needle)) {
  72.             $this->assertFalse(in_array($needle$haystack)$message);
  73.         }
  74.  
  75.         else {
  76.             $this->fail('Unsupported parameter passed to assertNotContains().');
  77.         }
  78.     }
  79.  
  80.     /**
  81.     * Asserts that two variables are equal.
  82.     *
  83.     * @param  mixed 
  84.     * @param  mixed 
  85.     * @param  string 
  86.     * @param  mixed 
  87.     * @access public
  88.     */
  89.     function assertEquals($expected$actual$message ''$delta = 0{
  90.         if ((is_array($actual)  && is_array($expected)) ||
  91.             (is_object($actual&& is_object($expected))) {
  92.             if (is_array($actual&& is_array($expected)) {
  93.                 ksort($actual);
  94.                 ksort($expected);
  95.             }
  96.  
  97.             if ($this->_looselyTyped{
  98.                 $actual   $this->_convertToString($actual);
  99.                 $expected $this->_convertToString($expected);
  100.             }
  101.  
  102.             $actual   serialize($actual);
  103.             $expected serialize($expected);
  104.  
  105.             $message sprintf(
  106.               '%sexpected %s, actual %s',
  107.  
  108.               !empty($message$message ' ' '',
  109.               $expected,
  110.               $actual
  111.             );
  112.  
  113.             if ($actual !== $expected{
  114.                 return $this->fail($message);
  115.             }
  116.         }
  117.  
  118.         elseif (is_numeric($actual&& is_numeric($expected)) {
  119.             $message sprintf(
  120.               '%sexpected %s%s, actual %s',
  121.  
  122.               !empty($message$message ' ' '',
  123.               $expected,
  124.               ($delta != 0('+/- ' $delta'',
  125.               $actual
  126.             );
  127.  
  128.             if (!($actual >= ($expected $delta&& $actual <= ($expected $delta))) {
  129.                 return $this->fail($message);
  130.             }
  131.         }
  132.  
  133.         else {
  134.             $message sprintf(
  135.               '%sexpected %s, actual %s',
  136.  
  137.               !empty($message$message ' ' '',
  138.               $expected,
  139.               $actual
  140.             );
  141.  
  142.             if ($actual !== $expected{
  143.                 return $this->fail($message);
  144.             }
  145.         }
  146.     }
  147.  
  148.     /**
  149.     * Asserts that two variables reference the same object.
  150.     * This requires the Zend Engine 2 to work.
  151.     *
  152.     * @param  object 
  153.     * @param  object 
  154.     * @param  string 
  155.     * @access public
  156.     * @deprecated
  157.     */
  158.     function assertSame($expected$actual$message ''{
  159.         if (!version_compare(phpversion()'5.0.0''>=')) {
  160.             $this->fail('assertSame() only works with PHP >= 5.0.0.');
  161.         }
  162.  
  163.         if ((is_object($expected|| is_null($expected)) &&
  164.             (is_object($actual)   || is_null($actual))) {
  165.             $message sprintf(
  166.               '%sexpected two variables to reference the same object',
  167.  
  168.               !empty($message$message ' ' ''
  169.             );
  170.  
  171.             if ($expected !== $actual{
  172.                 return $this->fail($message);
  173.             }
  174.         else {
  175.             $this->fail('Unsupported parameter passed to assertSame().');
  176.         }
  177.     }
  178.  
  179.     /**
  180.     * Asserts that two variables do not reference the same object.
  181.     * This requires the Zend Engine 2 to work.
  182.     *
  183.     * @param  object 
  184.     * @param  object 
  185.     * @param  string 
  186.     * @access public
  187.     * @deprecated
  188.     */
  189.     function assertNotSame($expected$actual$message ''{
  190.         if (!version_compare(phpversion()'5.0.0''>=')) {
  191.             $this->fail('assertNotSame() only works with PHP >= 5.0.0.');
  192.         }
  193.  
  194.         if ((is_object($expected|| is_null($expected)) &&
  195.             (is_object($actual)   || is_null($actual))) {
  196.             $message sprintf(
  197.               '%sexpected two variables to reference different objects',
  198.  
  199.               !empty($message$message ' ' ''
  200.             );
  201.  
  202.             if ($expected === $actual{
  203.                 return $this->fail($message);
  204.             }
  205.         else {
  206.             $this->fail('Unsupported parameter passed to assertNotSame().');
  207.         }
  208.     }
  209.  
  210.     /**
  211.     * Asserts that a variable is not NULL.
  212.     *
  213.     * @param  mixed 
  214.     * @param  string 
  215.     * @access public
  216.     */
  217.     function assertNotNull($actual$message ''{
  218.         $message sprintf(
  219.           '%sexpected NOT NULL, actual NULL',
  220.  
  221.           !empty($message$message ' ' ''
  222.         );
  223.  
  224.         if (is_null($actual)) {
  225.             return $this->fail($message);
  226.         }
  227.     }
  228.  
  229.     /**
  230.     * Asserts that a variable is NULL.
  231.     *
  232.     * @param  mixed 
  233.     * @param  string 
  234.     * @access public
  235.     */
  236.     function assertNull($actual$message ''{
  237.         $message sprintf(
  238.           '%sexpected NULL, actual NOT NULL',
  239.  
  240.           !empty($message$message ' ' ''
  241.         );
  242.  
  243.         if (!is_null($actual)) {
  244.             return $this->fail($message);
  245.         }
  246.     }
  247.  
  248.     /**
  249.     * Asserts that a condition is true.
  250.     *
  251.     * @param  boolean 
  252.     * @param  string 
  253.     * @access public
  254.     */
  255.     function assertTrue($condition$message ''{
  256.         $message sprintf(
  257.           '%sexpected TRUE, actual FALSE',
  258.  
  259.           !empty($message$message ' ' ''
  260.         );
  261.  
  262.         if (!$condition{
  263.             return $this->fail($message);
  264.         }
  265.     }
  266.  
  267.     /**
  268.     * Asserts that a condition is false.
  269.     *
  270.     * @param  boolean 
  271.     * @param  string 
  272.     * @access public
  273.     */
  274.     function assertFalse($condition$message ''{
  275.         $message sprintf(
  276.           '%sexpected FALSE, actual TRUE',
  277.  
  278.           !empty($message$message ' ' ''
  279.         );
  280.  
  281.         if ($condition{
  282.             return $this->fail($message);
  283.         }
  284.     }
  285.  
  286.     /**
  287.     * Asserts that a string matches a given regular expression.
  288.     *
  289.     * @param  string 
  290.     * @param  string 
  291.     * @param  string 
  292.     * @access public
  293.     */
  294.     function assertRegExp($pattern$string$message ''{
  295.         $message sprintf(
  296.           '%s"%s" does not match pattern "%s"',
  297.  
  298.           !empty($message$message ' ' '',
  299.           $string,
  300.           $pattern
  301.         );
  302.  
  303.         if (!preg_match($pattern$string)) {
  304.             return $this->fail($message);
  305.         }
  306.     }
  307.  
  308.     /**
  309.     * Asserts that a string does not match a given regular expression.
  310.     *
  311.     * @param  string 
  312.     * @param  string 
  313.     * @param  string 
  314.     * @access public
  315.     * @since  1.1.0
  316.     */
  317.     function assertNotRegExp($pattern$string$message ''{
  318.         $message sprintf(
  319.           '%s"%s" matches pattern "%s"',
  320.  
  321.           !empty($message$message ' ' '',
  322.           $string,
  323.           $pattern
  324.         );
  325.  
  326.         if (preg_match($pattern$string)) {
  327.             return $this->fail($message);
  328.         }
  329.     }
  330.  
  331.     /**
  332.     * Asserts that a variable is of a given type.
  333.     *
  334.     * @param  string          $expected 
  335.     * @param  mixed           $actual 
  336.     * @param  optional string $message
  337.     * @access public
  338.     */
  339.     function assertType($expected$actual$message ''{
  340.         return $this->assertEquals(
  341.           $expected,
  342.           gettype($actual),
  343.           $message
  344.         );
  345.     }
  346.  
  347.     /**
  348.     * Converts a value to a string.
  349.     *
  350.     * @param  mixed   $value 
  351.     * @access private
  352.     */
  353.     function _convertToString($value{
  354.         foreach ($value as $k => $v{
  355.             if (is_array($v)) {
  356.                 $value[$k$this->_convertToString($value[$k]);
  357.             else {
  358.                 settype($value[$k]'string');
  359.             }
  360.         }
  361.  
  362.         return $value;
  363.     }
  364.  
  365.     /**
  366.     * @param  boolean $looselyTyped 
  367.     * @access public
  368.     */
  369.     function setLooselyTyped($looselyTyped{
  370.         if (is_bool($looselyTyped)) {
  371.             $this->_looselyTyped $looselyTyped;
  372.         }
  373.     }
  374.  
  375.     /**
  376.     * Fails a test with the given message.
  377.     *
  378.     * @param  string 
  379.     * @access protected
  380.     * @abstract
  381.     */
  382.     function fail($message ''/* abstract */ }
  383. }
  384. ?>

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