Skip to content

Commit

Permalink
Revert "Treat InvokedCount like any other mock object expectation and…
Browse files Browse the repository at this point in the history
… only verify it after the test method has finished"

This reverts commit ec59ed6.
  • Loading branch information
sebastianbergmann committed Jun 19, 2023
1 parent 45ffdd1 commit 14303e3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Framework/MockObject/Rule/InvocationOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
}
23 changes: 23 additions & 0 deletions src/Framework/MockObject/Rule/InvokedCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
52 changes: 52 additions & 0 deletions tests/unit/Framework/MockObject/OldTestDoubleTestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 14303e3

Please sign in to comment.