Skip to content

Commit

Permalink
Merge pull request #656 from tienvx/wrap-ffi-call-setup-message-inter…
Browse files Browse the repository at this point in the history
…action-methods

refactor: Wrap ffi call > setup message interaction methods
  • Loading branch information
tienvx authored Sep 27, 2024
2 parents ff3949b + 55051a1 commit 35ddcde
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 79 deletions.
56 changes: 56 additions & 0 deletions helper/FFI/ClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,60 @@ protected function expectsUponReceiving(int $interaction, string $description, b
$this->expectExceptionMessage("The interaction or Pact can't be modified (i.e. the mock server for it has already started)");
}
}

protected function expectsMessageExpectsToReceive(int $message, string $description): void
{
$this->client
->expects($this->once())
->method('messageExpectsToReceive')
->with($message, $description);
}

/**
* @param array<string, string> $metadata
*/
protected function expectsMessageWithMetadataV2(int $message, array $metadata): void
{
$calls = [];
foreach ($metadata as $key => $value) {
$calls[] = [$message, $key, $value];
}
$this->client
->expects($this->exactly(count($calls)))
->method('messageWithMetadataV2')
->willReturnCallback(function (...$args) use (&$calls) {
$call = array_shift($calls);
foreach ($args as $key => $arg) {
$this->assertThat($arg, $call[$key] instanceof Constraint ? $call[$key] : new IsIdentical($call[$key]));
}
});
}

protected function expectsMessageGiven(int $message, string $name): void
{
$this->client
->expects($this->once())
->method('messageGiven')
->with($message, $name);
}

