This commit is contained in:
2020-10-06 14:27:47 +07:00
commit 586be80cf6
16613 changed files with 3274099 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
<?php
/*
* This file is part of the phpunit-mock-objects 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.
*/
namespace PHPUnit\Framework\MockObject\Builder;
/**
* 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().
*/
interface 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,299 @@
<?php
/*
* This file is part of the phpunit-mock-objects 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.
*/
namespace PHPUnit\Framework\MockObject\Builder;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\MockObject\Matcher;
use PHPUnit\Framework\MockObject\Matcher\Invocation;
use PHPUnit\Framework\MockObject\RuntimeException;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\MockObject\Stub\MatcherCollection;
/**
* 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.
*/
class InvocationMocker implements MethodNameMatch
{
/**
* @var MatcherCollection
*/
private $collection;
/**
* @var Matcher
*/
private $matcher;
/**
* @var string[]
*/
private $configurableMethods = [];
/**
* @param MatcherCollection $collection
* @param Invocation $invocationMatcher
* @param array $configurableMethods
*/
public function __construct(MatcherCollection $collection, Invocation $invocationMatcher, array $configurableMethods)
{
$this->collection = $collection;
$this->matcher = new Matcher($invocationMatcher);
$this->collection->addMatcher($this->matcher);
$this->configurableMethods = $configurableMethods;
}
/**
* @return Matcher
*/
public function getMatcher()
{
return $this->matcher;
}
/**
* @param mixed $id
*
* @return InvocationMocker
*/
public function id($id)
{
$this->collection->registerId($id, $this);
return $this;
}
/**
* @param Stub $stub
*
* @return InvocationMocker
*/
public function will(Stub $stub)
{
$this->matcher->setStub($stub);
return $this;
}
/**
* @param mixed $value
* @param mixed $nextValues, ...
*
* @return InvocationMocker
*/
public function willReturn($value, ...$nextValues)
{
if (\count($nextValues) === 0) {
$stub = new Stub\ReturnStub($value);
} else {
$stub = new Stub\ConsecutiveCalls(
\array_merge([$value], $nextValues)
);
}
return $this->will($stub);
}
/**
* @param mixed $reference
*
* @return InvocationMocker
*/
public function willReturnReference(&$reference)
{
$stub = new Stub\ReturnReference($reference);
return $this->will($stub);
}
/**
* @param array $valueMap
*
* @return InvocationMocker
*/
public function willReturnMap(array $valueMap)
{
$stub = new Stub\ReturnValueMap($valueMap);
return $this->will($stub);
}
/**
* @param mixed $argumentIndex
*
* @return InvocationMocker
*/
public function willReturnArgument($argumentIndex)
{
$stub = new Stub\ReturnArgument($argumentIndex);
return $this->will($stub);
}
/**
* @param callable $callback
*
* @return InvocationMocker
*/
public function willReturnCallback($callback)
{
$stub = new Stub\ReturnCallback($callback);
return $this->will($stub);
}
/**
* @return InvocationMocker
*/
public function willReturnSelf()
{
$stub = new Stub\ReturnSelf;
return $this->will($stub);
}
/**
* @param mixed $values, ...
*
* @return InvocationMocker
*/
public function willReturnOnConsecutiveCalls(...$values)
{
$stub = new Stub\ConsecutiveCalls($values);
return $this->will($stub);
}
/**
* @param \Exception $exception
*
* @return InvocationMocker
*/
public function willThrowException(\Exception $exception)
{
$stub = new Stub\Exception($exception);
return $this->will($stub);
}
/**
* @param mixed $id
*
* @return InvocationMocker
*/
public function after($id)
{
$this->matcher->setAfterMatchBuilderId($id);
return $this;
}
/**
* @param array ...$arguments
*
* @return InvocationMocker
*
* @throws RuntimeException
*/
public function with(...$arguments)
{
$this->canDefineParameters();
$this->matcher->setParametersMatcher(new Matcher\Parameters($arguments));
return $this;
}
/**
* @param array ...$arguments
*
* @return InvocationMocker
*
* @throws RuntimeException
*/
public function withConsecutive(...$arguments)
{
$this->canDefineParameters();
$this->matcher->setParametersMatcher(new Matcher\ConsecutiveParameters($arguments));
return $this;
}
/**
* @return InvocationMocker
*
* @throws RuntimeException
*/
public function withAnyParameters()
{
$this->canDefineParameters();
$this->matcher->setParametersMatcher(new Matcher\AnyParameters);
return $this;
}
/**
* @param Constraint|string $constraint
*
* @return InvocationMocker
*
* @throws RuntimeException
*/
public function method($constraint)
{
if ($this->matcher->hasMethodNameMatcher()) {
throw new RuntimeException(
'Method name matcher is already defined, cannot redefine'
);
}
if (\is_string($constraint) && !\in_array(\strtolower($constraint), $this->configurableMethods)) {
throw new 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->setMethodNameMatcher(new Matcher\MethodName($constraint));
return $this;
}
/**
* Validate that a parameters matcher can be defined, throw exceptions otherwise.
*
* @throws RuntimeException
*/
private function canDefineParameters()
{
if (!$this->matcher->hasMethodNameMatcher()) {
throw new RuntimeException(
'Method name matcher is not defined, cannot define parameter ' .
'matcher without one'
);
}
if ($this->matcher->hasParametersMatcher()) {
throw new RuntimeException(
'Parameter matcher is already defined, cannot redefine'
);
}
}
}

View File

@@ -0,0 +1,26 @@
<?php
/*
* This file is part of the phpunit-mock-objects 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.
*/
namespace PHPUnit\Framework\MockObject\Builder;
/**
* Builder interface for invocation order matches.
*/
interface Match extends 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 Stub
*/
public function after($id);
}

View File

@@ -0,0 +1,26 @@
<?php
/*
* This file is part of the phpunit-mock-objects 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.
*/
namespace PHPUnit\Framework\MockObject\Builder;
/**
* Builder interface for matcher of method names.
*/
interface MethodNameMatch extends ParametersMatch
{
/**
* Adds a new method name match and returns the parameter match object for
* further matching possibilities.
*
* @param \PHPUnit\Framework\Constraint\Constraint $name Constraint for matching method, if a string is passed it will use the PHPUnit_Framework_Constraint_IsEqual
*
* @return ParametersMatch
*/
public function method($name);
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the phpunit-mock-objects 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.
*/
namespace PHPUnit\Framework\MockObject\Builder;
/**
* Interface for builders which can register builders with a given identification.
*
* This interface relates to Identity.
*/
interface NamespaceMatch
{
/**
* Looks up the match builder with identification $id and returns it.
*
* @param string $id The identification of the match builder
*
* @return 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 Match $builder The builder which is being registered
*/
public function registerId($id, Match $builder);
}

View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the phpunit-mock-objects 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.
*/
namespace PHPUnit\Framework\MockObject\Builder;
use PHPUnit\Framework\MockObject\Matcher\AnyParameters;
/**
* Builder interface for parameter matchers.
*/
interface ParametersMatch extends Match
{
/**
* Sets the parameters to match for, each parameter to this function will
* be part of match. To perform specific matches or constraints create a
* new PHPUnit\Framework\Constraint\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 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 AnyParameters
*/
public function withAnyParameters();
}

View File

@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the phpunit-mock-objects 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.
*/
namespace PHPUnit\Framework\MockObject\Builder;
use PHPUnit\Framework\MockObject\Stub as BaseStub;
/**
* Builder interface for stubs which are actions replacing an invocation.
*/
interface Stub extends 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 BaseStub $stub
*
* @return Identity
*/
public function will(BaseStub $stub);
}