cvs: pear /PHPUnit2 package.xml /PHPUnit2/Extensions/MockObjects CodeGenerator.php Expectation.php Registry.php
/PHPUnit2/Extensions/MockObjects/Expectation CallCount.php MaximumCallCount.php MinimumCallCount.php /PHPUnit2/Framework Assert.php /PHPUnit2/Util
Filter.php

From: Date: Mon, 25 Jul 2005 07:19:26 +0000
Subject: cvs: pear /PHPUnit2 package.xml /PHPUnit2/Extensions/MockObjects CodeGenerator.php Expectation.php Registry.php
/PHPUnit2/Extensions/MockObjects/Expectation CallCount.php MaximumCallCount.php MinimumCallCount.php /PHPUnit2/Framework Assert.php /PHPUnit2/Util
Filter.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-33285@lists.php.net to get a copy of this message
sebastian Mon Jul 25 03:19:26 2005 EDT Added files: /pear/PHPUnit2/Extensions/MockObjects CodeGenerator.php Expectation.php Registry.php /pear/PHPUnit2/Extensions/MockObjects/Expectation CallCount.php MaximumCallCount.php MinimumCallCount.php Modified files: /pear/PHPUnit2 package.xml /pear/PHPUnit2/Framework Assert.php /pear/PHPUnit2/Util Filter.php Log: Initial checkin of Mock Objects support.