/**
* @param array<string, string> $params
*/
protected function expectsMessageGivenWithParam(int $message, string $name, array $params): void
{
$calls = [];
foreach ($params as $key => $value) {
$calls[] = [$message, $name, $key, $value];
}
$this->client
->expects($this->exactly(count($calls)))
->method('messageGivenWithParam')
->willReturnCallback(function (...$args) use (&$calls) {
$call = array_shift($calls);
foreach ($args as $key => $arg) {
$this->assertThat($arg, $call[$key] instanceof Constraint ? $call[$key] : new IsIdentical($call[$key]));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ private function withContents(Message $message): void

private function expectsToReceive(Message $message): void
{
$this->client->call('pactffi_message_expects_to_receive', $message->getHandle(), $message->getDescription());
$this->client->messageExpectsToReceive($message->getHandle(), $message->getDescription());
}

protected function given(Message $message): void
{
foreach ($message->getProviderStates() as $providerState) {
$this->client->call('pactffi_message_given', $message->getHandle(), $providerState->getName());
$this->client->messageGiven($message->getHandle(), $providerState->getName());
foreach ($providerState->getParams() as $key => $value) {
$this->client->call('pactffi_message_given_with_param', $message->getHandle(), $providerState->getName(), (string) $key, (string) $value);
$this->client->messageGivenWithParam($message->getHandle(), $providerState->getName(), (string) $key, (string) $value);
}
}
}

private function withMetadata(Message $message): void
{
foreach ($message->getMetadata() as $key => $value) {
$this->client->call('pactffi_message_with_metadata_v2', $message->getHandle(), (string) $key, (string) $value);
$this->client->messageWithMetadataV2($message->getHandle(), (string) $key, (string) $value);
}
}
}
24 changes: 24 additions & 0 deletions src/PhpPact/FFI/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,30 @@ public function uponReceiving(int $interaction, string $description): bool
return $result;
}

public function messageExpectsToReceive(int $message, string $description): void
{
$method = 'pactffi_message_expects_to_receive';
$this->call($method, $message, $description);
}

public function messageWithMetadataV2(int $message, string $key, string $value): void
{
$method = 'pactffi_message_with_metadata_v2';
$this->call($method, $message, $key, $value);
}

public function messageGiven(int $message, string $name): void
{
$method = 'pactffi_message_given';
$this->call($method, $message, $name);
}

public function messageGivenWithParam(int $message, string $name, string $key, string $value): void
{
$method = 'pactffi_message_given_with_param';
$this->call($method, $message, $name, $key, $value);
}

public function getInteractionPartRequest(): int
{
return $this->getEnum('InteractionPart_Request');
Expand Down
8 changes: 8 additions & 0 deletions src/PhpPact/FFI/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public function givenWithParam(int $interaction, string $name, string $key, stri

public function uponReceiving(int $interaction, string $description): bool;

public function messageExpectsToReceive(int $message, string $description): void;

public function messageWithMetadataV2(int $message, string $key, string $value): void;

public function messageGiven(int $message, string $name): void;

public function messageGivenWithParam(int $message, string $name, string $key, string $value): void;

public function getInteractionPartRequest(): int;

public function getInteractionPartResponse(): int;
Expand Down
80 changes: 35 additions & 45 deletions tests/PhpPact/Consumer/Driver/Interaction/MessageDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,13 @@ public function testRegisterInteraction(): void
->method('registerBody')
->with($this->message);
$this->expectsNewMessageInteraction($this->pactHandle, $this->description, $this->messageHandle);
$calls = [
['pactffi_message_given', $this->messageHandle, 'item exist', null],
['pactffi_message_given_with_param', $this->messageHandle, 'item exist', 'id', '12', null],
['pactffi_message_given_with_param', $this->messageHandle, 'item exist', 'name', 'abc', null],
['pactffi_message_expects_to_receive', $this->messageHandle, $this->description, null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key1', 'value1', null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key2', 'value2', null],
];
$this->assertClientCalls($calls);
$this->expectsMessageGiven($this->messageHandle, 'item exist');
$this->expectsMessageGivenWithParam($this->messageHandle, 'item exist', [
'id' => '12',
'name' => 'abc',
]);
$this->expectsMessageExpectsToReceive($this->messageHandle, $this->description);
$this->expectsMessageWithMetadataV2($this->messageHandle, $this->metadata);
$this->driver->registerMessage($this->message);
$this->assertSame($this->messageHandle, $this->message->getHandle());
}
Expand All @@ -118,16 +116,14 @@ public function testSetKey(?string $key, bool $success): void
->method('getPact')
->willReturn(new Pact($this->pactHandle));
$this->expectsNewMessageInteraction($this->pactHandle, $this->description, $this->messageHandle);
$calls = [
['pactffi_message_given', $this->messageHandle, 'item exist', null],
['pactffi_message_given_with_param', $this->messageHandle, 'item exist', 'id', '12', null],
['pactffi_message_given_with_param', $this->messageHandle, 'item exist', 'name', 'abc', null],
['pactffi_message_expects_to_receive', $this->messageHandle, $this->description, null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key1', 'value1', null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key2', 'value2', null],
];
$this->expectsMessageGiven($this->messageHandle, 'item exist');
$this->expectsMessageGivenWithParam($this->messageHandle, 'item exist', [
'id' => '12',
'name' => 'abc',
]);
$this->expectsMessageExpectsToReceive($this->messageHandle, $this->description);
$this->expectsMessageWithMetadataV2($this->messageHandle, $this->metadata);
$this->expectsSetInteractionKey($this->messageHandle, $this->description, $key, $success);
$this->assertClientCalls($calls);
$this->driver->registerMessage($this->message);
}

Expand All @@ -145,16 +141,14 @@ public function testSetPending(?bool $pending, bool $success): void
->method('getPact')
->willReturn(new Pact($this->pactHandle));
$this->expectsNewMessageInteraction($this->pactHandle, $this->description, $this->messageHandle);
$calls = [
['pactffi_message_given', $this->messageHandle, 'item exist', null],
['pactffi_message_given_with_param', $this->messageHandle, 'item exist', 'id', '12', null],
['pactffi_message_given_with_param', $this->messageHandle, 'item exist', 'name', 'abc', null],
['pactffi_message_expects_to_receive', $this->messageHandle, $this->description, null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key1', 'value1', null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key2', 'value2', null],
];
$this->expectsMessageGiven($this->messageHandle, 'item exist');
$this->expectsMessageGivenWithParam($this->messageHandle, 'item exist', [
'id' => '12',
'name' => 'abc',
]);
$this->expectsMessageExpectsToReceive($this->messageHandle, $this->description);
$this->expectsMessageWithMetadataV2($this->messageHandle, $this->metadata);
$this->expectsSetInteractionPending($this->messageHandle, $this->description, $pending, $success);
$this->assertClientCalls($calls);
$this->driver->registerMessage($this->message);
}

Expand All @@ -176,16 +170,14 @@ public function testSetComments(array $comments, bool $success): void
->method('getPact')
->willReturn(new Pact($this->pactHandle));
$this->expectsNewMessageInteraction($this->pactHandle, $this->description, $this->messageHandle);
$calls = [
['pactffi_message_given', $this->messageHandle, 'item exist', null],
['pactffi_message_given_with_param', $this->messageHandle, 'item exist', 'id', '12', null],
['pactffi_message_given_with_param', $this->messageHandle, 'item exist', 'name', 'abc', null],
['pactffi_message_expects_to_receive', $this->messageHandle, $this->description, null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key1', 'value1', null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key2', 'value2', null],
];
$this->expectsMessageGiven($this->messageHandle, 'item exist');
$this->expectsMessageGivenWithParam($this->messageHandle, 'item exist', [
'id' => '12',
'name' => 'abc',
]);
$this->expectsMessageExpectsToReceive($this->messageHandle, $this->description);
$this->expectsMessageWithMetadataV2($this->messageHandle, $this->metadata);
$this->expectsSetComments($this->messageHandle, $this->description, $comments, $success);
$this->assertClientCalls($calls);
$this->driver->registerMessage($this->message);
}

