mock_objects.php

来自「一款可以和GOOGLE媲美的开源统计系统,运用AJAX.功能强大. 无色提示:」· PHP 代码 · 共 1,307 行 · 第 1/4 页

PHP
1,307
字号
<?php/** *	base include file for SimpleTest *	@package	SimpleTest *	@subpackage	MockObjects *	@version	$Id: mock_objects.php 163 2008-01-14 04:40:16Z matt $ *//**#@+ * include SimpleTest files */require_once(dirname(__FILE__) . '/expectation.php');require_once(dirname(__FILE__) . '/simpletest.php');require_once(dirname(__FILE__) . '/dumper.php');if (version_compare(phpversion(), '5') >= 0) {    require_once(dirname(__FILE__) . '/reflection_php5.php');} else {    require_once(dirname(__FILE__) . '/reflection_php4.php');}/**#@-*//** * Default character simpletest will substitute for any value */if (! defined('MOCK_ANYTHING')) {    define('MOCK_ANYTHING', '*');}/** *    Parameter comparison assertion. *    @package SimpleTest *    @subpackage MockObjects */class ParametersExpectation extends SimpleExpectation {    var $_expected;    /**     *    Sets the expected parameter list.     *    @param array $parameters  Array of parameters including     *                              those that are wildcarded.     *                              If the value is not an array     *                              then it is considered to match any.     *    @param string $message    Customised message on failure.     *    @access public     */    function ParametersExpectation($expected = false, $message = '%s') {        $this->SimpleExpectation($message);        $this->_expected = $expected;    }    /**     *    Tests the assertion. True if correct.     *    @param array $parameters     Comparison values.     *    @return boolean              True if correct.     *    @access public     */    function test($parameters) {        if (! is_array($this->_expected)) {            return true;        }        if (count($this->_expected) != count($parameters)) {            return false;        }        for ($i = 0; $i < count($this->_expected); $i++) {            if (! $this->_testParameter($parameters[$i], $this->_expected[$i])) {                return false;            }        }        return true;    }    /**     *    Tests an individual parameter.     *    @param mixed $parameter    Value to test.     *    @param mixed $expected     Comparison value.     *    @return boolean            True if expectation     *                               fulfilled.     *    @access private     */    function _testParameter($parameter, $expected) {        $comparison = $this->_coerceToExpectation($expected);        return $comparison->test($parameter);    }    /**     *    Returns a human readable test message.     *    @param array $comparison   Incoming parameter list.     *    @return string             Description of success     *                               or failure.     *    @access public     */    function testMessage($parameters) {        if ($this->test($parameters)) {            return "Expectation of " . count($this->_expected) .                    " arguments of [" . $this->_renderArguments($this->_expected) .                    "] is correct";        } else {            return $this->_describeDifference($this->_expected, $parameters);        }    }    /**     *    Message to display if expectation differs from     *    the parameters actually received.     *    @param array $expected      Expected parameters as list.     *    @param array $parameters    Actual parameters received.     *    @return string              Description of difference.     *    @access private     */    function _describeDifference($expected, $parameters) {        if (count($expected) != count($parameters)) {            return "Expected " . count($expected) .                    " arguments of [" . $this->_renderArguments($expected) .                    "] but got " . count($parameters) .                    " arguments of [" . $this->_renderArguments($parameters) . "]";        }        $messages = array();        for ($i = 0; $i < count($expected); $i++) {            $comparison = $this->_coerceToExpectation($expected[$i]);            if (! $comparison->test($parameters[$i])) {                $messages[] = "parameter " . ($i + 1) . " with [" .                        $comparison->overlayMessage($parameters[$i], $this->_getDumper()) . "]";            }        }        return "Parameter expectation differs at " . implode(" and ", $messages);    }    /**     *    Creates an identical expectation if the     *    object/value is not already some type     *    of expectation.     *    @param mixed $expected      Expected value.     *    @return SimpleExpectation   Expectation object.     *    @access private     */    function _coerceToExpectation($expected) {        if (SimpleExpectation::isExpectation($expected)) {            return $expected;        }        return new IdenticalExpectation($expected);    }    /**     *    Renders the argument list as a string for     *    messages.     *    @param array $args    Incoming arguments.     *    @return string        Simple description of type and value.     *    @access private     */    function _renderArguments($args) {        $descriptions = array();        if (is_array($args)) {            foreach ($args as $arg) {                $dumper = &new SimpleDumper();                $descriptions[] = $dumper->describeValue($arg);            }        }        return implode(', ', $descriptions);    }}/** *    Confirms that the number of calls on a method is as expected. *	@package	SimpleTest *	@subpackage	MockObjects */class CallCountExpectation extends SimpleExpectation {    var $_method;    var $_count;    /**     *    Stashes the method and expected count for later     *    reporting.     *    @param string $method    Name of method to confirm against.     *    @param integer $count    Expected number of calls.     *    @param string $message   Custom error message.     */    function CallCountExpectation($method, $count, $message = '%s') {        $this->_method = $method;        $this->_count = $count;        $this->SimpleExpectation($message);    }    /**     *    Tests the assertion. True if correct.     *    @param integer $compare     Measured call count.     *    @return boolean             True if expected.     *    @access public     */    function test($compare) {        return ($this->_count == $compare);    }    /**     *    Reports the comparison.     *    @param integer $compare     Measured call count.     *    @return string              Message to show.     *    @access public     */    function testMessage($compare) {        return 'Expected call count for [' . $this->_method .                '] was [' . $this->_count .                '] got [' . $compare . ']';    }}/** *    Confirms that the number of calls on a method is as expected. *	@package	SimpleTest *	@subpackage	MockObjects */class MinimumCallCountExpectation extends SimpleExpectation {    var $_method;    var $_count;    /**     *    Stashes the method and expected count for later     *    reporting.     *    @param string $method    Name of method to confirm against.     *    @param integer $count    Minimum number of calls.     *    @param string $message   Custom error message.     */    function MinimumCallCountExpectation($method, $count, $message = '%s') {        $this->_method = $method;        $this->_count = $count;        $this->SimpleExpectation($message);    }    /**     *    Tests the assertion. True if correct.     *    @param integer $compare     Measured call count.     *    @return boolean             True if enough.     *    @access public     */    function test($compare) {        return ($this->_count <= $compare);    }    /**     *    Reports the comparison.     *    @param integer $compare     Measured call count.     *    @return string              Message to show.     *    @access public     */    function testMessage($compare) {        return 'Minimum call count for [' . $this->_method .                '] was [' . $this->_count .                '] got [' . $compare . ']';    }}/** *    Confirms that the number of calls on a method is as expected. *	@package	SimpleTest *	@subpackage	MockObjects */class MaximumCallCountExpectation extends SimpleExpectation {    var $_method;    var $_count;    /**     *    Stashes the method and expected count for later     *    reporting.     *    @param string $method    Name of method to confirm against.     *    @param integer $count    Minimum number of calls.     *    @param string $message   Custom error message.     */    function MaximumCallCountExpectation($method, $count, $message = '%s') {        $this->_method = $method;        $this->_count = $count;        $this->SimpleExpectation($message);    }    /**     *    Tests the assertion. True if correct.     *    @param integer $compare     Measured call count.     *    @return boolean             True if not over.     *    @access public     */    function test($compare) {        return ($this->_count >= $compare);    }    /**     *    Reports the comparison.     *    @param integer $compare     Measured call count.     *    @return string              Message to show.     *    @access public     */    function testMessage($compare) {        return 'Maximum call count for [' . $this->_method .                '] was [' . $this->_count .                '] got [' . $compare . ']';    }}/** *    Retrieves values and references by searching the *    parameter lists until a match is found. *    @package SimpleTest *    @subpackage MockObjects */class CallMap {    var $_map;    /**     *    Creates an empty call map.     *    @access public     */    function CallMap() {        $this->_map = array();    }    /**     *    Stashes a value against a method call.     *    @param array $parameters    Arguments including wildcards.     *    @param mixed $value         Value copied into the map.     *    @access public     */    function addValue($parameters, $value) {        $this->addReference($parameters, $value);    }    /**     *    Stashes a reference against a method call.     *    @param array $parameters    Array of arguments (including wildcards).     *    @param mixed $reference     Array reference placed in the map.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?