From 14303e37714ff16c92c95f7a04185c495d151335 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Mon, 19 Jun 2023 13:48:48 +0200 Subject: [PATCH] Revert "Treat InvokedCount like any other mock object expectation and only verify it after the test method has finished" This reverts commit ec59ed611aafc7b7cedbc534adea0886b2893857. --- .../MockObject/Rule/InvocationOrder.php | 6 +++ .../MockObject/Rule/InvokedCount.php | 23 ++++++++ .../MockObject/OldTestDoubleTestsTest.php | 52 +++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/src/Framework/MockObject/Rule/InvocationOrder.php b/src/Framework/MockObject/Rule/InvocationOrder.php index 7209433af86..622922889a2 100644 --- a/src/Framework/MockObject/Rule/InvocationOrder.php +++ b/src/Framework/MockObject/Rule/InvocationOrder.php @@ -37,7 +37,13 @@ public function hasBeenInvoked(): bool final public function invoked(BaseInvocation $invocation): void { $this->invocations[] = $invocation; + + $this->invokedDo($invocation); } abstract public function matches(BaseInvocation $invocation): bool; + + protected function invokedDo(BaseInvocation $invocation): void + { + } } diff --git a/src/Framework/MockObject/Rule/InvokedCount.php b/src/Framework/MockObject/Rule/InvokedCount.php index c307518c0fd..7ccc5105cda 100644 --- a/src/Framework/MockObject/Rule/InvokedCount.php +++ b/src/Framework/MockObject/Rule/InvokedCount.php @@ -61,4 +61,27 @@ public function verify(): void ); } } + + /** + * @throws ExpectationFailedException + */ + protected function invokedDo(BaseInvocation $invocation): void + { + $count = $this->numberOfInvocations(); + + if ($count > $this->expectedCount) { + $message = $invocation->toString() . ' '; + + $message .= match ($this->expectedCount) { + 0 => 'was not expected to be called.', + 1 => 'was not expected to be called more than once.', + default => sprintf( + 'was not expected to be called more than %d times.', + $this->expectedCount, + ), + }; + + throw new ExpectationFailedException($message); + } + } } diff --git a/tests/unit/Framework/MockObject/OldTestDoubleTestsTest.php b/tests/unit/Framework/MockObject/OldTestDoubleTestsTest.php index 1e3e365062e..915a17591ef 100644 --- a/tests/unit/Framework/MockObject/OldTestDoubleTestsTest.php +++ b/tests/unit/Framework/MockObject/OldTestDoubleTestsTest.php @@ -759,6 +759,58 @@ public function testVerificationOfMethodNameFailsWithWrongParameters(): void $this->resetMockObjects(); } + public function testVerificationOfNeverFailsWithEmptyParameters(): void + { + $mock = $this->getMockBuilder(SomeClass::class) + ->addMethods(['right', 'wrong']) + ->getMock(); + + $mock->expects($this->never()) + ->method('right') + ->with(); + + try { + $mock->right(); + $this->fail('Expected exception'); + } catch (ExpectationFailedException $e) { + $this->assertSame( + sprintf( + '%s::right() was not expected to be called.', + SomeClass::class, + ), + $e->getMessage(), + ); + } + + $this->resetMockObjects(); + } + + public function testVerificationOfNeverFailsWithAnyParameters(): void + { + $mock = $this->getMockBuilder(SomeClass::class) + ->addMethods(['right', 'wrong']) + ->getMock(); + + $mock->expects($this->never()) + ->method('right') + ->withAnyParameters(); + + try { + $mock->right(); + $this->fail('Expected exception'); + } catch (ExpectationFailedException $e) { + $this->assertSame( + sprintf( + '%s::right() was not expected to be called.', + SomeClass::class, + ), + $e->getMessage(), + ); + } + + $this->resetMockObjects(); + } + public function testWithAnythingInsteadOfWithAnyParameters(): void { $mock = $this->getMockBuilder(SomeClass::class)