commitall

This commit is contained in:
Sampanna Rimal
2024-07-10 18:28:19 +05:45
parent 140abda4e6
commit 9cd05ef3cb
15723 changed files with 4818733 additions and 0 deletions

View File

@ -0,0 +1,31 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Builder interface for unique identifiers.
*
* Defines the interface for recording unique identifiers. The identifiers
* can be used to define the invocation order of expectations. The expectation
* is recorded using id() and then defined in order using
* PHPUnit_Framework_MockObject_Builder_Match::after().
*
* @since Interface available since Release 1.0.0
*/
interface PHPUnit_Framework_MockObject_Builder_Identity
{
/**
* Sets the identification of the expectation to $id.
*
* @note The identifier is unique per mock object.
*
* @param string $id Unique identification of expectation.
*/
public function id($id);
}

View File

@ -0,0 +1,291 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Builder for mocked or stubbed invocations.
*
* Provides methods for building expectations without having to resort to
* instantiating the various matchers manually. These methods also form a
* more natural way of reading the expectation. This class should be together
* with the test case PHPUnit_Framework_MockObject_TestCase.
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Builder_InvocationMocker implements PHPUnit_Framework_MockObject_Builder_MethodNameMatch
{
/**
* @var PHPUnit_Framework_MockObject_Stub_MatcherCollection
*/
protected $collection;
/**
* @var PHPUnit_Framework_MockObject_Matcher
*/
protected $matcher;
/**
* @var string[]
*/
private $configurableMethods = [];
/**
* @param PHPUnit_Framework_MockObject_Stub_MatcherCollection $collection
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher
* @param array $configurableMethods
*/
public function __construct(PHPUnit_Framework_MockObject_Stub_MatcherCollection $collection, PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher, array $configurableMethods)
{
$this->collection = $collection;
$this->matcher = new PHPUnit_Framework_MockObject_Matcher(
$invocationMatcher
);
$this->collection->addMatcher($this->matcher);
$this->configurableMethods = $configurableMethods;
}
/**
* @return PHPUnit_Framework_MockObject_Matcher
*/
public function getMatcher()
{
return $this->matcher;
}
/**
* @param mixed $id
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function id($id)
{
$this->collection->registerId($id, $this);
return $this;
}
/**
* @param PHPUnit_Framework_MockObject_Stub $stub
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function will(PHPUnit_Framework_MockObject_Stub $stub)
{
$this->matcher->stub = $stub;
return $this;
}
/**
* @param mixed $value
* @param mixed $nextValues, ...
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willReturn($value, ...$nextValues)
{
$stub = count($nextValues) === 0 ?
new PHPUnit_Framework_MockObject_Stub_Return($value) :
new PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls(
array_merge([$value], $nextValues)
);
return $this->will($stub);
}
/**
* @param mixed $reference
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willReturnReference(&$reference)
{
$stub = new PHPUnit_Framework_MockObject_Stub_ReturnReference($reference);
return $this->will($stub);
}
/**
* @param array $valueMap
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willReturnMap(array $valueMap)
{
$stub = new PHPUnit_Framework_MockObject_Stub_ReturnValueMap(
$valueMap
);
return $this->will($stub);
}
/**
* @param mixed $argumentIndex
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willReturnArgument($argumentIndex)
{
$stub = new PHPUnit_Framework_MockObject_Stub_ReturnArgument(
$argumentIndex
);
return $this->will($stub);
}
/**
* @param callable $callback
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willReturnCallback($callback)
{
$stub = new PHPUnit_Framework_MockObject_Stub_ReturnCallback(
$callback
);
return $this->will($stub);
}
/**
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willReturnSelf()
{
$stub = new PHPUnit_Framework_MockObject_Stub_ReturnSelf;
return $this->will($stub);
}
/**
* @param mixed $values, ...
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willReturnOnConsecutiveCalls(...$values)
{
$stub = new PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls($values);
return $this->will($stub);
}
/**
* @param Exception $exception
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function willThrowException(Exception $exception)
{
$stub = new PHPUnit_Framework_MockObject_Stub_Exception($exception);
return $this->will($stub);
}
/**
* @param mixed $id
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function after($id)
{
$this->matcher->afterMatchBuilderId = $id;
return $this;
}
/**
* Validate that a parameters matcher can be defined, throw exceptions otherwise.
*
* @throws PHPUnit_Framework_MockObject_RuntimeException
*/
private function canDefineParameters()
{
if ($this->matcher->methodNameMatcher === null) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'Method name matcher is not defined, cannot define parameter ' .
'matcher without one'
);
}
if ($this->matcher->parametersMatcher !== null) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'Parameter matcher is already defined, cannot redefine'
);
}
}
/**
* @param array ...$arguments
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function with(...$arguments)
{
$this->canDefineParameters();
$this->matcher->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_Parameters($arguments);
return $this;
}
/**
* @param array ...$arguments
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function withConsecutive(...$arguments)
{
$this->canDefineParameters();
$this->matcher->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters($arguments);
return $this;
}
/**
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function withAnyParameters()
{
$this->canDefineParameters();
$this->matcher->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_AnyParameters;
return $this;
}
/**
* @param PHPUnit_Framework_Constraint|string $constraint
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function method($constraint)
{
if ($this->matcher->methodNameMatcher !== null) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'Method name matcher is already defined, cannot redefine'
);
}
if (is_string($constraint) && !in_array(strtolower($constraint), $this->configurableMethods)) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'Trying to configure method "%s" which cannot be configured because it does not exist, has not been specified, is final, or is static',
$constraint
)
);
}
$this->matcher->methodNameMatcher = new PHPUnit_Framework_MockObject_Matcher_MethodName($constraint);
return $this;
}
}

View File

@ -0,0 +1,27 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Builder interface for invocation order matches.
*
* @since Interface available since Release 1.0.0
*/
interface PHPUnit_Framework_MockObject_Builder_Match extends PHPUnit_Framework_MockObject_Builder_Stub
{
/**
* Defines the expectation which must occur before the current is valid.
*
* @param string $id The identification of the expectation that should
* occur before this one.
*
* @return PHPUnit_Framework_MockObject_Builder_Stub
*/
public function after($id);
}

View File

@ -0,0 +1,27 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Builder interface for matcher of method names.
*
* @since Interface available since Release 1.0.0
*/
interface PHPUnit_Framework_MockObject_Builder_MethodNameMatch extends PHPUnit_Framework_MockObject_Builder_ParametersMatch
{
/**
* Adds a new method name match and returns the parameter match object for
* further matching possibilities.
*
* @param PHPUnit_Framework_Constraint $name Constraint for matching method, if a string is passed it will use the PHPUnit_Framework_Constraint_IsEqual
*
* @return PHPUnit_Framework_MockObject_Builder_ParametersMatch
*/
public function method($name);
}

View File

