Skip to content

Commit

Permalink
Refactored runtime code for mocks.
Browse files Browse the repository at this point in the history
* The old "Matchers" are separated into three responsibilities:
** method invocation
** parameters
** method name
* Some interfaces that were used only by a single class are removed.
* Renamed some concepts to better reflect what they are doing
  • Loading branch information
MichelHartmann authored and sebastianbergmann committed Sep 7, 2019
1 parent 5955110 commit 7f664ff
Show file tree
Hide file tree
Showing 96 changed files with 576 additions and 524 deletions.
12 changes: 6 additions & 6 deletions src/Framework/Assert/Functions.php
Expand Up @@ -49,12 +49,12 @@
use PHPUnit\Framework\Constraint\TraversableContains;
use PHPUnit\Framework\Constraint\TraversableContainsOnly;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\MockObject\Matcher\AnyInvokedCount as AnyInvokedCountMatcher;
use PHPUnit\Framework\MockObject\Matcher\InvokedAtIndex as InvokedAtIndexMatcher;
use PHPUnit\Framework\MockObject\Matcher\InvokedAtLeastCount as InvokedAtLeastCountMatcher;
use PHPUnit\Framework\MockObject\Matcher\InvokedAtLeastOnce as InvokedAtLeastOnceMatcher;
use PHPUnit\Framework\MockObject\Matcher\InvokedAtMostCount as InvokedAtMostCountMatcher;
use PHPUnit\Framework\MockObject\Matcher\InvokedCount as InvokedCountMatcher;
use PHPUnit\Framework\MockObject\Rule\AnyInvokedCount as AnyInvokedCountMatcher;
use PHPUnit\Framework\MockObject\Rule\InvokedAtIndex as InvokedAtIndexMatcher;
use PHPUnit\Framework\MockObject\Rule\InvokedAtLeastCount as InvokedAtLeastCountMatcher;
use PHPUnit\Framework\MockObject\Rule\InvokedAtLeastOnce as InvokedAtLeastOnceMatcher;
use PHPUnit\Framework\MockObject\Rule\InvokedAtMostCount as InvokedAtMostCountMatcher;
use PHPUnit\Framework\MockObject\Rule\InvokedCount as InvokedCountMatcher;
use PHPUnit\Framework\MockObject\Stub\ConsecutiveCalls as ConsecutiveCallsStub;
use PHPUnit\Framework\MockObject\Stub\Exception as ExceptionStub;
use PHPUnit\Framework\MockObject\Stub\ReturnArgument as ReturnArgumentStub;
Expand Down
12 changes: 6 additions & 6 deletions src/Framework/MockObject/Api/Api.php
Expand Up @@ -10,7 +10,7 @@
namespace PHPUnit\Framework\MockObject;

use PHPUnit\Framework\MockObject\Builder\InvocationMocker as InvocationMockerBuilder;
use PHPUnit\Framework\MockObject\Matcher\Invocation;
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;

