mock_objects.php
来自「一款可以和GOOGLE媲美的开源统计系统,运用AJAX.功能强大. 无色提示:」· PHP 代码 · 共 1,307 行 · 第 1/4 页
PHP
1,307 行
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); } } /** * @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->_getReturn($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. * @access protected */ function &_getReturn($method, $args, $step) { if (isset($this->_return_sequence[$method][$step])) { if ($this->_return_sequence[$method][$step]->isMatch($args)) { $result = &$this->_return_sequence[$method][$step]->findFirstMatch($args); return $result; } } if (isset($this->_returns[$method])) { $result = &$this->_returns[$method]->findFirstMatch($args); return $result; } $null = null; return $null; } /** * 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; var $_mock_class; var $_mock_base; var $_reflection; /** * Builds initial reflection object. * @param string $class Class to be mocked. * @param string $mock_class New class with identical interface, * but no behaviour. */ function MockGenerator($class, $mock_class) { $this->_class = $class; $this->_mock_class = $mock_class; if (! $this->_mock_class) { $this->_mock_class = 'Mock' . $this->_class; } $this->_mock_base = SimpleTest::getMockBaseClass(); $this->_reflection = new SimpleReflection($this->_class); } /** * Clones a class' interface and creates a mock version * that can have return values and expectations set. * @param array $methods Additional methods to add beyond * those in th cloned class. Use this * to emulate the dynamic addition of * methods in the cloned class or when * the class hasn't been written yet. * @access public */ function generate($methods) { if (! $this->_reflection->classOrInterfaceExists()) { return false; } $mock_reflection = new SimpleReflection($this->_mock_class); if ($mock_reflection->classExistsSansAutoload()) { return false; } $code = $this->_createClassCode($methods ? $methods : array()); return eval("$code return \$code;"); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?