Skip to content

Commit

Permalink
Fix atMost(...) invocation rule throws error on zero invocation
Browse files Browse the repository at this point in the history
Closes #4950
  • Loading branch information
T-bond authored and sebastianbergmann committed Apr 11, 2022
1 parent eef408a commit 50ffe68
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Framework/MockObject/Matcher.php
Expand Up @@ -17,6 +17,7 @@
use PHPUnit\Framework\MockObject\Rule\AnyInvokedCount;
use PHPUnit\Framework\MockObject\Rule\AnyParameters;
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
use PHPUnit\Framework\MockObject\Rule\InvokedAtMostCount;
use PHPUnit\Framework\MockObject\Rule\InvokedCount;
use PHPUnit\Framework\MockObject\Rule\MethodName;
use PHPUnit\Framework\MockObject\Rule\ParametersRule;
Expand Down Expand Up @@ -231,10 +232,11 @@ public function verify(): void
$this->parametersRule = new AnyParameters;
}

$invocationIsAny = $this->invocationRule instanceof AnyInvokedCount;
$invocationIsNever = $this->invocationRule instanceof InvokedCount && $this->invocationRule->isNever();
$invocationIsAny = $this->invocationRule instanceof AnyInvokedCount;
$invocationIsNever = $this->invocationRule instanceof InvokedCount && $this->invocationRule->isNever();
$invocationIsAtMost = $this->invocationRule instanceof InvokedAtMostCount;

if (!$invocationIsAny && !$invocationIsNever) {
if (!$invocationIsAny && !$invocationIsNever && !$invocationIsAtMost) {
$this->parametersRule->verify();
}
} catch (ExpectationFailedException $e) {
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/Framework/MockObject/MatcherTest.php
Expand Up @@ -117,4 +117,22 @@ public function testStubIsInvokedIfAllMatchersAndRulesApply(): void

$matcher->invoked($invocation);
}

public function testAtMostNotFailsWithoutInvocation(): void
{
$invocationMatcher = $this->createStub(InvocationOrder::class);
$invocation = new Invocation('Foo', 'bar', [], 'void', new stdClass);

$stub = $this->createMock(Stub\Stub::class);
$stub->expects($this->atMost(1))
->method('invoke')
->with($invocation);

$parameterRule = $this->createStub(ParametersRule::class);

$matcher = new Matcher($invocationMatcher);
$matcher->setMethodNameRule(new MethodName('bar'));
$matcher->setParametersRule($parameterRule);
$matcher->setStub($stub);
}
}

0 comments on commit 50ffe68

Please sign in to comment.