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-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.22 2004/12/21 18:25:14 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-2004 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 an object isn't null.
  212.     *
  213.     * @param  object 
  214.     * @param  string 
  215.     * @access public
  216.     */
  217.     function assertNotNull($object$message ''{
  218.         if (is_object($object|| is_null($object)) {
  219.             $message sprintf(
  220.               '%sexpected NOT NULL, actual NULL',
  221.  
  222.               !empty($message$message ' ' ''
  223.             );
  224.  
  225.             if (is_null($object)) {
  226.                 return $this->fail($message);
  227.             }
  228.         else {
  229.             $this->fail('Unsupported parameter passed to assertNotNull().');
  230.         }
  231.     }
  232.  
  233.     /**
  234.     * Asserts that an object is null.
  235.     *
  236.     * @param  object 
  237.     * @param  string 
  238.     * @access public
  239.     */
  240.     function assertNull($object$message ''{
  241.         if (is_object($object|| is_null($object)) {
  242.             $message sprintf(
  243.               '%sexpected NULL, actual NOT NULL',
  244.  
  245.               !empty($message$message ' ' ''
  246.             );
  247.  
  248.             if (!is_null($object)) {
  249.                 return $this->fail($message);
  250.             }
  251.         else {
  252.             $this->fail('Unsupported parameter passed to assertNotNull().');
  253.         }
  254.     }
  255.  
  256.     /**
  257.     * Asserts that a condition is true.
  258.     *
  259.     * @param  boolean 
  260.     * @param  string 
  261.     * @access public
  262.     */
  263.     function assertTrue($condition$message ''{
  264.         $message sprintf(
  265.           '%sexpected TRUE, actual FALSE',
  266.  
  267.           !empty($message$message ' ' ''
  268.         );
  269.  
  270.         if (!$condition{
  271.             return $this->fail($message);
  272.         }
  273.     }
  274.  
  275.     /**
  276.     * Asserts that a condition is false.
  277.     *
  278.     * @param  boolean 
  279.     * @param  string 
  280.     * @access public
  281.     */
  282.     function assertFalse($condition$message ''{
  283.         $message sprintf(
  284.           '%sexpected FALSE, actual TRUE',
  285.  
  286.           !empty($message$message ' ' ''
  287.         );
  288.  
  289.         if ($condition{
  290.             return $this->fail($message);
  291.         }
  292.     }
  293.  
  294.     /**
  295.     * Asserts that a string matches a given regular expression.
  296.     *
  297.     * @param  string 
  298.     * @param  string 
  299.     * @param  string 
  300.     * @access public
  301.     */
  302.     function assertRegExp($pattern$string$message ''{
  303.         $message sprintf(
  304.           '%s"%s" does not match pattern "%s"',
  305.  
  306.           !empty($message$message ' ' '',
  307.           $string,
  308.           $pattern
  309.         );
  310.  
  311.         if (!preg_match($pattern$string)) {
  312.             return $this->fail($message);
  313.         }
  314.     }
  315.  
  316.     /**
  317.     * Asserts that a string does not match a given regular expression.
  318.     *
  319.     * @param  string 
  320.     * @param  string 
  321.     * @param  string 
  322.     * @access public
  323.     * @since  1.1.0
  324.     */
  325.     function assertNotRegExp($pattern$string$message ''{
  326.         $message sprintf(
  327.           '%s"%s" matches pattern "%s"',
  328.  
  329.           !empty($message$message ' ' '',
  330.           $string,
  331.           $pattern
  332.         );
  333.  
  334.         if (preg_match($pattern$string)) {
  335.             return $this->fail($message);
  336.         }
  337.     }
  338.  
  339.     /**
  340.     * Asserts that a variable is of a given type.
  341.     *
  342.     * @param  string          $expected 
  343.     * @param  mixed           $actual 
  344.     * @param  optional string $message
  345.     * @access public
  346.     */
  347.     function assertType($expected$actual$message ''{
  348.         return $this->assertEquals(
  349.           $expected,
  350.           gettype($actual),
  351.           $message
  352.         );
  353.     }
  354.  
  355.     /**
  356.     * Converts a value to a string.
  357.     *
  358.     * @param  mixed   $value 
  359.     * @access private
  360.     */
  361.     function _convertToString($value{
  362.         foreach ($value as $k => $v{
  363.             if (is_array($v)) {
  364.                 $value[$k$this->_convertToString($value[$k]);
  365.             else {
  366.                 settype($value[$k]'string');
  367.             }
  368.         }
  369.  
  370.         return $value;
  371.     }
  372.  
  373.     /**
  374.     * @param  boolean $looselyTyped 
  375.     * @access public
  376.     */
  377.     function setLooselyTyped($looselyTyped{
  378.         if (is_bool($looselyTyped)) {
  379.             $this->_looselyTyped $looselyTyped;
  380.         }
  381.     }
  382.  
  383.     /**
  384.     * Fails a test with the given message.
  385.     *
  386.     * @param  string 
  387.     * @access protected
  388.     * @abstract
  389.     */
  390.     function fail($message ''/* abstract */ }
  391. }
  392. ?>

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