mock_objects.php.svn-base

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· SVN-BASE 代码 · 共 1,582 行 · 第 1/4 页

SVN-BASE
1,582
字号
<?php/** *  base include file for SimpleTest *  @package    SimpleTest *  @subpackage MockObjects *  @version    $Id: mock_objects.php 1672 2008-03-02 04:47:34Z edwardzyang $ *//**#@+ * 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 method actions by searching the *    parameter lists until an expected match is found. *    @package SimpleTest *    @subpackage MockObjects */class SimpleSignatureMap {    var $_map;    /**     *    Creates an empty call map.     *    @access public     */    function SimpleSignatureMap() {        $this->_map = array();    }    /**     *    Stashes a reference against a method call.     *    @param array $parameters    Array of arguments (including wildcards).     *    @param mixed $action        Reference placed in the map.     *    @access public     */    function add($parameters, &$action) {        $place = count($this->_map);        $this->_map[$place] = array();        $this->_map[$place]['params'] = new ParametersExpectation($parameters);        $this->_map[$place]['content'] = &$action;    }    /**     *    Searches the call list for a matching parameter     *    set. Returned by reference.     *    @param array $parameters    Parameters to search by     *                                without wildcards.     *    @return object              Object held in the first matching     *                                slot, otherwise null.     *    @access public     */    function &findFirstAction($parameters) {        $slot = $this->_findFirstSlot($parameters);        if (isset($slot) && isset($slot['content'])) {            return $slot['content'];        }        $null = null;        return $null;    }    /**     *    Searches the call list for a matching parameter     *    set. True if successful.     *    @param array $parameters    Parameters to search by     *                                without wildcards.     *    @return boolean             True if a match is present.     *    @access public     */    function isMatch($parameters) {        return ($this->_findFirstSlot($parameters) != null);    }        /**     *    Compares the incoming parameters with the     *    internal expectation. Uses the incoming $test     *    to dispatch the test message.     *    @param SimpleTestCase $test   Test to dispatch to.     *    @param array $parameters      The actual calling arguments.     *    @param string $message        The message to overlay.     *    @access public     */    function test(&$test, $parameters, $message) {    }    /**     *    Searches the map for a matching item.     *    @param array $parameters    Parameters to search by     *                                without wildcards.     *    @return array               Reference to slot or null.     *    @access private     */    function &_findFirstSlot($parameters) {        $count = count($this->_map);        for ($i = 0; $i < $count; $i++) {            if ($this->_map[$i]["params"]->test($parameters)) {                return $this->_map[$i];            }        }        $null = null;        return $null;    }}/** *    Allows setting of actions against call signatures either *    at a specific time, or always. Specific time settings *    trump lasting ones, otherwise the most recently added *    will mask an earlier match. *    @package SimpleTest *    @subpackage MockObjects */class SimpleCallSchedule {

⌨️ 快捷键说明

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