Skip to content

Commit

Permalink
Add tests for id() and after()
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jun 20, 2023
1 parent 8cfe630 commit 2856222
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 41 deletions.
Expand Up @@ -9,13 +9,9 @@
*/
namespace PHPUnit\TestFixture\MockObject;

class ClassWithImplicitProtocol
interface InterfaceWithImplicitProtocol
{
public function firstCall(): void
{
}
public function one(): void;

public function secondCall(): void
{
}
public function two(): void;
}
34 changes: 0 additions & 34 deletions tests/unit/Framework/MockObject/Builder/InvocationMockerTest.php
Expand Up @@ -20,7 +20,6 @@
use PHPUnit\Framework\TestCase;
use PHPUnit\TestFixture\Foo;
use PHPUnit\TestFixture\MockObject\ClassWithAllPossibleReturnTypes;
use PHPUnit\TestFixture\MockObject\ClassWithImplicitProtocol;
use stdClass;

#[CoversClass(InvocationMocker::class)]
Expand Down Expand Up @@ -195,39 +194,6 @@ public function testWillReturnFailsWhenTryingToReturnValueFromVoidMethod(): void
$method->willReturn(true);
}

public function testExpectationsAreEnabledByPreviousMethodCallWhenChainedWithAfter(): void
{
$mock = $this->createMock(ClassWithImplicitProtocol::class);

$mock->expects($this->once())
->method('firstCall')
->id($fristCallId = 'first-call-id');

$mock->expects($this->once())
->method('secondCall')
->after($fristCallId);

$mock->firstCall();
$mock->secondCall();
}

public function testExpectationsAreNotTriggeredUntilPreviousMethodWasCalled(): void
{
$mock = $this->createMock(ClassWithImplicitProtocol::class);

$mock->expects($this->once())
->method('firstCall')
->id($firstCallId = 'first-call-id');

$mock->expects($this->once())
->method('secondCall')
->after($firstCallId);

$mock->secondCall();
$mock->firstCall();
$mock->secondCall();
}

public function testWillReturnAlreadyInstantiatedStubs(): void
{
$mock = $this->getMockBuilder(stdClass::class)
Expand Down
59 changes: 59 additions & 0 deletions tests/unit/Framework/MockObject/Interface/MockObjectTest.php
Expand Up @@ -17,6 +17,7 @@
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use PHPUnit\TestFixture\MockObject\AnInterface;
use PHPUnit\TestFixture\MockObject\InterfaceWithImplicitProtocol;
use PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration;
use ReflectionProperty;

Expand Down Expand Up @@ -322,6 +323,48 @@ public function testExpectationThatMethodIsCalledWithParameterFailsWhenMethodIsC
);
}

/**
* With <code>$mock->expects($this->once())->method('one')->id($id);</code>,
* we configure an expectation that one() is called once. This expectation is given the ID $id.
*
* With <code>$mock->expects($this->once())->method('two')->after($id);</code>,
* we configure an expectation that two() is called once. However, this expectation will only be verified
* if/after one() has been called.
*/
public function testMethodCallCanBeExpectedContingentOnWhetherAnotherMethodWasPreviouslyCalled(): void
{
$mock = $this->mockWithContingentExpectation();

$mock->one();
$mock->two();
}

public function testContingentExpectationsAreNotEvaluatedUntilTheirConditionIsMet(): void
{
$mock = $this->mockWithContingentExpectation();

$mock->two();
$mock->one();
$mock->two();
}

public function testContingentExpectationsAreEvaluatedWhenTheirConditionIsMet(): void
{
$mock = $this->mockWithContingentExpectation();

$mock->two();
$mock->one();

$this->assertThatMockObjectExpectationFails(
<<<'EOT'
Expectation failed for method name is "two" when invoked 1 time.
Method was expected to be called 1 time, actually called 0 times.
EOT,
$mock,
);
}

/**
* @psalm-param class-string $type
*/
Expand All @@ -338,6 +381,22 @@ protected function createTestDoubleForIntersection(array $interfaces): object
return $this->createMockForIntersectionOfInterfaces($interfaces);
}

private function mockWithContingentExpectation(): InterfaceWithImplicitProtocol&MockObject
{
$id = 'the-id';
$mock = $this->createMock(InterfaceWithImplicitProtocol::class);

$mock->expects($this->once())
->method('one')
->id($id);

$mock->expects($this->once())
->method('two')
->after($id);

return $mock;
}

private function assertThatMockObjectExpectationFails(string $expectationFailureMessage, MockObject $mock, string $methodName = '__phpunit_verify', array $arguments = []): void
{
try {
Expand Down

0 comments on commit 2856222

Please sign in to comment.