@ -0,0 +1,38 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Interface for builders which can register builders with a given identification.
*
* This interface relates to PHPUnit_Framework_MockObject_Builder_Identity.
*
* @since Interface available since Release 1.0.0
*/
interface PHPUnit_Framework_MockObject_Builder_Namespace
{
/**
* Looks up the match builder with identification $id and returns it.
*
* @param string $id The identification of the match builder
*
* @return PHPUnit_Framework_MockObject_Builder_Match
*/
public function lookupId($id);
/**
* Registers the match builder $builder with the identification $id. The
* builder can later be looked up using lookupId() to figure out if it
* has been invoked.
*
* @param string $id The identification of the match builder
* @param PHPUnit_Framework_MockObject_Builder_Match $builder The builder which is being registered
*/
public function registerId($id, PHPUnit_Framework_MockObject_Builder_Match $builder);
}

View File

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Builder interface for parameter matchers.
*
* @since Interface available since Release 1.0.0
*/
interface PHPUnit_Framework_MockObject_Builder_ParametersMatch extends PHPUnit_Framework_MockObject_Builder_Match
{
/**
* Sets the parameters to match for, each parameter to this funtion will
* be part of match. To perform specific matches or constraints create a
* new PHPUnit_Framework_Constraint and use it for the parameter.
* If the parameter value is not a constraint it will use the
* PHPUnit_Framework_Constraint_IsEqual for the value.
*
* Some examples:
* <code>
* // match first parameter with value 2
* $b->with(2);
* // match first parameter with value 'smock' and second identical to 42
* $b->with('smock', new PHPUnit_Framework_Constraint_IsEqual(42));
* </code>
*
* @return PHPUnit_Framework_MockObject_Builder_ParametersMatch
*/
public function with(...$arguments);
/**
* Sets a matcher which allows any kind of parameters.
*
* Some examples:
* <code>
* // match any number of parameters
* $b->withAnyParameters();
* </code>
*
* @return PHPUnit_Framework_MockObject_Matcher_AnyParameters
*/
public function withAnyParameters();
}

View File

@ -0,0 +1,27 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Builder interface for stubs which are actions replacing an invocation.
*
* @since Interface available since Release 1.0.0
*/
interface PHPUnit_Framework_MockObject_Builder_Stub extends PHPUnit_Framework_MockObject_Builder_Identity
{
/**
* Stubs the matching method with the stub object $stub. Any invocations of
* the matched method will now be handled by the stub instead.
*
* @param PHPUnit_Framework_MockObject_Stub $stub
*
* @return PHPUnit_Framework_MockObject_Builder_Identity
*/
public function will(PHPUnit_Framework_MockObject_Stub $stub);
}

View File

@ -0,0 +1,16 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @since Class available since Release 2.0.6
*/
class PHPUnit_Framework_MockObject_BadMethodCallException extends BadMethodCallException implements PHPUnit_Framework_MockObject_Exception
{
}

View File

@ -0,0 +1,18 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Interface for exceptions used by PHPUnit_MockObject.
*
* @since Interface available since Release 2.0.6
*/
interface PHPUnit_Framework_MockObject_Exception
{
}

View File