http://cvs.php.net/diff.php/pear/PHPUnit2/package.xml?r1=1.115&r2=1.116&ty=u Index: pear/PHPUnit2/package.xml diff -u pear/PHPUnit2/package.xml:1.115 pear/PHPUnit2/package.xml:1.116 --- pear/PHPUnit2/package.xml:1.115 Tue Jun 21 05:01:47 2005 +++ pear/PHPUnit2/package.xml Mon Jul 25 03:19:25 2005 @@ -18,11 +18,36 @@ <date>2005-MM-DD</date> <notes> <![CDATA[ ++ Added support for Mock Objects. ]]> </notes> <filelist> <dir name="/" baseinstalldir="PHPUnit2"> <dir name="Extensions"> + <dir name="MockObjects"> + <dir name="Expectation"> + <file name="CallCount.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + <file name="MaximumCallCount.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + <file name="MinimumCallCount.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + </dir> + + <file name="CodeGenerator.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + <file name="Expectation.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + <file name="Registry.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + </dir> + <file role="php" name="ExceptionTestCase.php"> <replace type="package-info" from="@package_version@" to="version"/> </file> http://cvs.php.net/diff.php/pear/PHPUnit2/Framework/Assert.php?r1=1.45&r2=1.46&ty=u Index: pear/PHPUnit2/Framework/Assert.php diff -u pear/PHPUnit2/Framework/Assert.php:1.45 pear/PHPUnit2/Framework/Assert.php:1.46 --- pear/PHPUnit2/Framework/Assert.php:1.45 Thu Jun 9 13:51:39 2005 +++ pear/PHPUnit2/Framework/Assert.php Mon Jul 25 03:19:26 2005 @@ -15,7 +15,7 @@ * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> * @license http://www.php.net/license/3_0.txt PHP License 3.0 - * @version CVS: $Id: Assert.php,v 1.45 2005/06/09 17:51:39 sebastian Exp $ + * @version CVS: $Id: Assert.php,v 1.46 2005/07/25 07:19:26 sebastian Exp $ * @link http://pear.php.net/package/PHPUnit2 * @since File available since Release 2.0.0 */ @@ -536,6 +536,23 @@ } /** + * Returns a Mock Object for a given class. + * + * If the $methods array is not empty a method will only + * be mocked if its name is in the $methods array. + * + * @param mixed $class + * @param Array $methods + * @return object + * @access public + * @static + * @since Method available since Release 2.4.0 + */ + public static function getMockObject($class, $methods = array()) { + return PHPUnit2_Extensions_MockObjects_CodeGenerator::getMockObject($class, $methods); + } + + /** * Converts a value to a string. * * @param mixed $value http://cvs.php.net/diff.php/pear/PHPUnit2/Util/Filter.php?r1=1.34&r2=1.35&ty=u Index: pear/PHPUnit2/Util/Filter.php diff -u pear/PHPUnit2/Util/Filter.php:1.34 pear/PHPUnit2/Util/Filter.php:1.35 --- pear/PHPUnit2/Util/Filter.php:1.34 Mon Jul 11 04:02:44 2005 +++ pear/PHPUnit2/Util/Filter.php Mon Jul 25 03:19:26 2005 @@ -15,7 +15,7 @@ * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> * @license http://www.php.net/license/3_0.txt PHP License 3.0 - * @version CVS: $Id: Filter.php,v 1.34 2005/07/11 08:02:44 sebastian Exp $ + * @version CVS: $Id: Filter.php,v 1.35 2005/07/25 07:19:26 sebastian Exp $ * @link http://pear.php.net/package/PHPUnit2 * @since File available since Release 2.0.0 */ @@ -41,6 +41,12 @@ * @static */ protected static $filteredFiles = array( + 'PHPUnit2/Extensions/MockObjects/Expectation/CallCount.php', + 'PHPUnit2/Extensions/MockObjects/Expectation/MaximumCallCount.php', + 'PHPUnit2/Extensions/MockObjects/Expectation/MinimumCallCount.php', + 'PHPUnit2/Extensions/MockObjects/CodeGenerator.php', + 'PHPUnit2/Extensions/MockObjects/Expectation.php', + 'PHPUnit2/Extensions/MockObjects/Registry.php', 'PHPUnit2/Extensions/ExceptionTestCase.php', 'PHPUnit2/Extensions/PerformanceTestCase.php', 'PHPUnit2/Extensions/RepeatedTest.php', @@ -220,6 +226,7 @@ */ protected static function isFiltered($filename) { if (substr($filename, -7) == 'phpunit' || + strpos($filename, 'eval()\'d code') || in_array(self::getCanonicalFilename($filename), self::$filteredFiles)) { return TRUE; } http://cvs.php.net/co.php/pear/PHPUnit2/Extensions/MockObjects/CodeGenerator.php?r=1.1&p=1 Index: pear/PHPUnit2/Extensions/MockObjects/CodeGenerator.php +++ pear/PHPUnit2/Extensions/MockObjects/CodeGenerator.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * PHP Version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Testing * @package PHPUnit2 * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: CodeGenerator.php,v 1.1 2005/07/25 07:19:25 sebastian Exp $ * @link http://pear.php.net/package/PHPUnit2 * @since File available since Release 2.4.0 */ require_once 'PHPUnit2/Util/Classloader.php'; /** * Code Generator for Mock Object classes. * * @category Testing * @package PHPUnit2 * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version Release: @package_version@ * @link http://pear.php.net/package/PHPUnit2 * @since Class available since Release 2.4.0 * @static */ class PHPUnit2_Extensions_MockObjects_CodeGenerator { const MOCKOBJECT_API = ' public function __expectArguments($method, $arguments, $message = "") { $this->__mockObjectRegistry->expectArguments($this->__mockObjectID, $method, $arguments, $message); } public function __expectArgumentsAt($method, $arguments, $timing, $message = "") { $this->__mockObjectRegistry->expectArgumentsAt($this->__mockObjectID, $method, $arguments, $timing, $message); } public function __expectCallCount($method, $count, $message = "") { $this->__mockObjectRegistry->expectCallCount($this->__mockObjectID, $method, $count, $message); } public function __expectMaximumCallCount($method, $count, $message = "") { $this->__mockObjectRegistry->expectMaximumCallCount($this->__mockObjectID, $method, $count, $message); } public function __expectMinimumCallCount($method, $count, $message = "") { $this->__mockObjectRegistry->expectMinimumCallCount($this->__mockObjectID, $method, $count, $message); } public function __expectNever($method, $message = "") { $this->__mockObjectRegistry->expectNever($this->__mockObjectID, $method, $message); } public function __expectOnce($method, $arguments = FALSE, $message = "") { $this->__mockObjectRegistry->expectOnce($this->__mockObjectID, $method, $arguments, $message); } public function __expectAtLeastOnce($method, $arguments = FALSE, $message = "") { $this->__mockObjectRegistry->expectAtLeastOnce($this->__mockObjectID, $method, $arguments, $message); } public function __getCallCount($method) { return $this->__mockObjectRegistry->getCallCount($this->__mockObjectID, $method); } public function __setReturn($method, $value, $arguments = FALSE) { $this->__mockObjectRegistry->setReturn($this->__mockObjectID, $method, $value, $arguments); } public function __setReturnAt($method, $value, $timing, $arguments = FALSE) { $this->__mockObjectRegistry->setReturnAt($this->__mockObjectID, $method, $value, $timing, $arguments); } public function __destruct() { $this->__mockObjectRegistry->evaluate($this->__mockObjectID); }'; /** * @var Array * @access protected * @static */ protected static $mockClasses = array(); /** * * * @access private * @final */ private final function __construct() { } /** * Returns a Mock Object for a given class. * * If the $methods array is not empty a method will only * be mocked if its name is in the $methods array. * * @param mixed $class * @param Array $methods * @return object * @access public * @static */ public static function getMockObject($class, $methods = array()) { $class = self::getClass($class); $mockClassName = 'Mock' . $class->getName(); if (!isset(self::$mockClasses[$mockClassName])) { if (class_exists($mockClassName)) { throw new RuntimeException( sprintf( 'Cannot mock "%s", "%s" already exists.', $class->getName(), $mockClassName ) ); } eval(self::getMockObjectCode($class, $methods)); self::$mockClasses[$mockClassName] = TRUE; } return new $mockClassName; } /** * Generates code for a Mock Object class. * * If the $methods array is not empty a method will only * be mocked if its name is in the $methods array. * * @param mixed $class * @param Array $methods * @return string * @access public * @static */ public static function getMockObjectCode($class, $methods = array()) { $class = self::getClass($class); if (!$class->isInstantiable()) { throw new RuntimeException( sprintf( 'Cannot mock "%s", class is not instantiable.', $class->getName(), $class->getName() ) ); } return self::generateClassCode($class, $methods); } /** * Returns the code for a Mock Object class. * * @param ReflectionClass $class * @param Array $methods * @return string * @access protected * @static */ protected static function generateClassCode(ReflectionClass $class, Array $methods) { return sprintf( "class Mock%s extends %s {\n" . " private \$__mockObjectID;\n" . " private \$__mockObjectRegistry;\n" . "%s\n\n%s\n%s" . "}\n", $class->getName(), $class->getName(), self::MOCKOBJECT_API, self::generateConstructorCode($class, $methods), self::generateMethodsCode($class, $methods) ); } /** * Returns the code for the constructor a Mock Object class. * * @param ReflectionClass $class * @param Array $methods * @return string * @access protected * @static */ protected static function generateConstructorCode(ReflectionClass $class, Array $methods) { $constructor = $class->getConstructor(); if ($constructor === NULL || ($constructor !== NULL && self::isToBeMocked($constructor, $methods))) { return " public function __construct() {\n" . " \$this->__mockObjectRegistry = PHPUnit2_Extensions_MockObjects_Registry::getInstance();\n" . " \$this->__mockObjectID = \$this->__mockObjectRegistry->registerMockObject(\$this);\n" . " }\n"; } else if ($constructor !== NULL && !self::isToBeMocked($constructor, $methods)) { return sprintf( " public function %s(%s) {\n" . " parent::%s(%s);\n" . " \$this->__mockObjectRegistry = PHPUnit2_Extensions_MockObjects_Registry::getInstance();\n" . " \$this->__mockObjectID = \$this->__mockObjectRegistry->registerMockObject(\$this);\n" . " }\n", $constructor->getName(), self::getMethodParametersCode($constructor), $constructor->getName(), self::getMethodParametersCode($constructor) ); } } /** * Returns the code for the methods of a Mock Object class. * * @param ReflectionClass $class * @return string * @access protected * @static */ protected static function generateMethodsCode(ReflectionClass $class, Array $methods) { $code = ''; foreach ($class->getMethods() as $method) { if (self::isMockableMethod($method) && self::isToBeMocked($method, $methods)) { $code .= sprintf( " public function %s(%s) {\n" . " \$arguments = func_get_args();\n" . " return \$this->__mockObjectRegistry->addCall(\$this->__mockObjectID, '%s', \$arguments);\n" . " }\n", $method->getName(), self::getMethodParametersCode($method), $method->getName() ); } } return $code; } /** * Returns the code for the parameters of a method. * * @param ReflectionMethod $method * @return string * @access protected * @static */ protected static function getMethodParametersCode(ReflectionMethod $method) { $parameters = ''; foreach ($method->getParameters() as $parameter) { if (!empty($parameters)) { $parameters .= ', '; } $typehint = ''; if ($parameter->isArray()) { $typehint = 'Array '; } else if (($classHint = $parameter->getClass()) !== NULL) { $typehint = $classHint->getName() . ' '; } $default = ''; if ($parameter->isDefaultValueAvailable()) { $value = $parameter->getDefaultValue(); $quote = is_string($value) ? "'" : ''; $default = ' = ' . $quote . $parameter->getDefaultValue() . $quote; } $parameters .= sprintf( '%s$%s%s', $typehint, $parameter->getName(), $default ); } return $parameters; } /** * Checks whether a class is mockable and returns an ReflectionClass * object for it if that is the case. * * @param mixed $class * @return ReflectionClass * @throws InvalidArgumentException * @throws RuntimeException * @throws ReflectionException * @access protected * @static */ protected static function getClass($class) { $argumentsValid = FALSE; if (is_object($class) && $class instanceof ReflectionClass) { $argumentsValid = TRUE; } else if (is_string($class) && class_exists($class)) { $class = new ReflectionClass($class); $argumentsValid = TRUE; } if (!$argumentsValid) { throw new InvalidArgumentException; } return $class; } /** * Returns TRUE when the passed method can / should * be replaced with a mock. * * @param ReflectionMethod $method * @return boolean * @access protected * @static */ protected static function isMockableMethod(ReflectionMethod $method) { if (!$method->isPublic()) { return FALSE; } if ($method->isStatic()) { return FALSE; } if (substr($method->getName(), 0, 2) == '__') { return FALSE; } return TRUE; } /** * Returns TRUE when the passed method is to be mocked. * * @param ReflectionMethod $method * @param Array $methods * @return boolean * @access protected * @static */ protected static function isToBeMocked(ReflectionMethod $method, Array $methods) { return empty($methods) || in_array($method->getName(), $methods); } } /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?> http://cvs.php.net/co.php/pear/PHPUnit2/Extensions/MockObjects/Expectation.php?r=1.1&p=1 Index: pear/PHPUnit2/Extensions/MockObjects/Expectation.php +++ pear/PHPUnit2/Extensions/MockObjects/Expectation.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * PHP Version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Testing * @package PHPUnit2 * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: Expectation.php,v 1.1 2005/07/25 07:19:25 sebastian Exp $ * @link http://pear.php.net/package/PHPUnit2 * @since File available since Release 2.4.0 */ /** * Abstract Base Class for Expectations. * * @category Testing * @package PHPUnit2 * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version Release: @package_version@ * @link http://pear.php.net/package/PHPUnit2 * @since Class available since Release 2.4.0 * @static */ abstract class PHPUnit2_Extensions_MockObjects_Expectation { /** * @var integer * @access protected */ protected $mockObjectID; /** * @var string * @access protected */ protected $class; /** * @var string * @access protected */ protected $method; /** * @var string * @access protected */ protected $message; /** * @var PHPUnit2_Extensions_MockObjects_Registry * @access protected */ protected $registry; /** * Constructor. * * @param integer $mockObjectID * @param string $class * @param string $method * @param string $message * @access public */ public function __construct($mockObjectID, $class, $method, $message) { $this->mockObjectID = $mockObjectID; $this->class = $class; $this->method = $method; $this->message = $message; $this->registry = PHPUnit2_Extensions_MockObjects_Registry::getInstance(); } /** * Evaluates the expectation. * * @throws PHPUnit2_Framework_AssertionFailedError * @access public * @abstract */ public abstract function evaluate(); } /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?> http://cvs.php.net/co.php/pear/PHPUnit2/Extensions/MockObjects/Registry.php?r=1.1&p=1 Index: pear/PHPUnit2/Extensions/MockObjects/Registry.php +++ pear/PHPUnit2/Extensions/MockObjects/Registry.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * PHP Version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Testing * @package PHPUnit2 * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: Registry.php,v 1.1 2005/07/25 07:19:25 sebastian Exp $ * @link http://pear.php.net/package/PHPUnit2 * @since File available since Release 2.4.0 */ /** * Mock Object Registry. * * @category Testing * @package PHPUnit2 * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version Release: @package_version@ * @link http://pear.php.net/package/PHPUnit2 * @since Class available since Release 2.4.0 */ class PHPUnit2_Extensions_MockObjects_Registry { /** * @var Array * @access private */ private $mockObjects = array(); /** * @var integer * @access private */ private $nextMockObjectID = 1; /** * @var PHPUnit2_Extensions_MockObjects_Registry * @access private * @static */ private static $uniqueInstance = NULL; /** * * * @access private * @final */ private final function __construct() { } /** * * * @access private * @final */ private final function __clone() { } /** * Returns the unique instance of the registry. * * @return PHPUnit2_Extensions_MockObjects_Registry * @access public * @static */ public static function getInstance() { if (self::$uniqueInstance === NULL) { self::$uniqueInstance = new PHPUnit2_Extensions_MockObjects_Registry; } return self::$uniqueInstance; } /** * Registers a Mock Object. * * @param object $object * @return integer * @access public */ public function registerMockObject($object) { $mockObjectID = $this->nextMockObjectID++; $this->mockObjects[$mockObjectID]['class'] = get_parent_class($object); $this->mockObjects[$mockObjectID]['expectations'] = array(); $this->mockObjects[$mockObjectID]['methodCalls'] = array(); $this->mockObjects[$mockObjectID]['returns'] = array(); return $mockObjectID; } /** * * * @throws InvalidArgumentException * @access public */ public function addCall($mockObjectID, $method, Array $arguments) { if (isset($this->mockObjects[$mockObjectID])) { $this->mockObjects[$mockObjectID]['methodCalls'][$method][] = $arguments; if (isset($this->mockObjects[$mockObjectID]['returns'][$method])) { $arguments = serialize($arguments); foreach ($this->mockObjects[$mockObjectID]['returns'][$method] as $call) { if ($arguments = serialize($call['arguments'])) { return $call['value']; } } } } else { $this->throwInvalidArgumentException($mockObjectID); } } /** * Sets 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. * * @throws InvalidArgumentException * @access public */ public function expectArguments($mockObjectID, $method, $arguments, $message) { if (isset($this->mockObjects[$mockObjectID])) { } else { $this->throwInvalidArgumentException($mockObjectID); } } /** * Sets 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. * * @throws InvalidArgumentException * @access public */ public function expectArgumentsAt($mockObjectID, $method, $arguments, $timing, $message) { if (isset($this->mockObjects[$mockObjectID])) { } else { $this->throwInvalidArgumentException($mockObjectID); } } /** * Sets an expectation for the number of times * a method will be called. * * @throws InvalidArgumentException * @access public */ public function expectCallCount($mockObjectID, $method, $count, $message) { if (isset($this->mockObjects[$mockObjectID])) { $this->mockObjects[$mockObjectID]['expectations'][] = new PHPUnit2_Extensions_MockObjects_Expectation_CallCount( $mockObjectID, $this->mockObjects[$mockObjectID]['class'], $method, $message, $count ); } else { $this->throwInvalidArgumentException($mockObjectID); } } /** * Sets an expectation for the maximum number of times * a method will be called. * * @throws InvalidArgumentException * @access public */ public function expectMaximumCallCount($mockObjectID, $method, $count, $message) { if (isset($this->mockObjects[$mockObjectID])) { $this->mockObjects[$mockObjectID]['expectations'][] = new PHPUnit2_Extensions_MockObjects_Expectation_MaximumCallCount( $mockObjectID, $this->mockObjects[$mockObjectID]['class'], $method, $message, $count ); } else { $this->throwInvalidArgumentException($mockObjectID); } } /** * Sets an expectation for the minimum number of times * a method will be called. * * @throws InvalidArgumentException * @access public */ public function expectMinimumCallCount($mockObjectID, $method, $count, $message) { if (isset($this->mockObjects[$mockObjectID])) { $this->mockObjects[$mockObjectID]['expectations'][] = new PHPUnit2_Extensions_MockObjects_Expectation_MinimumCallCount( $mockObjectID, $this->mockObjects[$mockObjectID]['class'], $method, $message, $count ); } else { $this->throwInvalidArgumentException($mockObjectID); } } /** * Convenience method for barring a method call. * * @throws InvalidArgumentException * @access public */ public function expectNever($mockObjectID, $method, $message) { $this->expectMaximumCallCount($mockObjectID, $method, 0, $message); } /** * Convenience method for a single method call. * * @throws InvalidArgumentException * @access public */ public function expectOnce($mockObjectID, $method, $arguments, $message) { $this->expectCallCount($mockObjectID, $method, 1, $message); if ($arguments !== FALSE) { $this->expectArguments($mockObjectID, $method, $arguments, $message); } } /** * Convenience method for requiring a method call. * * @throws InvalidArgumentException * @access public */ public function expectAtLeastOnce($mockObjectID, $method, $arguments, $message) { $this->expectMinimumCallCount($mockObjectID, $method, 1, $message); if ($arguments !== FALSE) { $this->expectArguments($mockObjectID, $method, $arguments, $message); } } /** * Returns the number of times a method was called. * * @throws InvalidArgumentException * @access public */ public function getCallCount($mockObjectID, $method) { if (isset($this->mockObjects[$mockObjectID])) { return sizeof($this->mockObjects[$mockObjectID]['methodCalls'][$method]); } else { $this->throwInvalidArgumentException($mockObjectID); } } /** * Evaluates the Mock Object's expectations. * * @throws InvalidArgumentException * @access public */ public function evaluate($mockObjectID) { if (isset($this->mockObjects[$mockObjectID])) { foreach ($this->mockObjects[$mockObjectID]['expectations'] as $expectation) { $expectation->evaluate(); } unset($this->mockObjects[$mockObjectID]); } else { $this->throwInvalidArgumentException($mockObjectID); } } /** * Sets a return value for an argument list * for all calls to this method. * * @throws InvalidArgumentException * @access public */ public function setReturn($mockObjectID, $method, $value, $arguments) { if (isset($this->mockObjects[$mockObjectID])) { $this->mockObjects[$mockObjectID]['returns'][$method][] = array( 'arguments' => $arguments, 'value' => $value ); } else { $this->throwInvalidArgumentException($mockObjectID); } } /** * Sets a return value for an argument list * only when the required call count is reached. * * @throws InvalidArgumentException * @access public */ public function setReturnAt($mockObjectID, $method, $value, $timing, $arguments) { if (isset($this->mockObjects[$mockObjectID])) { } else { $this->throwInvalidArgumentException($mockObjectID); } } /** * Utility method to throw an InvalidArgumentException. * * @throws InvalidArgumentException * @access private */ private function throwInvalidArgumentException($mockObjectID) { throw new InvalidArgumentException( sprintf( 'No Mock Object with ID #%s registered.', $mockObjectID ) ); } } /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?> http://cvs.php.net/co.php/pear/PHPUnit2/Extensions/MockObjects/Expectation/CallCount.php?r=1.1&p=1 Index: pear/PHPUnit2/Extensions/MockObjects/Expectation/CallCount.php +++ pear/PHPUnit2/Extensions/MockObjects/Expectation/CallCount.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * PHP Version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Testing * @package PHPUnit2 * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: CallCount.php,v 1.1 2005/07/25 07:19:26 sebastian Exp $ * @link http://pear.php.net/package/PHPUnit2 * @since File available since Release 2.4.0 */ require_once 'PHPUnit2/Util/Classloader.php'; /** * CallCount Expectation. * * @category Testing * @package PHPUnit2 * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version Release: @package_version@ * @link http://pear.php.net/package/PHPUnit2 * @since Class available since Release 2.4.0 * @static */ class PHPUnit2_Extensions_MockObjects_Expectation_CallCount extends PHPUnit2_Extensions_MockObjects_Expectation { /** * @var integer * @access protected */ protected $count; /** * Constructor. * * @param integer $mockObjectID * @param string $class * @param string $method * @param string $message * @param integer $count * @access public */ public function __construct($mockObjectID, $class, $method, $message, $count) { parent::__construct($mockObjectID, $class, $method, $message); $this->count = $count; } /** * Evaluates the expectation. * * @throws PHPUnit2_Framework_AssertionFailedError * @access public */ public function evaluate() { if ($this->registry->getCallCount($this->mockObjectID, $this->method) != $this->count) { throw new PHPUnit2_Framework_AssertionFailedError( sprintf( '%s::%s(): expected: %s, actual: %s', $this->class, $this->method, $this->count, $this->registry->getCallCount($this->mockObjectID, $this->method) ) ); } } } /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?> http://cvs.php.net/co.php/pear/PHPUnit2/Extensions/MockObjects/Expectation/MaximumCallCount.php?r=1.1&p=1 Index: pear/PHPUnit2/Extensions/MockObjects/Expectation/MaximumCallCount.php +++ pear/PHPUnit2/Extensions/MockObjects/Expectation/MaximumCallCount.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * PHP Version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Testing * @package PHPUnit2 * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: MaximumCallCount.php,v 1.1 2005/07/25 07:19:26 sebastian Exp $ * @link http://pear.php.net/package/PHPUnit2 * @since File available since Release 2.4.0 */ require_once 'PHPUnit2/Util/Classloader.php'; /** * MaximumCallCount Expectation. * * @category Testing * @package PHPUnit2 * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version Release: @package_version@ * @link http://pear.php.net/package/PHPUnit2 * @since Class available since Release 2.4.0 * @static */ class PHPUnit2_Extensions_MockObjects_Expectation_MaximumCallCount extends PHPUnit2_Extensions_MockObjects_Expectation_CallCount { /** * Evaluates the expectation. * * @throws PHPUnit2_Framework_AssertionFailedError * @access public */ public function evaluate() { if ($this->registry->getCallCount($this->mockObjectID, $this->method) > $this->count) { throw new PHPUnit2_Framework_AssertionFailedError( sprintf( '%s::%s(): expected: <= %s, actual: %s', $this->class, $this->method, $this->count, $this->registry->getCallCount($this->mockObjectID, $this->method) ) ); } } } /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?> http://cvs.php.net/co.php/pear/PHPUnit2/Extensions/MockObjects/Expectation/MinimumCallCount.php?r=1.1&p=1 Index: pear/PHPUnit2/Extensions/MockObjects/Expectation/MinimumCallCount.php +++ pear/PHPUnit2/Extensions/MockObjects/Expectation/MinimumCallCount.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * PHP Version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Testing * @package PHPUnit2 * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: MinimumCallCount.php,v 1.1 2005/07/25 07:19:26 sebastian Exp $ * @link http://pear.php.net/package/PHPUnit2 * @since File available since Release 2.4.0 */ require_once 'PHPUnit2/Util/Classloader.php'; /** * MinimumCallCount Expectation. * * @category Testing * @package PHPUnit2 * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version Release: @package_version@ * @link http://pear.php.net/package/PHPUnit2 * @since Class available since Release 2.4.0 * @static */ class PHPUnit2_Extensions_MockObjects_Expectation_MinimumCallCount extends PHPUnit2_Extensions_MockObjects_Expectation_CallCount { /** * Evaluates the expectation. * * @throws PHPUnit2_Framework_AssertionFailedError * @access public */ public function evaluate() { if ($this->registry->getCallCount($this->mockObjectID, $this->method) < $this->count) { throw new PHPUnit2_Framework_AssertionFailedError( sprintf( '%s::%s(): expected: >= %s, actual: %s', $this->class, $this->method, $this->count, $this->registry->getCallCount($this->mockObjectID, $this->method) ) ); } } } /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?>
« previous php.pear.cvs (#33285) next »