mock_objects.php.svn-base
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· SVN-BASE 代码 · 共 1,582 行 · 第 1/4 页
SVN-BASE
1,582 行
var $_wildcard = MOCK_ANYTHING; var $_always; var $_at; /** * Sets up an empty response schedule. * Creates an empty call map. */ function SimpleCallSchedule() { $this->_always = array(); $this->_at = array(); } /** * Stores an action against a signature that * will always fire unless masked by a time * specific one. * @param string $method Method name. * @param array $args Calling parameters. * @param SimpleAction $action Actually simpleByValue, etc. * @access public */ function register($method, $args, &$action) { $args = $this->_replaceWildcards($args); $method = strtolower($method); if (! isset($this->_always[$method])) { $this->_always[$method] = new SimpleSignatureMap(); } $this->_always[$method]->add($args, $action); } /** * Stores an action against a signature that * will fire at a specific time in the future. * @param integer $step delay of calls to this method, * 0 is next. * @param string $method Method name. * @param array $args Calling parameters. * @param SimpleAction $action Actually SimpleByValue, etc. * @access public */ function registerAt($step, $method, $args, &$action) { $args = $this->_replaceWildcards($args); $method = strtolower($method); if (! isset($this->_at[$method])) { $this->_at[$method] = array(); } if (! isset($this->_at[$method][$step])) { $this->_at[$method][$step] = new SimpleSignatureMap(); } $this->_at[$method][$step]->add($args, $action); } function expectArguments($method, $args, $message) { $args = $this->_replaceWildcards($args); $message .= Mock::getExpectationLine(); $this->_expected_args[strtolower($method)] = new ParametersExpectation($args, $message); } /** * Actually carry out the action stored previously, * if the parameters match. * @param integer $step Time of call. * @param string $method Method name. * @param array $args The parameters making up the * rest of the call. * @return mixed The result of the action. * @access public. */ function &respond($step, $method, $args) { $method = strtolower($method); if (isset($this->_at[$method][$step])) { if ($this->_at[$method][$step]->isMatch($args)) { $action = &$this->_at[$method][$step]->findFirstAction($args); if (isset($action)) { return $action->act(); } } } if (isset($this->_always[$method])) { $action = &$this->_always[$method]->findFirstAction($args); if (isset($action)) { return $action->act(); } } $null = null; return $null; } /** * 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; }}/** * A type of SimpleMethodAction. * Stashes a reference for returning later. * @package SimpleTest * @subpackage MockObjects */class SimpleByReference { var $_reference; /** * Stashes it for later. * @param mixed $reference Actual PHP4 style reference. * @access public */ function SimpleByReference(&$reference) { $this->_reference = &$reference; } /** * Returns the reference stored earlier. * @return mixed Whatever was stashed. * @access public */ function &act() { return $this->_reference; }}/** * A type of SimpleMethodAction. * Stashes a value for returning later. * @package SimpleTest * @subpackage MockObjects */class SimpleByValue { var $_value; /** * Stashes it for later. * @param mixed $value You need to clone objects * if you want copy semantics * for these. * @access public */ function SimpleByValue($value) { $this->_value = $value; } /** * Returns the value stored earlier. * @return mixed Whatever was stashed. * @access public */ function &act() { $dummy = $this->_value; return $dummy; }}/** * A type of SimpleMethodAction. * Stashes an exception for throwing later. * @package SimpleTest * @subpackage MockObjects */class SimpleThrower { var $_exception; /** * Stashes it for later. * @param Exception $exception The exception object to throw. * @access public */ function SimpleThrower($exception) { $this->_exception = $exception; } /** * Throws the exceptins stashed earlier. * @access public */ function act() { eval('throw $this->_exception;'); }}/** * A type of SimpleMethodAction. * Stashes an error for emitting later. * @package SimpleTest * @subpackage MockObjects */class SimpleErrorThrower { var $_error; var $_severity; /** * Stashes an error to throw later. * @param string $error Error message. * @param integer $severity PHP error constant, e.g E_USER_ERROR. * @access public */ function SimpleErrorThrower($error, $severity) { $this->_error = $error; $this->_severity = $severity; } /** * Triggers the stashed error. * @return null The usual PHP4.4 shenanigans are needed here. * @access public */ function &act() { trigger_error($this->_error, $this->_severity); $null = null; return $null; }}/** * A base class or delegate that extends 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 or returning * preprogrammed responses. * @package SimpleTest * @subpackage MockObjects */class SimpleMock { var $_actions; var $_wildcard = MOCK_ANYTHING; var $_is_strict = true; var $_call_counts; var $_expected_counts; var $_max_counts; var $_expected_args; var $_expected_args_at; /** * Creates an empty action list and expectation list. * All call counts are set to zero. * @access public */ function SimpleMock() { $this->_actions = &new SimpleCallSchedule(); $this->_expectations = &new SimpleCallSchedule(); $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 integer 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"); $this->_actions->register($method, $args, new SimpleByValue($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");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?