@ -0,0 +1,16 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @since Class available since Release 2.0.6
*/
class PHPUnit_Framework_MockObject_RuntimeException extends RuntimeException implements PHPUnit_Framework_MockObject_Exception
{
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
@trigger_error({deprecation}, E_USER_DEPRECATED);

View File

@ -0,0 +1,40 @@
{prologue}{class_declaration}
{
private $__phpunit_invocationMocker;
private $__phpunit_originalObject;
private $__phpunit_configurable = {configurable};
{clone}{mocked_methods}
public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
{
return $this->__phpunit_getInvocationMocker()->expects($matcher);
}
{method}
public function __phpunit_setOriginalObject($originalObject)
{
$this->__phpunit_originalObject = $originalObject;
}
public function __phpunit_getInvocationMocker()
{
if ($this->__phpunit_invocationMocker === null) {
$this->__phpunit_invocationMocker = new PHPUnit_Framework_MockObject_InvocationMocker($this->__phpunit_configurable);
}
return $this->__phpunit_invocationMocker;
}
public function __phpunit_hasMatchers()
{
return $this->__phpunit_getInvocationMocker()->hasMatchers();
}
public function __phpunit_verify($unsetInvocationMocker = true)
{
$this->__phpunit_getInvocationMocker()->verify();
if ($unsetInvocationMocker) {
$this->__phpunit_invocationMocker = null;
}
}
}{epilogue}

View File

@ -0,0 +1,7 @@
public function method()
{
$any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
$expects = $this->expects($any);
return call_user_func_array(array($expects, 'method'), func_get_args());
}

View File

@ -0,0 +1,4 @@
public function __clone()
{
$this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationMocker();
}

View File

@ -0,0 +1,22 @@
{modifier} function {reference}{method_name}({arguments_decl}){return_delim}{return_type}
{{deprecation}
$arguments = array({arguments_call});
$count = func_num_args();
if ($count > {arguments_count}) {
$_arguments = func_get_args();
for ($i = {arguments_count}; $i < $count; $i++) {
$arguments[] = $_arguments[$i];
}
}
$result = $this->__phpunit_getInvocationMocker()->invoke(
new PHPUnit_Framework_MockObject_Invocation_Object(
'{class_name}', '{method_name}', $arguments, '{return_type}', $this, {clone_arguments}
)
);
return $result;
}

View File

@ -0,0 +1,20 @@
{modifier} function {reference}{method_name}({arguments_decl}){return_delim}{return_type}
{{deprecation}
$arguments = array({arguments_call});
$count = func_num_args();
if ($count > {arguments_count}) {
$_arguments = func_get_args();
for ($i = {arguments_count}; $i < $count; $i++) {
$arguments[] = $_arguments[$i];
}
}
$this->__phpunit_getInvocationMocker()->invoke(
new PHPUnit_Framework_MockObject_Invocation_Object(
'{class_name}', '{method_name}', $arguments, '{return_type}', $this, {clone_arguments}
)
);
}

View File

@ -0,0 +1,5 @@
{modifier} function {reference}{method_name}({arguments_decl}){return_delim}{return_type}
{
throw new PHPUnit_Framework_MockObject_BadMethodCallException('Static method "{method_name}" cannot be invoked on mock object');
}

View File

@ -0,0 +1,22 @@
{modifier} function {reference}{method_name}({arguments_decl}){return_delim}{return_type}
{
$arguments = array({arguments_call});
$count = func_num_args();
if ($count > {arguments_count}) {
$_arguments = func_get_args();
for ($i = {arguments_count}; $i < $count; $i++) {
$arguments[] = $_arguments[$i];
}
}
$this->__phpunit_getInvocationMocker()->invoke(
new PHPUnit_Framework_MockObject_Invocation_Object(
'{class_name}', '{method_name}', $arguments, '{return_type}', $this, {clone_arguments}
)
);
return call_user_func_array(array($this->__phpunit_originalObject, "{method_name}"), $arguments);
}

View File

@ -0,0 +1,22 @@
{modifier} function {reference}{method_name}({arguments_decl}){return_delim}{return_type}
{
$arguments = array({arguments_call});
$count = func_num_args();
if ($count > {arguments_count}) {
$_arguments = func_get_args();
for ($i = {arguments_count}; $i < $count; $i++) {
$arguments[] = $_arguments[$i];
}
}
$this->__phpunit_getInvocationMocker()->invoke(
new PHPUnit_Framework_MockObject_Invocation_Object(
'{class_name}', '{method_name}', $arguments, '{return_type}', $this, {clone_arguments}
)
);
call_user_func_array(array($this->__phpunit_originalObject, "{method_name}"), $arguments);
}

View File

@ -0,0 +1,4 @@
{prologue}class {class_name}
{
use {trait_name};
}

View File

@ -0,0 +1,5 @@
public function __clone()
{
$this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationMocker();
parent::__clone();
}

View File

@ -0,0 +1,7 @@
{namespace}class {class_name} extends \SoapClient
{
public function __construct($wsdl, array $options)
{
parent::__construct('{wsdl}', $options);
}
{methods}}

View File

@ -0,0 +1,4 @@
public function {method_name}({arguments})
{
}

View File

@ -0,0 +1,22 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Interface for invocations.
*
* @since Interface available since Release 1.0.0
*/
interface PHPUnit_Framework_MockObject_Invocation
{
/**
* @return mixed Mocked return value.
*/
public function generateReturnValue();
}

View File

@ -0,0 +1,37 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Represents a non-static invocation.
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Invocation_Object extends PHPUnit_Framework_MockObject_Invocation_Static
{
/**
* @var object
*/
public $object;
/**
* @param string $className
* @param string $methodName
* @param array $parameters
* @param string $returnType
* @param object $object
* @param bool $cloneObjects
*/
public function __construct($className, $methodName, array $parameters, $returnType, $object, $cloneObjects = false)
{
parent::__construct($className, $methodName, $parameters, $returnType, $cloneObjects);
$this->object = $object;
}
}

View File

@ -0,0 +1,207 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use SebastianBergmann\Exporter\Exporter;
/**
* Represents a static invocation.
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Invocation_Static implements PHPUnit_Framework_MockObject_Invocation, PHPUnit_Framework_SelfDescribing
{
/**
* @var array
*/
protected static $uncloneableExtensions = [
'mysqli' => true,
'SQLite' => true,
'sqlite3' => true,
'tidy' => true,
'xmlwriter' => true,
'xsl' => true
];
/**
* @var array
*/
protected static $uncloneableClasses = [
'Closure',
'COMPersistHelper',
'IteratorIterator',
'RecursiveIteratorIterator',
'SplFileObject',
'PDORow',
'ZipArchive'
];
/**
* @var string
*/
public $className;
/**
* @var string
*/
public $methodName;
/**
* @var array
*/
public $parameters;
/**
* @var string
*/
public $returnType;
/**
* @var bool
*/
public $returnTypeNullable = false;
/**
* @param string $className
* @param string $methodName
* @param array $parameters
* @param string $returnType
* @param bool $cloneObjects
*/
public function __construct($className, $methodName, array $parameters, $returnType, $cloneObjects = false)
{
$this->className = $className;
$this->methodName = $methodName;
$this->parameters = $parameters;
if (strpos($returnType, '?') === 0) {
$returnType = substr($returnType, 1);
$this->returnTypeNullable = true;
}
$this->returnType = $returnType;
if (!$cloneObjects) {
return;
}
foreach ($this->parameters as $key => $value) {
if (is_object($value)) {
$this->parameters[$key] = $this->cloneObject($value);
}
}
}
/**
* @return string
*/
public function toString()
{
$exporter = new Exporter;
return sprintf(
'%s::%s(%s)%s',
$this->className,
$this->methodName,
implode(
', ',
array_map(
[$exporter, 'shortenedExport'],
$this->parameters
)
),
$this->returnType ? sprintf(': %s', $this->returnType) : ''
);
}
/**
* @return mixed Mocked return value.
*/
public function generateReturnValue()
{
switch ($this->returnType) {
case '': return;
case 'string': return $this->returnTypeNullable ? null : '';
case 'float': return $this->returnTypeNullable ? null : 0.0;
case 'int': return $this->returnTypeNullable ? null : 0;
case 'bool': return $this->returnTypeNullable ? null : false;
case 'array': return $this->returnTypeNullable ? null : [];
case 'void': return;
case 'callable':
case 'Closure':
return function () {};
case 'Traversable':
case 'Generator':
$generator = function () { yield; };
return $generator();
default:
if ($this->returnTypeNullable) {
return null;
}
$generator = new PHPUnit_Framework_MockObject_Generator;
return $generator->getMock($this->returnType, [], [], '', false);
}
}
/**
* @param object $original
*
* @return object
*/
protected function cloneObject($original)
{
$cloneable = null;
$object = new ReflectionObject($original);
// Check the blacklist before asking PHP reflection to work around
// https://bugs.php.net/bug.php?id=53967
if ($object->isInternal() &&
isset(self::$uncloneableExtensions[$object->getExtensionName()])) {
$cloneable = false;
}
if ($cloneable === null) {
foreach (self::$uncloneableClasses as $class) {
if ($original instanceof $class) {
$cloneable = false;
break;
}
}
}
if ($cloneable === null && method_exists($object, 'isCloneable')) {
$cloneable = $object->isCloneable();
}
if ($cloneable === null && $object->hasMethod('__clone')) {
$method = $object->getMethod('__clone');
$cloneable = $method->isPublic();
}
if ($cloneable === null) {
$cloneable = true;
}
if ($cloneable) {
try {
return clone $original;
} catch (Exception $e) {
return $original;
}
} else {
return $original;
}
}
}

View File

@ -0,0 +1,178 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Mocker for invocations which are sent from
* PHPUnit_Framework_MockObject_MockObject objects.
*
* Keeps track of all expectations and stubs as well as registering
* identifications for builders.
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_InvocationMocker implements PHPUnit_Framework_MockObject_Stub_MatcherCollection, PHPUnit_Framework_MockObject_Invokable, PHPUnit_Framework_MockObject_Builder_Namespace
{
/**
* @var PHPUnit_Framework_MockObject_Matcher_Invocation[]
*/
protected $matchers = [];
/**
* @var PHPUnit_Framework_MockObject_Builder_Match[]
*/
protected $builderMap = [];
/**
* @var string[]
*/
private $configurableMethods = [];
/**
* @param array $configurableMethods
*/
public function __construct(array $configurableMethods)
{
$this->configurableMethods = $configurableMethods;
}
/**
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
*/
public function addMatcher(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
{
$this->matchers[] = $matcher;
}
/**
* @since Method available since Release 1.1.0
*/
public function hasMatchers()
{
foreach ($this->matchers as $matcher) {
if ($matcher->hasMatchers()) {
return true;
}
}
return false;
}
/**
* @param mixed $id
*
* @return bool|null
*/
public function lookupId($id)
{
if (isset($this->builderMap[$id])) {
return $this->builderMap[$id];
}
return;
}
/**
* @param mixed $id
* @param PHPUnit_Framework_MockObject_Builder_Match $builder
*
* @throws PHPUnit_Framework_MockObject_RuntimeException
*/
public function registerId($id, PHPUnit_Framework_MockObject_Builder_Match $builder)
{
if (isset($this->builderMap[$id])) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'Match builder with id <' . $id . '> is already registered.'
);
}
$this->builderMap[$id] = $builder;
}
/**
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
{
return new PHPUnit_Framework_MockObject_Builder_InvocationMocker(
$this,
$matcher,
$this->configurableMethods
);
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return mixed
*
* @throws Exception
*/
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$exception = null;
$hasReturnValue = false;
$returnValue = null;
foreach ($this->matchers as $match) {
try {
if ($match->matches($invocation)) {
$value = $match->invoked($invocation);
if (!$hasReturnValue) {
$returnValue = $value;
$hasReturnValue = true;
}
}
} catch (Exception $e) {
$exception = $e;
}
}
if ($exception !== null) {
throw $exception;
}
if ($hasReturnValue) {
return $returnValue;
} elseif (strtolower($invocation->methodName) == '__tostring') {
return '';
}
return $invocation->generateReturnValue();
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
{
foreach ($this->matchers as $matcher) {
if (!$matcher->matches($invocation)) {
return false;
}
}
return true;
}
/**
* @return bool
*/
public function verify()
{
foreach ($this->matchers as $matcher) {
$matcher->verify();
}
}
}