/**
* @internal This trait is not covered by the backward compatibility promise for PHPUnit
Expand Down Expand Up @@ -62,7 +62,7 @@ public function __phpunit_setReturnValueGeneration(bool $returnValueGeneration):
}

/** @noinspection MagicMethodsValidityInspection */
public function __phpunit_getInvocationMocker(): InvocationHandler
public function __phpunit_getInvocationHandler(): InvocationHandler
{
if ($this->__phpunit_invocationMocker === null) {
$this->__phpunit_invocationMocker = new InvocationHandler(
Expand All @@ -77,21 +77,21 @@ public function __phpunit_getInvocationMocker(): InvocationHandler
/** @noinspection MagicMethodsValidityInspection */
public function __phpunit_hasMatchers(): bool
{
return $this->__phpunit_getInvocationMocker()->hasMatchers();
return $this->__phpunit_getInvocationHandler()->hasMatchers();
}

/** @noinspection MagicMethodsValidityInspection */
public function __phpunit_verify(bool $unsetInvocationMocker = true): void
{
$this->__phpunit_getInvocationMocker()->verify();
$this->__phpunit_getInvocationHandler()->verify();

if ($unsetInvocationMocker) {
$this->__phpunit_invocationMocker = null;
}
}

public function expects(Invocation $matcher): InvocationMockerBuilder
public function expects(InvocationOrder $matcher): InvocationMockerBuilder
{
return $this->__phpunit_getInvocationMocker()->expects($matcher);
return $this->__phpunit_getInvocationHandler()->expects($matcher);
}
}
2 changes: 1 addition & 1 deletion src/Framework/MockObject/Api/Method.php
Expand Up @@ -9,7 +9,7 @@
*/
namespace PHPUnit\Framework\MockObject;

use PHPUnit\Framework\MockObject\Matcher\AnyInvokedCount;
use PHPUnit\Framework\MockObject\Rule\AnyInvokedCount;

/**
* @internal This trait is not covered by the backward compatibility promise for PHPUnit
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/MockObject/Api/MockedCloneMethod.php
Expand Up @@ -16,6 +16,6 @@ trait MockedCloneMethod
{
public function __clone()
{
$this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationMocker();
$this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationHandler();
}
}
2 changes: 1 addition & 1 deletion src/Framework/MockObject/Api/UnmockedCloneMethod.php
Expand Up @@ -16,7 +16,7 @@ trait UnmockedCloneMethod
{
public function __clone()
{
$this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationMocker();
$this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationHandler();

parent::__clone();
}
Expand Down
50 changes: 21 additions & 29 deletions src/Framework/MockObject/Builder/InvocationMocker.php
Expand Up @@ -12,12 +12,12 @@
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\MockObject\ConfigurableMethod;
use PHPUnit\Framework\MockObject\IncompatibleReturnValueException;
use PHPUnit\Framework\MockObject\InvocationHandler;
use PHPUnit\Framework\MockObject\Matcher;
use PHPUnit\Framework\MockObject\Matcher\Invocation;
use PHPUnit\Framework\MockObject\Rule;
use PHPUnit\Framework\MockObject\RuntimeException;
use PHPUnit\Framework\MockObject\Stub\ConsecutiveCalls;
use PHPUnit\Framework\MockObject\Stub\Exception;
use PHPUnit\Framework\MockObject\Stub\MatcherCollection;
use PHPUnit\Framework\MockObject\Stub\ReturnArgument;
use PHPUnit\Framework\MockObject\Stub\ReturnCallback;
use PHPUnit\Framework\MockObject\Stub\ReturnReference;
Expand All @@ -32,9 +32,9 @@
final class InvocationMocker implements InvocationStubber, MethodNameMatch
{
/**
* @var MatcherCollection
* @var InvocationHandler
*/
private $collection;
private $invocationHandler;

/**
* @var Matcher
Expand All @@ -46,24 +46,16 @@ final class InvocationMocker implements InvocationStubber, MethodNameMatch
*/
private $configurableMethods;

public function __construct(MatcherCollection $collection, Invocation $invocationMatcher, ConfigurableMethod ...$configurableMethods)
public function __construct(InvocationHandler $handler, Matcher $matcher, ConfigurableMethod ...$configurableMethods)
{
$this->collection = $collection;
$this->matcher = new Matcher($invocationMatcher);

$this->collection->addMatcher($this->matcher);

$this->invocationHandler = $handler;
$this->matcher = $matcher;
$this->configurableMethods = $configurableMethods;
}

public function getMatcher(): Matcher
{
return $this->matcher;
}

public function id($id): self
{
$this->collection->registerId($id, $this);
$this->invocationHandler->registerMatcher($id, $this->matcher);

return $this;
}
Expand Down Expand Up @@ -157,7 +149,7 @@ public function with(...$arguments): self
{
$this->canDefineParameters();

$this->matcher->setParametersMatcher(new Matcher\Parameters($arguments));
$this->matcher->setParametersRule(new Rule\Parameters($arguments));

return $this;
}
Expand All @@ -171,7 +163,7 @@ public function withConsecutive(...$arguments): self
{
$this->canDefineParameters();

$this->matcher->setParametersMatcher(new Matcher\ConsecutiveParameters($arguments));
$this->matcher->setParametersRule(new Rule\ConsecutiveParameters($arguments));

return $this;
}
Expand All @@ -183,7 +175,7 @@ public function withAnyParameters(): self
{
$this->canDefineParameters();

$this->matcher->setParametersMatcher(new Matcher\AnyParameters);
$this->matcher->setParametersRule(new Rule\AnyParameters);

return $this;
}
Expand All @@ -195,9 +187,9 @@ public function withAnyParameters(): self
*/
public function method($constraint): self
{
if ($this->matcher->hasMethodNameMatcher()) {
if ($this->matcher->hasMethodNameRule()) {
throw new RuntimeException(
'Method name matcher is already defined, cannot redefine'
'Rule for method name is already defined, cannot redefine'
);
}

Expand All @@ -217,28 +209,28 @@ static function (ConfigurableMethod $configurable) {
);
}

$this->matcher->setMethodNameMatcher(new Matcher\MethodName($constraint));
$this->matcher->setMethodNameRule(new Rule\MethodName($constraint));

return $this;
}

/**
* Validate that a parameters matcher can be defined, throw exceptions otherwise.
* Validate that a parameters rule can be defined, throw exceptions otherwise.
*
* @throws RuntimeException
*/
private function canDefineParameters(): void
{
if (!$this->matcher->hasMethodNameMatcher()) {
if (!$this->matcher->hasMethodNameRule()) {
throw new RuntimeException(
'Method name matcher is not defined, cannot define parameter ' .
'matcher without one'
'Rule for method name is not defined, cannot define rule for parameters ' .
'without one'
);
}

if ($this->matcher->hasParametersMatcher()) {
if ($this->matcher->hasParametersRule()) {
throw new RuntimeException(
'Parameter matcher is already defined, cannot redefine'
'Rule for parameters is already defined, cannot redefine'
);
}
}
Expand All @@ -248,7 +240,7 @@ private function getConfiguredMethod(): ?ConfigurableMethod
$configuredMethod = null;

foreach ($this->configurableMethods as $configurableMethod) {
if ($this->matcher->getMethodNameMatcher()->matchesName($configurableMethod->getName())) {
if ($this->matcher->getMethodNameRule()->matchesName($configurableMethod->getName())) {
if ($configuredMethod !== null) {
return null;
}
Expand Down
35 changes: 0 additions & 35 deletions src/Framework/MockObject/Builder/NamespaceMatch.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Framework/MockObject/Builder/ParametersMatch.php
Expand Up @@ -9,7 +9,7 @@
*/
namespace PHPUnit\Framework\MockObject\Builder;

use PHPUnit\Framework\MockObject\Matcher\AnyParameters;
use PHPUnit\Framework\MockObject\Rule\AnyParameters;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
Expand All @@ -36,7 +36,7 @@ interface ParametersMatch extends Match
public function with(...$arguments);

/**
* Sets a matcher which allows any kind of parameters.
* Sets a rule which allows any kind of parameters.
*
* Some examples:
* <code>
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/MockObject/Generator/mocked_method.tpl
Expand Up @@ -12,7 +12,7 @@
}
}

$__phpunit_result = $this->__phpunit_getInvocationMocker()->invoke(
$__phpunit_result = $this->__phpunit_getInvocationHandler()->invoke(
new \PHPUnit\Framework\MockObject\Invocation(
'{class_name}', '{method_name}', $__phpunit_arguments, '{return_declaration}', $this, {clone_arguments}
)
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/MockObject/Generator/mocked_method_void.tpl
Expand Up @@ -12,7 +12,7 @@
}
}

$this->__phpunit_getInvocationMocker()->invoke(
$this->__phpunit_getInvocationHandler()->invoke(
new \PHPUnit\Framework\MockObject\Invocation(
'{class_name}', '{method_name}', $__phpunit_arguments, '{return_declaration}', $this, {clone_arguments}
)
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/MockObject/Generator/proxied_method.tpl
Expand Up @@ -12,7 +12,7 @@
}
}

$this->__phpunit_getInvocationMocker()->invoke(
$this->__phpunit_getInvocationHandler()->invoke(
new \PHPUnit\Framework\MockObject\Invocation(
'{class_name}', '{method_name}', $__phpunit_arguments, '{return_declaration}', $this, {clone_arguments}, true
)
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/MockObject/Generator/proxied_method_void.tpl
Expand Up @@ -12,7 +12,7 @@
}
}

$this->__phpunit_getInvocationMocker()->invoke(
$this->__phpunit_getInvocationHandler()->invoke(
new \PHPUnit\Framework\MockObject\Invocation(
'{class_name}', '{method_name}', $__phpunit_arguments, '{return_declaration}', $this, {clone_arguments}, true
)
Expand Down

0 comments on commit 7f664ff

Please sign in to comment.