diff --git a/src/Framework/MockObject/Matcher.php b/src/Framework/MockObject/Matcher.php index 2b1cd8c86cf..375a35cdd2a 100644 --- a/src/Framework/MockObject/Matcher.php +++ b/src/Framework/MockObject/Matcher.php @@ -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; @@ -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) { diff --git a/tests/unit/Framework/MockObject/MatcherTest.php b/tests/unit/Framework/MockObject/MatcherTest.php index 57e943260a6..3a8914f2910 100644 --- a/tests/unit/Framework/MockObject/MatcherTest.php +++ b/tests/unit/Framework/MockObject/MatcherTest.php @@ -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); + } }