View File

@ -0,0 +1,39 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Interface for classes which can be invoked.
*
* The invocation will be taken from a mock object and passed to an object
* of this class.
*
* @since Interface available since Release 1.0.0
*/
interface PHPUnit_Framework_MockObject_Invokable extends PHPUnit_Framework_MockObject_Verifiable
{
/**
* Invokes the invocation object $invocation so that it can be checked for
* expectations or matched against stubs.
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation The invocation object passed from mock object
*
* @return object
*/
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation);
/**
* Checks if the invocation matches.
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation The invocation object passed from mock object
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation);
}

View File

@ -0,0 +1,274 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Main matcher which defines a full expectation using method, parameter and
* invocation matchers.
* This matcher encapsulates all the other matchers and allows the builder to
* set the specific matchers when the appropriate methods are called (once(),
* where() etc.).
*
* All properties are public so that they can easily be accessed by the builder.
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Matcher implements PHPUnit_Framework_MockObject_Matcher_Invocation
{
/**
* @var PHPUnit_Framework_MockObject_Matcher_Invocation
*/
public $invocationMatcher;
/**
* @var mixed
*/
public $afterMatchBuilderId = null;
/**
* @var bool
*/
public $afterMatchBuilderIsInvoked = false;
/**
* @var PHPUnit_Framework_MockObject_Matcher_MethodName
*/
public $methodNameMatcher = null;
/**
* @var PHPUnit_Framework_MockObject_Matcher_Parameters
*/
public $parametersMatcher = null;
/**
* @var PHPUnit_Framework_MockObject_Stub
*/
public $stub = null;
/**
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher
*/
public function __construct(PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher)
{
$this->invocationMatcher = $invocationMatcher;
}
/**
* @return string
*/
public function toString()
{
$list = [];
if ($this->invocationMatcher !== null) {
$list[] = $this->invocationMatcher->toString();
}
if ($this->methodNameMatcher !== null) {
$list[] = 'where ' . $this->methodNameMatcher->toString();
}
if ($this->parametersMatcher !== null) {
$list[] = 'and ' . $this->parametersMatcher->toString();
}
if ($this->afterMatchBuilderId !== null) {
$list[] = 'after ' . $this->afterMatchBuilderId;
}
if ($this->stub !== null) {
$list[] = 'will ' . $this->stub->toString();
}
return implode(' ', $list);
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return mixed
*/
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
{
if ($this->invocationMatcher === null) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'No invocation matcher is set'
);
}
if ($this->methodNameMatcher === null) {
throw new PHPUnit_Framework_MockObject_RuntimeException('No method matcher is set');
}
if ($this->afterMatchBuilderId !== null) {
$builder = $invocation->object
->__phpunit_getInvocationMocker()
->lookupId($this->afterMatchBuilderId);
if (!$builder) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'No builder found for match builder identification <%s>',
$this->afterMatchBuilderId
)
);
}
$matcher = $builder->getMatcher();
if ($matcher && $matcher->invocationMatcher->hasBeenInvoked()) {
$this->afterMatchBuilderIsInvoked = true;
}
}
$this->invocationMatcher->invoked($invocation);
try {
if ($this->parametersMatcher !== null &&
!$this->parametersMatcher->matches($invocation)) {
$this->parametersMatcher->verify();
}
} catch (PHPUnit_Framework_ExpectationFailedException $e) {
throw new PHPUnit_Framework_ExpectationFailedException(
sprintf(
"Expectation failed for %s when %s\n%s",
$this->methodNameMatcher->toString(),
$this->invocationMatcher->toString(),
$e->getMessage()
),
$e->getComparisonFailure()
);
}
if ($this->stub) {
return $this->stub->invoke($invocation);
}
return $invocation->generateReturnValue();
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
{
if ($this->afterMatchBuilderId !== null) {
$builder = $invocation->object
->__phpunit_getInvocationMocker()
->lookupId($this->afterMatchBuilderId);
if (!$builder) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'No builder found for match builder identification <%s>',
$this->afterMatchBuilderId
)
);
}
$matcher = $builder->getMatcher();
if (!$matcher) {
return false;
}
if (!$matcher->invocationMatcher->hasBeenInvoked()) {
return false;
}
}
if ($this->invocationMatcher === null) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'No invocation matcher is set'
);
}
if ($this->methodNameMatcher === null) {
throw new PHPUnit_Framework_MockObject_RuntimeException('No method matcher is set');
}
if (!$this->invocationMatcher->matches($invocation)) {
return false;
}
try {
if (!$this->methodNameMatcher->matches($invocation)) {
return false;
}
} catch (PHPUnit_Framework_ExpectationFailedException $e) {
throw new PHPUnit_Framework_ExpectationFailedException(
sprintf(
"Expectation failed for %s when %s\n%s",
$this->methodNameMatcher->toString(),
$this->invocationMatcher->toString(),
$e->getMessage()
),
$e->getComparisonFailure()
);
}
return true;
}
/**
* @throws PHPUnit_Framework_MockObject_RuntimeException
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public function verify()
{
if ($this->invocationMatcher === null) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'No invocation matcher is set'
);
}
if ($this->methodNameMatcher === null) {
throw new PHPUnit_Framework_MockObject_RuntimeException('No method matcher is set');
}
try {
$this->invocationMatcher->verify();
if ($this->parametersMatcher === null) {
$this->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_AnyParameters;
}
$invocationIsAny = $this->invocationMatcher instanceof PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
$invocationIsNever = $this->invocationMatcher instanceof PHPUnit_Framework_MockObject_Matcher_InvokedCount && $this->invocationMatcher->isNever();
if (!$invocationIsAny && !$invocationIsNever) {
$this->parametersMatcher->verify();
}
} catch (PHPUnit_Framework_ExpectationFailedException $e) {
throw new PHPUnit_Framework_ExpectationFailedException(
sprintf(
"Expectation failed for %s when %s.\n%s",
$this->methodNameMatcher->toString(),
$this->invocationMatcher->toString(),
PHPUnit_Framework_TestFailure::exceptionToString($e)
)
);
}
}
/**
* @since Method available since Release 1.2.4
*/
public function hasMatchers()
{
if ($this->invocationMatcher !== null &&
!$this->invocationMatcher instanceof PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount) {
return true;
}
return false;
}
}

