mock_objects.php.svn-base

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

SVN-BASE
1,582
字号
        $this->_actions->registerAt($timing, $method, $args, new SimpleByValue($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");        $this->_actions->register($method, $args, new SimpleByReference($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");        $this->_actions->registerAt($timing, $method, $args, new SimpleByReference($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');        $this->_expectations->expectArguments($method, $args, $message);        $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);        if (! isset($this->_expected_args_at[$timing])) {            $this->_expected_args_at[$timing] = array();        }        $method = strtolower($method);        $message .= Mock::getExpectationLine();        $this->_expected_args_at[$timing][$method] =                new ParametersExpectation($args, $message);    }    /**     *    @deprecated     */    function expectArgumentsAt($timing, $method, $args, $message = '%s') {        return $this->expectAt($timing, $method, $args, $message);    }    /**     *    Sets an expectation for the number of times     *    a method will be called. The tally method     *    is used to check this.     *    @param string $method        Method call to test.     *    @param integer $count        Number of times it should     *                                 have been called at tally.     *    @param string $message       Overridden message.     *    @access public     */    function expectCallCount($method, $count, $message = '%s') {        $this->_dieOnNoMethod($method, 'set expected call count');        $message .= Mock::getExpectationLine();        $this->_expected_counts[strtolower($method)] =                new CallCountExpectation($method, $count, $message);    }    /**     *    Sets the number of times a method may be called     *    before a test failure is triggered.     *    @param string $method        Method call to test.     *    @param integer $count        Most number of times it should     *                                 have been called.     *    @param string $message       Overridden message.     *    @access public     */    function expectMaximumCallCount($method, $count, $message = '%s') {        $this->_dieOnNoMethod($method, 'set maximum call count');        $message .= Mock::getExpectationLine();        $this->_max_counts[strtolower($method)] =                new MaximumCallCountExpectation($method, $count, $message);    }    /**     *    Sets the number of times to call a method to prevent     *    a failure on the tally.     *    @param string $method      Method call to test.     *    @param integer $count      Least number of times it should     *                               have been called.     *    @param string $message     Overridden message.     *    @access public     */    function expectMinimumCallCount($method, $count, $message = '%s') {        $this->_dieOnNoMethod($method, 'set minimum call count');        $message .= Mock::getExpectationLine();        $this->_expected_counts[strtolower($method)] =                new MinimumCallCountExpectation($method, $count, $message);    }    /**     *    Convenience method for barring a method     *    call.     *    @param string $method        Method call to ban.     *    @param string $message       Overridden message.     *    @access public     */    function expectNever($method, $message = '%s') {        $this->expectMaximumCallCount($method, 0, $message);    }    /**     *    Convenience method for a single method     *    call.     *    @param string $method     Method call to track.     *    @param array $args        Expected argument list or     *                              false for any arguments.     *    @param string $message    Overridden message.     *    @access public     */    function expectOnce($method, $args = false, $message = '%s') {        $this->expectCallCount($method, 1, $message);        if ($args !== false) {            $this->expect($method, $args, $message);        }    }    /**     *    Convenience method for requiring a method     *    call.     *    @param string $method       Method call to track.     *    @param array $args          Expected argument list or     *                                false for any arguments.     *    @param string $message      Overridden message.     *    @access public     */    function expectAtLeastOnce($method, $args = false, $message = '%s') {        $this->expectMinimumCallCount($method, 1, $message);        if ($args !== false) {            $this->expect($method, $args, $message);        }    }        /**     *    Sets up a trigger to throw an exception upon the     *    method call.     *    @param string $method     Method name to throw on.     */    function throwOn($method, $exception = false, $args = false) {        $this->_dieOnNoMethod($method, "throw on");        $this->_actions->register($method, $args,                new SimpleThrower($exception ? $exception : new Exception()));    }        /**     *    Sets up a trigger to throw an exception upon the     *    method call.     */    function throwAt($timing, $method, $exception = false, $args = false) {        $this->_dieOnNoMethod($method, "throw at");        $this->_actions->registerAt($timing, $method, $args,                new SimpleThrower($exception ? $exception : new Exception()));    }        /**     *    Sets up a trigger to throw an error upon the     *    method call.     */    function errorOn($method, $error = 'A mock error', $args = false, $severity = E_USER_ERROR) {        $this->_dieOnNoMethod($method, "error on");        $this->_actions->register($method, $args, new SimpleErrorThrower($error, $severity));    }        /**     *    Sets up a trigger to throw an error upon the     *    method call.     */    function errorAt($timing, $method, $error = 'A mock error', $args = false, $severity = E_USER_ERROR) {        $this->_dieOnNoMethod($method, "error at");        $this->_actions->registerAt($timing, $method, $args, new SimpleErrorThrower($error, $severity));    }    /**     *    @deprecated     */    function tally() {    }    /**     *    Receives event from unit test that the current     *    test method has finished. Totals up the call     *    counts and triggers a test assertion if a test     *    is present for expected call counts.     *    @param string $test_method      Current method name.     *    @param SimpleTestCase $test     Test to send message to.     *    @access public     */    function atTestEnd($test_method, &$test) {        foreach ($this->_expected_counts as $method => $expectation) {            $test->assert($expectation, $this->getCallCount($method));        }        foreach ($this->_max_counts as $method => $expectation) {            if ($expectation->test($this->getCallCount($method))) {                $test->assert($expectation, $this->getCallCount($method));            }        }    }    /**     *    Returns the expected value for the method name     *    and checks expectations. Will generate any     *    test assertions as a result of expectations     *    if there is a test present.     *    @param string $method       Name of method to simulate.     *    @param array $args          Arguments as an array.     *    @return mixed               Stored return.     *    @access private     */    function &_invoke($method, $args) {        $method = strtolower($method);        $step = $this->getCallCount($method);        $this->_addCall($method, $args);        $this->_checkExpectations($method, $args, $step);        $result = &$this->_emulateCall($method, $args, $step);        return $result;    }        /**     *    Finds the return value matching the incoming     *    arguments. If there is no matching value found     *    then an error is triggered.     *    @param string $method      Method name.     *    @param array $args         Calling arguments.     *    @param integer $step       Current position in the     *                               call history.     *    @return mixed              Stored return or other action.     *    @access protected     */    function &_emulateCall($method, $args, $step) {        return $this->_actions->respond($step, $method, $args);    }    /**     *    Tests the arguments against expectations.     *    @param string $method        Method to check.     *    @param array $args           Argument list to match.     *    @param integer $timing       The position of this call     *                                 in the call history.     *    @access private     */    function _checkExpectations($method, $args, $timing) {        $test = &$this->_getCurrentTestCase();        if (isset($this->_max_counts[$method])) {            if (! $this->_max_counts[$method]->test($timing + 1)) {                $test->assert($this->_max_counts[$method], $timing + 1);            }        }        if (isset($this->_expected_args_at[$timing][$method])) {            $test->assert(                    $this->_expected_args_at[$timing][$method],                    $args,                    "Mock method [$method] at [$timing] -> %s");        } elseif (isset($this->_expected_args[$method])) {            $test->assert(                    $this->_expected_args[$method],                    $args,                    "Mock method [$method] -> %s");        }    }}/** *    Static methods only service class for code generation of *    mock objects. *    @package SimpleTest *    @subpackage MockObjects */class Mock {    /**     *    Factory for mock object classes.     *    @access public     */    function Mock() {        trigger_error('Mock factory methods are static.');    }    /**     *    Clones a class' interface and creates a mock version     *    that can have return values and expectations set.     *    @param string $class         Class to clone.     *    @param string $mock_class    New class name. Default is     *                                 the old name with "Mock"     *                                 prepended.     *    @param array $methods        Additional methods to add beyond     *                                 those in the cloned class. Use this     *                                 to emulate the dynamic addition of     *                                 methods in the cloned class or when     *                                 the class hasn't been written yet.     *    @static     *    @access public     */    function generate($class, $mock_class = false, $methods = false) {        $generator = new MockGenerator($class, $mock_class);        return $generator->generateSubclass($methods);    }    /**     *    Generates a version of a class with selected     *    methods mocked only. Inherits the old class     *    and chains the mock methods of an aggregated     *    mock object.     *    @param string $class            Class to clone.     *    @param string $mock_class       New class name.     *    @param array $methods           Methods to be overridden     *                                    with mock versions.     *    @static     *    @access public     */    function generatePartial($class, $mock_class, $methods) {        $generator = new MockGenerator($class, $mock_class);        return $generator->generatePartial($methods);    }    /**     *    Uses a stack trace to find the line of an assertion.     *    @access public     *    @static     */    function getExpectationLine() {        $trace = new SimpleStackTrace(array('expect'));        return $trace->traceMethod();    }}/** *    @package  SimpleTest *    @subpackage   MockObjects *    @deprecated */class Stub extends Mock {}/** *    Service class for code generation of mock objects. *    @package SimpleTest *    @subpackage MockObjects */class MockGenerator {    var $_class;

⌨️ 快捷键说明

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