Expand All @@ -204,16 +196,14 @@ public function testAddTextComment(array $comments, bool $success): void
->method('getPact')
->willReturn(new Pact($this->pactHandle));
$this->expectsNewMessageInteraction($this->pactHandle, $this->description, $this->messageHandle);
$calls = [
['pactffi_message_given', $this->messageHandle, 'item exist', null],
['pactffi_message_given_with_param', $this->messageHandle, 'item exist', 'id', '12', null],
['pactffi_message_given_with_param', $this->messageHandle, 'item exist', 'name', 'abc', null],
['pactffi_message_expects_to_receive', $this->messageHandle, $this->description, null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key1', 'value1', null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key2', 'value2', null],
];
$this->expectsMessageGiven($this->messageHandle, 'item exist');
$this->expectsMessageGivenWithParam($this->messageHandle, 'item exist', [
'id' => '12',
'name' => 'abc',
]);
$this->expectsMessageExpectsToReceive($this->messageHandle, $this->description);
$this->expectsMessageWithMetadataV2($this->messageHandle, $this->metadata);
$this->expectsAddTextComments($this->messageHandle, $this->description, $comments, $success);
$this->assertClientCalls($calls);
$this->driver->registerMessage($this->message);
}
}
24 changes: 24 additions & 0 deletions tests/PhpPact/FFI/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,30 @@ public function testUponReceiving(): void
$this->assertFalse($result);
}

public function testMessageExpectsToReceive(): void
{
$this->client->messageExpectsToReceive(1, 'test');
$this->expectNotToPerformAssertions();
}

public function testMessageWithMetadataV2(): void
{
$this->client->messageWithMetadataV2(1, 'key', 'value');
$this->expectNotToPerformAssertions();
}

public function testMessageGiven(): void
{
$this->client->messageGiven(1, 'test');
$this->expectNotToPerformAssertions();
}

public function testMessageGivenWithParam(): void
{
$this->client->messageGivenWithParam(1, 'test', 'key', 'value');
$this->expectNotToPerformAssertions();
}

public function testGetInteractionPartRequest(): void
{
$this->assertSame(0, $this->client->getInteractionPartRequest());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,8 @@ public function testRegisterInteraction(): void
'id' => '12',
'name' => 'abc',
], true);
$calls = [
['pactffi_message_expects_to_receive', $this->messageHandle, $this->description, null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key1', 'value1', null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key2', 'value2', null],
];
$this->assertClientCalls($calls);
$this->expectsMessageExpectsToReceive($this->messageHandle, $this->description);
$this->expectsMessageWithMetadataV2($this->messageHandle, $this->metadata);
$this->driver->registerMessage($this->message);
$this->assertSame($this->messageHandle, $this->message->getHandle());
}
Expand All @@ -124,13 +120,9 @@ public function testSetKey(?string $key, bool $success): void
'id' => '12',
'name' => 'abc',
], true);
$calls = [
['pactffi_message_expects_to_receive', $this->messageHandle, $this->description, null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key1', 'value1', null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key2', 'value2', null],
];
$this->expectsMessageExpectsToReceive($this->messageHandle, $this->description);
$this->expectsMessageWithMetadataV2($this->messageHandle, $this->metadata);
$this->expectsSetInteractionKey($this->messageHandle, $this->description, $key, $success);
$this->assertClientCalls($calls);
$this->driver->registerMessage($this->message);
}

Expand All @@ -153,13 +145,9 @@ public function testSetPending(?bool $pending, bool $success): void
'id' => '12',
'name' => 'abc',
], true);
$calls = [
['pactffi_message_expects_to_receive', $this->messageHandle, $this->description, null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key1', 'value1', null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key2', 'value2', null],
];
$this->expectsMessageExpectsToReceive($this->messageHandle, $this->description);
$this->expectsMessageWithMetadataV2($this->messageHandle, $this->metadata);
$this->expectsSetInteractionPending($this->messageHandle, $this->description, $pending, $success);
$this->assertClientCalls($calls);
$this->driver->registerMessage($this->message);
}

Expand All @@ -186,13 +174,9 @@ public function testSetComments(array $comments, bool $success): void
'id' => '12',
'name' => 'abc',
], true);
$calls = [
['pactffi_message_expects_to_receive', $this->messageHandle, $this->description, null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key1', 'value1', null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key2', 'value2', null],
];
$this->expectsMessageExpectsToReceive($this->messageHandle, $this->description);
$this->expectsMessageWithMetadataV2($this->messageHandle, $this->metadata);
$this->expectsSetComments($this->messageHandle, $this->description, $comments, $success);
$this->assertClientCalls($calls);
$this->driver->registerMessage($this->message);
}

Expand All @@ -216,13 +200,9 @@ public function testAddTextComment(array $comments, bool $success): void
'id' => '12',
'name' => 'abc',
], true);
$calls = [
['pactffi_message_expects_to_receive', $this->messageHandle, $this->description, null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key1', 'value1', null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key2', 'value2', null],
];
$this->expectsMessageExpectsToReceive($this->messageHandle, $this->description);
$this->expectsMessageWithMetadataV2($this->messageHandle, $this->metadata);
$this->expectsAddTextComments($this->messageHandle, $this->description, $comments, $success);
$this->assertClientCalls($calls);
$this->driver->registerMessage($this->message);
}

Expand Down

0 comments on commit 35ddcde

Please sign in to comment.