mock_objects.php

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

PHP
1,307
字号
     *    @access public     */    function addReference($parameters, &$reference) {        $place = count($this->_map);        $this->_map[$place] = array();        $this->_map[$place]["params"] = new ParametersExpectation($parameters);        $this->_map[$place]["content"] = &$reference;    }    /**     *    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 &findFirstMatch($parameters) {        $slot = $this->_findFirstSlot($parameters);        if (!isset($slot)) {            $null = null;            return $null;        }        return $slot["content"];    }    /**     *    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);    }    /**     *    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;    }}/** *    An empty collection of methods that can have their *    return values set and expectations made of the *    calls upon them. The mock will assert the *    expectations against it's attached test case in *    addition to the server stub behaviour. *    @package SimpleTest *    @subpackage MockObjects */class SimpleMock {    var $_wildcard = MOCK_ANYTHING;    var $_is_strict = true;    var $_returns;    var $_return_sequence;    var $_call_counts;    var $_expected_counts;    var $_max_counts;    var $_expected_args;    var $_expected_args_at;    /**     *    Creates an empty return list and expectation list.     *    All call counts are set to zero.     */    function SimpleMock() {        $this->_returns = array();        $this->_return_sequence = array();        $this->_call_counts = array();        $this->_expected_counts = array();        $this->_max_counts = array();        $this->_expected_args = array();        $this->_expected_args_at = array();        $test = &$this->_getCurrentTestCase();        $test->tell($this);    }        /**     *    Disables a name check when setting expectations.     *    This hack is needed for the partial mocks.     *    @access public     */    function disableExpectationNameChecks() {        $this->_is_strict = false;    }    /**     *    Finds currently running test.     *    @return SimpeTestCase    Current test case.     *    @access protected     */    function &_getCurrentTestCase() {        $context = &SimpleTest::getContext();        return $context->getTest();    }    /**     *    Die if bad arguments array is passed     *    @param mixed $args     The arguments value to be checked.     *    @param string $task    Description of task attempt.     *    @return boolean        Valid arguments     *    @access private     */    function _checkArgumentsIsArray($args, $task) {        if (! is_array($args)) {            trigger_error(                "Cannot $task as \$args parameter is not an array",                E_USER_ERROR);        }    }    /**     *    Triggers a PHP error if the method is not part     *    of this object.     *    @param string $method        Name of method.     *    @param string $task          Description of task attempt.     *    @access protected     */    function _dieOnNoMethod($method, $task) {        if ($this->_is_strict && ! method_exists($this, $method)) {            trigger_error(                    "Cannot $task as no ${method}() in class " . get_class($this),                    E_USER_ERROR);        }    }    /**     *    Replaces wildcard matches with wildcard     *    expectations in the argument list.     *    @param array $args      Raw argument list.     *    @return array           Argument list with     *                            expectations.     *    @access private     */    function _replaceWildcards($args) {        if ($args === false) {            return false;        }        for ($i = 0; $i < count($args); $i++) {            if ($args[$i] === $this->_wildcard) {                $args[$i] = new AnythingExpectation();            }        }        return $args;    }    /**     *    Adds one to the call count of a method.     *    @param string $method        Method called.     *    @param array $args           Arguments as an array.     *    @access protected     */    function _addCall($method, $args) {        if (!isset($this->_call_counts[$method])) {            $this->_call_counts[$method] = 0;        }        $this->_call_counts[$method]++;    }    /**     *    Fetches the call count of a method so far.     *    @param string $method        Method name called.     *    @return                      Number of calls so far.     *    @access public     */    function getCallCount($method) {        $this->_dieOnNoMethod($method, "get call count");        $method = strtolower($method);        if (! isset($this->_call_counts[$method])) {            return 0;        }        return $this->_call_counts[$method];    }    /**     *    Sets a return for a parameter list that will     *    be passed by value for all calls to this method.     *    @param string $method       Method name.     *    @param mixed $value         Result of call passed by value.     *    @param array $args          List of parameters to match     *                                including wildcards.     *    @access public     */    function setReturnValue($method, $value, $args = false) {        $this->_dieOnNoMethod($method, "set return value");        $args = $this->_replaceWildcards($args);        $method = strtolower($method);        if (! isset($this->_returns[$method])) {            $this->_returns[$method] = new CallMap();        }        $this->_returns[$method]->addValue($args, $value);    }    /**     *    Sets a return for a parameter list that will     *    be passed by value only when the required call count     *    is reached.     *    @param integer $timing   Number of calls in the future     *                             to which the result applies. If     *                             not set then all calls will return     *                             the value.     *    @param string $method    Method name.     *    @param mixed $value      Result of call passed by value.     *    @param array $args       List of parameters to match     *                             including wildcards.     *    @access public     */    function setReturnValueAt($timing, $method, $value, $args = false) {        $this->_dieOnNoMethod($method, "set return value sequence");        $args = $this->_replaceWildcards($args);        $method = strtolower($method);        if (! isset($this->_return_sequence[$method])) {            $this->_return_sequence[$method] = array();        }        if (! isset($this->_return_sequence[$method][$timing])) {            $this->_return_sequence[$method][$timing] = new CallMap();        }        $this->_return_sequence[$method][$timing]->addValue($args, $value);    }    /**     *    Sets a return for a parameter list that will     *    be passed by reference for all calls.     *    @param string $method       Method name.     *    @param mixed $reference     Result of the call will be this object.     *    @param array $args          List of parameters to match     *                                including wildcards.     *    @access public     */    function setReturnReference($method, &$reference, $args = false) {        $this->_dieOnNoMethod($method, "set return reference");        $args = $this->_replaceWildcards($args);        $method = strtolower($method);        if (! isset($this->_returns[$method])) {            $this->_returns[$method] = new CallMap();        }        $this->_returns[$method]->addReference($args, $reference);    }    /**     *    Sets a return for a parameter list that will     *    be passed by value only when the required call count     *    is reached.     *    @param integer $timing    Number of calls in the future     *                              to which the result applies. If     *                              not set then all calls will return     *                              the value.     *    @param string $method     Method name.     *    @param mixed $reference   Result of the call will be this object.     *    @param array $args        List of parameters to match     *                              including wildcards.     *    @access public     */    function setReturnReferenceAt($timing, $method, &$reference, $args = false) {        $this->_dieOnNoMethod($method, "set return reference sequence");        $args = $this->_replaceWildcards($args);        $method = strtolower($method);        if (! isset($this->_return_sequence[$method])) {            $this->_return_sequence[$method] = array();        }        if (! isset($this->_return_sequence[$method][$timing])) {            $this->_return_sequence[$method][$timing] = new CallMap();        }        $this->_return_sequence[$method][$timing]->addReference($args, $reference);    }    /**     *    Sets up an expected call with a set of     *    expected parameters in that call. All     *    calls will be compared to these expectations     *    regardless of when the call is made.     *    @param string $method        Method call to test.     *    @param array $args           Expected parameters for the call     *                                 including wildcards.     *    @param string $message       Overridden message.     *    @access public     */    function expect($method, $args, $message = '%s') {        $this->_dieOnNoMethod($method, 'set expected arguments');        $this->_checkArgumentsIsArray($args, 'set expected arguments');        $args = $this->_replaceWildcards($args);        $message .= Mock::getExpectationLine();        $this->_expected_args[strtolower($method)] =                new ParametersExpectation($args, $message);    }    /**     *    @deprecated     */    function expectArguments($method, $args, $message = '%s') {        return $this->expect($method, $args, $message);    }    /**     *    Sets up an expected call with a set of     *    expected parameters in that call. The     *    expected call count will be adjusted if it     *    is set too low to reach this call.     *    @param integer $timing    Number of calls in the future at     *                              which to test. Next call is 0.     *    @param string $method     Method call to test.     *    @param array $args        Expected parameters for the call     *                              including wildcards.     *    @param string $message    Overridden message.     *    @access public     */    function expectAt($timing, $method, $args, $message = '%s') {        $this->_dieOnNoMethod($method, 'set expected arguments at time');        $this->_checkArgumentsIsArray($args, 'set expected arguments at time');        $args = $this->_replaceWildcards($args);

⌨️ 快捷键说明

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