View File

@ -0,0 +1,32 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Invocation matcher which checks if a method has been invoked zero or more
* times. This matcher will always match.
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
{
/**
* @return string
*/
public function toString()
{
return 'invoked zero or more times';
}
/**
*/
public function verify()
{
}
}

View File

@ -0,0 +1,35 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Invocation matcher which allows any parameters to a method.
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Matcher_AnyParameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
{
/**
* @return string
*/
public function toString()
{
return 'with any parameters';
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
{
return true;
}
}

View File

@ -0,0 +1,126 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Invocation matcher which looks for sets of specific parameters in the invocations.
*
* Checks the parameters of the incoming invocations, the parameter list is
* checked against the defined constraints in $parameters. If the constraint
* is met it will return true in matches().
*
* It takes a list of match groups and and increases a call index after each invocation.
* So the first invocation uses the first group of constraints, the second the next and so on.
*/
class PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
{
/**
* @var array
*/
private $parameterGroups = [];
/**
* @var array
*/
private $invocations = [];
/**
* @param array $parameterGroups
*/
public function __construct(array $parameterGroups)
{
foreach ($parameterGroups as $index => $parameters) {
foreach ($parameters as $parameter) {
if (!$parameter instanceof PHPUnit_Framework_Constraint) {
$parameter = new PHPUnit_Framework_Constraint_IsEqual($parameter);
}
$this->parameterGroups[$index][] = $parameter;
}
}
}
/**
* @return string
*/
public function toString()
{
$text = 'with consecutive parameters';
return $text;
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$this->invocations[] = $invocation;
$callIndex = count($this->invocations) - 1;
$this->verifyInvocation($invocation, $callIndex);
return false;
}
public function verify()
{
foreach ($this->invocations as $callIndex => $invocation) {
$this->verifyInvocation($invocation, $callIndex);
}
}
/**
* Verify a single invocation
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation
* @param int $callIndex
*
* @throws PHPUnit_Framework_ExpectationFailedException
*/
private function verifyInvocation(PHPUnit_Framework_MockObject_Invocation $invocation, $callIndex)
{
if (isset($this->parameterGroups[$callIndex])) {
$parameters = $this->parameterGroups[$callIndex];
} else {
// no parameter assertion for this call index
return;
}
if ($invocation === null) {
throw new PHPUnit_Framework_ExpectationFailedException(
'Mocked method does not exist.'
);
}
if (count($invocation->parameters) < count($parameters)) {
throw new PHPUnit_Framework_ExpectationFailedException(
sprintf(
'Parameter count for invocation %s is too low.',
$invocation->toString()
)
);
}
foreach ($parameters as $i => $parameter) {
$parameter->evaluate(
$invocation->parameters[$i],
sprintf(
'Parameter %s for invocation #%d %s does not match expected ' .
'value.',
$i,
$callIndex,
$invocation->toString()
)
);
}
}
}

View File

@ -0,0 +1,46 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Interface for classes which matches an invocation based on its
* method name, argument, order or call count.
*
* @since Interface available since Release 1.0.0
*/
interface PHPUnit_Framework_MockObject_Matcher_Invocation extends PHPUnit_Framework_SelfDescribing, PHPUnit_Framework_MockObject_Verifiable
{
/**
* Registers the invocation $invocation in the object as being invoked.
* This will only occur after matches() returns true which means the
* current invocation is the correct one.
*
* The matcher can store information from the invocation which can later
* be checked in verify(), or it can check the values directly and throw
* and exception if an expectation is not met.
*
* If the matcher is a stub it will also have a return value.
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation Object containing information on a mocked or stubbed method which was invoked
*
* @return mixed
*/
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation);
/**
* Checks if the invocation $invocation matches the current rules. If it does
* the matcher will get the invoked() method called which should check if an
* expectation is met.
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation Object containing information on a mocked or stubbed method which was invoked
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation);
}

View File

@ -0,0 +1,87 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Invocation matcher which checks if a method was invoked at a certain index.
*
* If the expected index number does not match the current invocation index it
* will not match which means it skips all method and parameter matching. Only
* once the index is reached will the method and parameter start matching and
* verifying.
*
* If the index is never reached it will throw an exception in index.
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex implements PHPUnit_Framework_MockObject_Matcher_Invocation
{
/**
* @var int
*/
protected $sequenceIndex;
/**
* @var int
*/
protected $currentIndex = -1;
/**
* @param int $sequenceIndex
*/
public function __construct($sequenceIndex)
{
$this->sequenceIndex = $sequenceIndex;
}
/**
* @return string
*/
public function toString()
{
return 'invoked at sequence index ' . $this->sequenceIndex;
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$this->currentIndex++;
return $this->currentIndex == $this->sequenceIndex;
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*/
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
{
}
/**
* Verifies that the current expectation is valid. If everything is OK the
* code should just return, if not it must throw an exception.
*
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public function verify()
{
if ($this->currentIndex < $this->sequenceIndex) {
throw new PHPUnit_Framework_ExpectationFailedException(
sprintf(
'The expected invocation at index %s was never reached.',
$this->sequenceIndex
)
);
}
}
}

View File

@ -0,0 +1,57 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Invocation matcher which checks if a method has been invoked at least
* N times.
*
* @since Class available since Release 2.2.0
*/
class PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
{
/**
* @var int
*/
private $requiredInvocations;
/**
* @param int $requiredInvocations
*/
public function __construct($requiredInvocations)
{
$this->requiredInvocations = $requiredInvocations;
}
/**
* @return string
*/
public function toString()
{
return 'invoked at least ' . $this->requiredInvocations . ' times';
}
/**
* Verifies that the current expectation is valid. If everything is OK the
* code should just return, if not it must throw an exception.
*
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public function verify()
{
$count = $this->getInvocationCount();
if ($count < $this->requiredInvocations) {
throw new PHPUnit_Framework_ExpectationFailedException(
'Expected invocation at least ' . $this->requiredInvocations .
' times but it occurred ' . $count . ' time(s).'
);
}
}
}

View File

@ -0,0 +1,45 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Invocation matcher which checks if a method has been invoked at least one
* time.
*
* If the number of invocations is 0 it will throw an exception in verify.
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
{
/**
* @return string
*/
public function toString()
{
return 'invoked at least once';
}
/**
* Verifies that the current expectation is valid. If everything is OK the
* code should just return, if not it must throw an exception.
*
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public function verify()
{
$count = $this->getInvocationCount();
if ($count < 1) {
throw new PHPUnit_Framework_ExpectationFailedException(
'Expected invocation at least once but it never occurred.'
);
}
}
}

View File

@ -0,0 +1,57 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Invocation matcher which checks if a method has been invoked at least
* N times.
*
* @since Class available since Release 2.2.0
*/
class PHPUnit_Framework_MockObject_Matcher_InvokedAtMostCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
{
/**
* @var int
*/
private $allowedInvocations;
/**
* @param int $allowedInvocations
*/
public function __construct($allowedInvocations)
{
$this->allowedInvocations = $allowedInvocations;
}
/**
* @return string
*/
public function toString()
{
return 'invoked at most ' . $this->allowedInvocations . ' times';
}
/**
* Verifies that the current expectation is valid. If everything is OK the
* code should just return, if not it must throw an exception.
*
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public function verify()
{
$count = $this->getInvocationCount();
if ($count > $this->allowedInvocations) {
throw new PHPUnit_Framework_ExpectationFailedException(
'Expected invocation at most ' . $this->allowedInvocations .
' times but it occurred ' . $count . ' time(s).'
);
}
}
}

View File

@ -0,0 +1,110 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Invocation matcher which checks if a method has been invoked a certain amount
* of times.
* If the number of invocations exceeds the value it will immediately throw an
* exception,
* If the number is less it will later be checked in verify() and also throw an
* exception.
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Matcher_InvokedCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
{
/**
* @var int
*/
protected $expectedCount;
/**
* @param int $expectedCount
*/
public function __construct($expectedCount)
{
$this->expectedCount = $expectedCount;
}
/**
* @return bool
*/
public function isNever()
{
return $this->expectedCount == 0;
}
/**
* @return string
*/
public function toString()
{
return 'invoked ' . $this->expectedCount . ' time(s)';
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
{
parent::invoked($invocation);
$count = $this->getInvocationCount();
if ($count > $this->expectedCount) {
$message = $invocation->toString() . ' ';
switch ($this->expectedCount) {
case 0: {
$message .= 'was not expected to be called.';
}
break;
case 1: {
$message .= 'was not expected to be called more than once.';
}
break;
default: {
$message .= sprintf(
'was not expected to be called more than %d times.',
$this->expectedCount
);
}
}
throw new PHPUnit_Framework_ExpectationFailedException($message);
}
}
/**
* Verifies that the current expectation is valid. If everything is OK the
* code should just return, if not it must throw an exception.
*
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public function verify()
{
$count = $this->getInvocationCount();
if ($count !== $this->expectedCount) {
throw new PHPUnit_Framework_ExpectationFailedException(
sprintf(
'Method was expected to be called %d times, ' .
'actually called %d times.',
$this->expectedCount,
$count
)
);
}
}
}

View File

@ -0,0 +1,68 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Records invocations and provides convenience methods for checking them later
* on.
* This abstract class can be implemented by matchers which needs to check the
* number of times an invocation has occurred.
*
* @since Class available since Release 1.0.0
* @abstract
*/
abstract class PHPUnit_Framework_MockObject_Matcher_InvokedRecorder implements PHPUnit_Framework_MockObject_Matcher_Invocation
{
/**
* @var PHPUnit_Framework_MockObject_Invocation[]
*/
protected $invocations = [];
/**
* @return int
*/
public function getInvocationCount()
{
return count($this->invocations);
}
/**
* @return PHPUnit_Framework_MockObject_Invocation[]
*/
public function getInvocations()
{
return $this->invocations;
}
/**
* @return bool
*/
public function hasBeenInvoked()
{
return count($this->invocations) > 0;
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*/
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$this->invocations[] = $invocation;
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
{
return true;
}
}

View File

@ -0,0 +1,68 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Invocation matcher which looks for a specific method name in the invocations.
*
* Checks the method name all incoming invocations, the name is checked against
* the defined constraint $constraint. If the constraint is met it will return
* true in matches().
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Matcher_MethodName extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
{
/**
* @var PHPUnit_Framework_Constraint
*/
protected $constraint;
/**
* @param PHPUnit_Framework_Constraint|string
*
* @throws PHPUnit_Framework_Constraint
*/
public function __construct($constraint)
{
if (!$constraint instanceof PHPUnit_Framework_Constraint) {
if (!is_string($constraint)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
}
$constraint = new PHPUnit_Framework_Constraint_IsEqual(
$constraint,
0,
10,
false,
true
);
}
$this->constraint = $constraint;
}
/**
* @return string
*/
public function toString()
{
return 'method name ' . $this->constraint->toString();
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
{
return $this->constraint->evaluate($invocation->methodName, '', true);
}
}

View File

@ -0,0 +1,158 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Invocation matcher which looks for specific parameters in the invocations.
*
* Checks the parameters of all incoming invocations, the parameter list is
* checked against the defined constraints in $parameters. If the constraint
* is met it will return true in matches().
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Matcher_Parameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
{
/**
* @var PHPUnit_Framework_Constraint[]
*/
protected $parameters = [];
/**
* @var PHPUnit_Framework_MockObject_Invocation
*/
protected $invocation;
/**
* @var PHPUnit_Framework_ExpectationFailedException
*/
private $parameterVerificationResult;
/**
* @param array $parameters
*/
public function __construct(array $parameters)
{
foreach ($parameters as $parameter) {
if (!($parameter instanceof PHPUnit_Framework_Constraint)) {
$parameter = new PHPUnit_Framework_Constraint_IsEqual(
$parameter
);
}
$this->parameters[] = $parameter;
}
}
/**
* @return string
*/
public function toString()
{
$text = 'with parameter';
foreach ($this->parameters as $index => $parameter) {
if ($index > 0) {
$text .= ' and';
}
$text .= ' ' . $index . ' ' . $parameter->toString();
}
return $text;
}
/**
* @param PHPUnit_Framework_MockObject_Invocation $invocation
*
* @return bool
*/
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$this->invocation = $invocation;
$this->parameterVerificationResult = null;
try {
$this->parameterVerificationResult = $this->verify();
return $this->parameterVerificationResult;
} catch (PHPUnit_Framework_ExpectationFailedException $e) {
$this->parameterVerificationResult = $e;
throw $this->parameterVerificationResult;
}
}
/**
* Checks if the invocation $invocation matches the current rules. If it
* does the matcher will get the invoked() method called which should check
* if an expectation is met.
*
* @return bool
*
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public function verify()
{
if (isset($this->parameterVerificationResult)) {
return $this->guardAgainstDuplicateEvaluationOfParameterConstraints();
}
if ($this->invocation === null) {
throw new PHPUnit_Framework_ExpectationFailedException(
'Mocked method does not exist.'
);
}
if (count($this->invocation->parameters) < count($this->parameters)) {
$message = 'Parameter count for invocation %s is too low.';
// The user called `->with($this->anything())`, but may have meant
// `->withAnyParameters()`.
//
// @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/199
if (count($this->parameters) === 1 &&
get_class($this->parameters[0]) === 'PHPUnit_Framework_Constraint_IsAnything') {
$message .= "\nTo allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead.";
}
throw new PHPUnit_Framework_ExpectationFailedException(
sprintf($message, $this->invocation->toString())
);
}
foreach ($this->parameters as $i => $parameter) {
$parameter->evaluate(
$this->invocation->parameters[$i],
sprintf(
'Parameter %s for invocation %s does not match expected ' .
'value.',
$i,
$this->invocation->toString()
)
);
}
return true;
}
/**
* @return bool
*
* @throws PHPUnit_Framework_ExpectationFailedException
*/
private function guardAgainstDuplicateEvaluationOfParameterConstraints()
{
if ($this->parameterVerificationResult instanceof Exception) {
throw $this->parameterVerificationResult;
}
return (bool) $this->parameterVerificationResult;
}
}

View File

@ -0,0 +1,54 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Invocation matcher which does not care about previous state from earlier
* invocations.
*
* This abstract class can be implemented by matchers which does not care about
* state but only the current run-time value of the invocation itself.
*
* @since Class available since Release 1.0.0
* @abstract
*/
abstract class PHPUnit_Framework_MockObject_Matcher_StatelessInvocation implements PHPUnit_Framework_MockObject_Matcher_Invocation
{
/**
* Registers the invocation $invocation in the object as being invoked.
* This will only occur after matches() returns true which means the
* current invocation is the correct one.
*
* The matcher can store information from the invocation which can later
* be checked in verify(), or it can check the values directly and throw
* and exception if an expectation is not met.
*
* If the matcher is a stub it will also have a return value.
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation Object containing information on a mocked or stubbed method which was invoked
*
* @return mixed
*/
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
{
}
/**
* Checks if the invocation $invocation matches the current rules. If it does
* the matcher will get the invoked() method called which should check if an
* expectation is met.
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation Object containing information on a mocked or stubbed method which was invoked
*
* @return bool
*/
public function verify()
{
}
}

View File

@ -0,0 +1,408 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Implementation of the Builder pattern for Mock objects.
*
* @since File available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_MockBuilder
{
/**
* @var PHPUnit_Framework_TestCase
*/
private $testCase;
/**
* @var string
*/
private $type;
/**
* @var array
*/
private $methods = [];
/**
* @var array
*/
private $methodsExcept = [];
/**
* @var string
*/
private $mockClassName = '';
/**
* @var array
*/
private $constructorArgs = [];
/**
* @var bool
*/
private $originalConstructor = true;
/**
* @var bool
*/
private $originalClone = true;
/**
* @var bool
*/
private $autoload = true;
/**
* @var bool
*/
private $cloneArguments = false;
/**
* @var bool
*/
private $callOriginalMethods = false;
/**
* @var object
*/
private $proxyTarget = null;
/**
* @var bool
*/
private $allowMockingUnknownTypes = true;
/**
* @var PHPUnit_Framework_MockObject_Generator
*/
private $generator;
/**
* @param PHPUnit_Framework_TestCase $testCase
* @param array|string $type
*/
public function __construct(PHPUnit_Framework_TestCase $testCase, $type)
{
$this->testCase = $testCase;
$this->type = $type;
$this->generator = new PHPUnit_Framework_MockObject_Generator;
}
/**
* Creates a mock object using a fluent interface.
*
* @return PHPUnit_Framework_MockObject_MockObject
*/
public function getMock()
{
$object = $this->generator->getMock(
$this->type,
$this->methods,
$this->constructorArgs,
$this->mockClassName,
$this->originalConstructor,
$this->originalClone,
$this->autoload,
$this->cloneArguments,
$this->callOriginalMethods,
$this->proxyTarget,
$this->allowMockingUnknownTypes
);
$this->testCase->registerMockObject($object);
return $object;
}
/**
* Creates a mock object for an abstract class using a fluent interface.
*
* @return PHPUnit_Framework_MockObject_MockObject
*/
public function getMockForAbstractClass()
{
$object = $this->generator->getMockForAbstractClass(
$this->type,
$this->constructorArgs,
$this->mockClassName,
$this->originalConstructor,
$this->originalClone,
$this->autoload,
$this->methods,
$this->cloneArguments
);
$this->testCase->registerMockObject($object);
return $object;
}
/**
* Creates a mock object for a trait using a fluent interface.
*
* @return PHPUnit_Framework_MockObject_MockObject
*/
public function getMockForTrait()
{
$object = $this->generator->getMockForTrait(
$this->type,
$this->constructorArgs,
$this->mockClassName,
$this->originalConstructor,
$this->originalClone,
$this->autoload,
$this->methods,
$this->cloneArguments
);
$this->testCase->registerMockObject($object);
return $object;
}
/**
* Specifies the subset of methods to mock. Default is to mock all of them.
*
* @param array|null $methods
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*/
public function setMethods(array $methods = null)
{
$this->methods = $methods;
return $this;
}
/**
* Specifies the subset of methods to not mock. Default is to mock all of them.
*
* @param array $methods
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*/
public function setMethodsExcept(array $methods = [])
{
$this->methodsExcept = $methods;
$this->setMethods(
array_diff(
$this->generator->getClassMethods($this->type),
$this->methodsExcept
)
);
return $this;
}
/**
* Specifies the arguments for the constructor.
*
* @param array $args
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*/
public function setConstructorArgs(array $args)
{
$this->constructorArgs = $args;
return $this;
}
/**
* Specifies the name for the mock class.
*
* @param string $name
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*/
public function setMockClassName($name)
{
$this->mockClassName = $name;
return $this;
}
/**
* Disables the invocation of the original constructor.
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*/
public function disableOriginalConstructor()
{
$this->originalConstructor = false;
return $this;
}
/**
* Enables the invocation of the original constructor.
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*
* @since Method available since Release 1.2.0
*/
public function enableOriginalConstructor()
{
$this->originalConstructor = true;
return $this;
}
/**
* Disables the invocation of the original clone constructor.
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*/
public function disableOriginalClone()
{
$this->originalClone = false;
return $this;
}
/**
* Enables the invocation of the original clone constructor.
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*
* @since Method available since Release 1.2.0
*/
public function enableOriginalClone()
{
$this->originalClone = true;
return $this;
}
/**
* Disables the use of class autoloading while creating the mock object.
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*/
public function disableAutoload()
{
$this->autoload = false;
return $this;
}
/**
* Enables the use of class autoloading while creating the mock object.
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*
* @since Method available since Release 1.2.0
*/
public function enableAutoload()
{
$this->autoload = true;
return $this;
}
/**
* Disables the cloning of arguments passed to mocked methods.
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*
* @since Method available since Release 1.2.0
*/
public function disableArgumentCloning()
{
$this->cloneArguments = false;
return $this;
}
/**
* Enables the cloning of arguments passed to mocked methods.
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*
* @since Method available since Release 1.2.0
*/
public function enableArgumentCloning()
{
$this->cloneArguments = true;
return $this;
}
/**
* Enables the invocation of the original methods.
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*
* @since Method available since Release 2.0.0
*/
public function enableProxyingToOriginalMethods()
{
$this->callOriginalMethods = true;
return $this;
}
/**
* Disables the invocation of the original methods.
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*
* @since Method available since Release 2.0.0
*/
public function disableProxyingToOriginalMethods()
{
$this->callOriginalMethods = false;
$this->proxyTarget = null;
return $this;
}
/**
* Sets the proxy target.
*
* @param object $object
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*
* @since Method available since Release 2.0.0
*/
public function setProxyTarget($object)
{
$this->proxyTarget = $object;
return $this;
}
/**
* @return PHPUnit_Framework_MockObject_MockBuilder
*
* @since Method available since Release 3.2.0
*/
public function allowMockingUnknownTypes()
{
$this->allowMockingUnknownTypes = true;
return $this;
}
/**
* @return PHPUnit_Framework_MockObject_MockBuilder
*
* @since Method available since Release 3.2.0
*/
public function disallowMockingUnknownTypes()
{
$this->allowMockingUnknownTypes = false;
return $this;
}
}

View File

@ -0,0 +1,55 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Interface for all mock objects which are generated by
* PHPUnit_Framework_MockObject_MockBuilder.
*
* @method PHPUnit_Framework_MockObject_Builder_InvocationMocker method($constraint)
*
* @since Interface available since Release 1.0.0
*/
interface PHPUnit_Framework_MockObject_MockObject /*extends PHPUnit_Framework_MockObject_Verifiable*/
{
/**
* Registers a new expectation in the mock object and returns the match
* object which can be infused with further details.
*
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
*
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
*/
public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher);
/**
* @return PHPUnit_Framework_MockObject_InvocationMocker
*
* @since Method available since Release 2.0.0
*/
public function __phpunit_setOriginalObject($originalObject);
/**
* @return PHPUnit_Framework_MockObject_InvocationMocker
*/
public function __phpunit_getInvocationMocker();
/**
* Verifies that the current expectation is valid. If everything is OK the
* code should just return, if not it must throw an exception.
*
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public function __phpunit_verify();
/**
* @return bool
*/
public function __phpunit_hasMatchers();
}

View File

@ -0,0 +1,30 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* An object that stubs the process of a normal method for a mock object.
*
* The stub object will replace the code for the stubbed method and return a
* specific value instead of the original value.
*
* @since Interface available since Release 1.0.0
*/
interface PHPUnit_Framework_MockObject_Stub extends PHPUnit_Framework_SelfDescribing
{
/**
* Fakes the processing of the invocation $invocation by returning a
* specific value.
*
* @param PHPUnit_Framework_MockObject_Invocation $invocation The invocation which was mocked and matched by the current method and argument matchers
*
* @return mixed
*/
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation);
}

View File

@ -0,0 +1,48 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use SebastianBergmann\Exporter\Exporter;
/**
* Stubs a method by returning a user-defined stack of values.
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls implements PHPUnit_Framework_MockObject_Stub
{
protected $stack;
protected $value;
public function __construct($stack)
{
$this->stack = $stack;
}
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$this->value = array_shift($this->stack);
if ($this->value instanceof PHPUnit_Framework_MockObject_Stub) {
$this->value = $this->value->invoke($invocation);
}
return $this->value;
}
public function toString()
{
$exporter = new Exporter;
return sprintf(
'return user-specified value %s',
$exporter->export($this->value)
);
}
}

View File

@ -0,0 +1,48 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use SebastianBergmann\Exporter\Exporter;
/**
* Stubs a method by raising a user-defined exception.
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Stub_Exception implements PHPUnit_Framework_MockObject_Stub
{
protected $exception;
public function __construct($exception)
{
// TODO Replace check with type declaration when support for PHP 5 is dropped
if (!$exception instanceof Throwable && !$exception instanceof Exception) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'Exception must be an instance of Throwable (PHP 7) or Exception (PHP 5)'
);
}
$this->exception = $exception;
}
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
throw $this->exception;
}
public function toString()
{
$exporter = new Exporter;
return sprintf(
'raise user-specified exception %s',
$exporter->export($this->exception)
);
}
}

View File

@ -0,0 +1,25 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Stubs a method by returning a user-defined value.
*
* @since Interface available since Release 1.0.0
*/
interface PHPUnit_Framework_MockObject_Stub_MatcherCollection
{
/**
* Adds a new matcher to the collection which can be used as an expectation
* or a stub.
*
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher Matcher for invocations to mock objects
*/
public function addMatcher(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher);
}

View File

@ -0,0 +1,41 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use SebastianBergmann\Exporter\Exporter;
/**
* Stubs a method by returning a user-defined value.
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Stub_Return implements PHPUnit_Framework_MockObject_Stub
{
protected $value;
public function __construct($value)
{
$this->value = $value;
}
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
return $this->value;
}
public function toString()
{
$exporter = new Exporter;
return sprintf(
'return user-specified value %s',
$exporter->export($this->value)
);
}
}

View File

@ -0,0 +1,38 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Stubs a method by returning an argument that was passed to the mocked method.
*
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Stub_ReturnArgument extends PHPUnit_Framework_MockObject_Stub_Return
{
protected $argumentIndex;
public function __construct($argumentIndex)
{
$this->argumentIndex = $argumentIndex;
}
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
if (isset($invocation->parameters[$this->argumentIndex])) {
return $invocation->parameters[$this->argumentIndex];
} else {
return;
}
}
public function toString()
{
return sprintf('return argument #%d', $this->argumentIndex);
}
}

View File

@ -0,0 +1,51 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @since Class available since Release 1.0.0
*/
class PHPUnit_Framework_MockObject_Stub_ReturnCallback implements PHPUnit_Framework_MockObject_Stub
{
protected $callback;
public function __construct($callback)
{
$this->callback = $callback;
}
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
return call_user_func_array($this->callback, $invocation->parameters);
}
public function toString()
{
if (is_array($this->callback)) {
if (is_object($this->callback[0])) {
$class = get_class($this->callback[0]);
$type = '->';
} else {
$class = $this->callback[0];
$type = '::';
}
return sprintf(
'return result of user defined callback %s%s%s() with the ' .
'passed arguments',
$class,
$type,
$this->callback[1]
);
} else {
return 'return result of user defined callback ' . $this->callback .
' with the passed arguments';
}
}
}

View File

@ -0,0 +1,22 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Stubs a method by returning a user-defined reference to a value.
*
* @since Class available since Release 3.0.7
*/
class PHPUnit_Framework_MockObject_Stub_ReturnReference extends PHPUnit_Framework_MockObject_Stub_Return
{
public function __construct(&$value)
{
$this->value = &$value;
}
}

View File

@ -0,0 +1,34 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Stubs a method by returning the current object.
*
* @since Class available since Release 1.1.0
*/
class PHPUnit_Framework_MockObject_Stub_ReturnSelf implements PHPUnit_Framework_MockObject_Stub
{
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
if (!$invocation instanceof PHPUnit_Framework_MockObject_Invocation_Object) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
'The current object can only be returned when mocking an ' .
'object, not a static class.'
);
}
return $invocation->object;
}
public function toString()
{
return 'return the current object';
}
}

View File

@ -0,0 +1,47 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Stubs a method by returning a value from a map.
*
* @since Class available since Release 1.1.0
*/
class PHPUnit_Framework_MockObject_Stub_ReturnValueMap implements PHPUnit_Framework_MockObject_Stub
{
protected $valueMap;
public function __construct(array $valueMap)
{
$this->valueMap = $valueMap;
}
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$parameterCount = count($invocation->parameters);
foreach ($this->valueMap as $map) {
if (!is_array($map) || $parameterCount != count($map) - 1) {
continue;
}
$return = array_pop($map);
if ($invocation->parameters === $map) {
return $return;
}
}
return;
}
public function toString()
{
return 'return value from a map';
}
}

View File

@ -0,0 +1,25 @@
<?php
/*
* This file is part of the PHPUnit_MockObject package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Interface for classes which must verify a given expectation.
*
* @since Interface available since Release 1.0.0
*/
interface PHPUnit_Framework_MockObject_Verifiable
{
/**
* Verifies that the current expectation is valid. If everything is OK the
* code should just return, if not it must throw an exception.
*
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public function verify();
}