Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Wrap ffi call > set interaction attributes methods #652

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions helper/FFI/ClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace PhpPactTest\Helper\FFI;

use PhpPact\Consumer\Driver\Exception\InteractionCommentNotSetException;
use PhpPact\Consumer\Driver\Exception\InteractionKeyNotSetException;
use PhpPact\Consumer\Driver\Exception\InteractionPendingNotSetException;
use PhpPact\FFI\ClientInterface;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsIdentical;
Expand Down Expand Up @@ -29,4 +32,86 @@ protected function assertClientCalls(array $calls): void
return $return;
});
}

protected function expectsSetInteractionKey(int $interaction, string $description, ?string $key, bool $result): void
{
$this->client
->expects($this->exactly($key === null ? 0 : 1))
->method('setKey')
->with($interaction, $key)
->willReturn($result);
if (!$result) {
$this->expectException(InteractionKeyNotSetException::class);
$this->expectExceptionMessage("Can not set the key '$key' for the interaction '{$description}'");
}
}

protected function expectsSetInteractionPending(int $interaction, string $description, ?bool $pending, bool $result): void
{
$this->client
->expects($this->exactly($pending === null ? 0 : 1))
->method('setPending')
->with($interaction, $pending)
->willReturn($result);
if (!$result) {
$this->expectException(InteractionPendingNotSetException::class);
$this->expectExceptionMessage("Can not mark interaction '{$description}' as pending");
}
}

/**
* @param array<string, mixed> $comments
*/
protected function expectsSetComments(int $interaction, string $description, array $comments, bool $result): void
{
$calls = [];
$lastKey = array_key_last($comments);
foreach ($comments as $key => $value) {
$calls[] = [$interaction, $key, (is_string($value) || is_null($value)) ? $value : json_encode($value), $key === $lastKey ? $result : true];
}
$this->client
->expects($this->exactly(count($calls)))
->method('setComment')
->willReturnCallback(function (...$args) use (&$calls) {
$call = array_shift($calls);
$return = array_pop($call);
foreach ($args as $key => $arg) {
$this->assertThat($arg, $call[$key] instanceof Constraint ? $call[$key] : new IsIdentical($call[$key]));
}

return $return;
});
if (!$result) {
$this->expectException(InteractionCommentNotSetException::class);
$this->expectExceptionMessage("Can not add comment '$key' to the interaction '{$description}'");
}
}

/**
* @param string[] $comments
*/
protected function expectsAddTextComments(int $interaction, string $description, array $comments, bool $result): void
{
$calls = [];
$lastKey = array_key_last($comments);
foreach ($comments as $key => $comment) {
$calls[] = [$interaction, $comment, $key === $lastKey ? $result : true];
}
$this->client
->expects($this->exactly(count($calls)))
->method('addTextComment')
->willReturnCallback(function (...$args) use (&$calls) {
$call = array_shift($calls);
$return = array_pop($call);
foreach ($args as $key => $arg) {
$this->assertThat($arg, $call[$key] instanceof Constraint ? $call[$key] : new IsIdentical($call[$key]));
}

return $return;
});
if (!$result) {
$this->expectException(InteractionCommentNotSetException::class);
$this->expectExceptionMessage("Can not add text comment '$comment' to the interaction '{$description}'");
}
}
}
11 changes: 7 additions & 4 deletions src/PhpPact/Consumer/Driver/Interaction/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function setKey(Interaction|Message $interaction): void
if (null === $key) {
return;
}
$success = $this->client->call('pactffi_set_key', $interaction->getHandle(), $key);
$success = $this->client->setKey($interaction->getHandle(), $key);
if (!$success) {
throw new InteractionKeyNotSetException(sprintf("Can not set the key '%s' for the interaction '%s'", $key, $interaction->getDescription()));
}
Expand All @@ -34,7 +34,7 @@ protected function setPending(Interaction|Message $interaction): void
if (null === $pending) {
return;
}
$success = $this->client->call('pactffi_set_pending', $interaction->getHandle(), $pending);
$success = $this->client->setPending($interaction->getHandle(), $pending);
if (!$success) {
throw new InteractionPendingNotSetException(sprintf("Can not mark interaction '%s' as pending", $interaction->getDescription()));
}
Expand All @@ -44,13 +44,16 @@ protected function setComments(Interaction|Message $interaction): void
{
foreach ($interaction->getComments() as $key => $value) {
$value = (is_string($value) || is_null($value)) ? $value : json_encode($value);
$success = $this->client->call('pactffi_set_comment', $interaction->getHandle(), $key, $value);
if (is_bool($value)) {
throw new InteractionCommentNotSetException(sprintf("Can not json encode value of comment '%s'", $key));
}
$success = $this->client->setComment($interaction->getHandle(), $key, $value);
if (!$success) {
throw new InteractionCommentNotSetException(sprintf("Can not add comment '%s' to the interaction '%s'", $key, $interaction->getDescription()));
}
}
foreach ($interaction->getTextComments() as $value) {
$success = $this->client->call('pactffi_add_text_comment', $interaction->getHandle(), $value);
$success = $this->client->addTextComment($interaction->getHandle(), $value);
if (!$success) {
throw new InteractionCommentNotSetException(sprintf("Can not add text comment '%s' to the interaction '%s'", $value, $interaction->getDescription()));
}
Expand Down
40 changes: 40 additions & 0 deletions src/PhpPact/FFI/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,46 @@ public function withMultipartFileV2(int $interaction, int $part, string $content
throw new InvalidResultException(sprintf('Invalid result of "%s". Neither ok or failed', $method));
}

public function setKey(int $interaction, string $key): bool
{
$method = 'pactffi_set_key';
$result = $this->call($method, $interaction, $key);
if (!is_bool($result)) {
throw new InvalidResultException(sprintf('Invalid result of "%s". Expected "boolean", but got "%s"', $method, get_debug_type($result)));
}
return $result;
}

public function setPending(int $interaction, bool $pending): bool
{
$method = 'pactffi_set_pending';
$result = $this->call($method, $interaction, $pending);
if (!is_bool($result)) {
throw new InvalidResultException(sprintf('Invalid result of "%s". Expected "boolean", but got "%s"', $method, get_debug_type($result)));
}
return $result;
}

public function setComment(int $interaction, string $key, ?string $value): bool
{
$method = 'pactffi_set_comment';
$result = $this->call($method, $interaction, $key, $value);
if (!is_bool($result)) {
throw new InvalidResultException(sprintf('Invalid result of "%s". Expected "boolean", but got "%s"', $method, get_debug_type($result)));
}
return $result;
}

public function addTextComment(int $interaction, string $value): bool
{
$method = 'pactffi_add_text_comment';
$result = $this->call($method, $interaction, $value);
if (!is_bool($result)) {
throw new InvalidResultException(sprintf('Invalid result of "%s". Expected "boolean", but got "%s"', $method, get_debug_type($result)));
}
return $result;
}

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 @@ -13,6 +13,14 @@ public function withBody(int $interaction, int $part, string $contentType, strin

public function withMultipartFileV2(int $interaction, int $part, string $contentType, string $path, string $name, string $boundary): Result;

public function setKey(int $interaction, string $key): bool;

public function setPending(int $interaction, bool $pending): bool;

public function setComment(int $interaction, string $key, ?string $value): bool;

public function addTextComment(int $interaction, string $value): bool;

public function getInteractionPartRequest(): int;

public function getInteractionPartResponse(): int;
Expand Down
43 changes: 13 additions & 30 deletions tests/PhpPact/Consumer/Driver/Interaction/InteractionDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,7 @@ public function testSetKey(?string $key, bool $success): void
['pactffi_given_with_param', $this->interactionHandle, 'item exist', 'name', 'abc', null],
['pactffi_upon_receiving', $this->interactionHandle, $this->description, null],
];
if (is_string($key)) {
$calls[] = ['pactffi_set_key', $this->interactionHandle, $key, $success];
}
if (!$success) {
$this->expectException(InteractionKeyNotSetException::class);
$this->expectExceptionMessage("Can not set the key '$key' for the interaction '{$this->description}'");
}
$this->expectsSetInteractionKey($this->interactionHandle, $this->description, $key, $success);
$this->assertClientCalls($calls);
$this->driver->registerInteraction($this->interaction, false);
}
Expand All @@ -159,13 +153,7 @@ public function testSetPending(?bool $pending, bool $success): void
['pactffi_given_with_param', $this->interactionHandle, 'item exist', 'name', 'abc', null],
['pactffi_upon_receiving', $this->interactionHandle, $this->description, null],
];
if (is_bool($pending)) {
$calls[] = ['pactffi_set_pending', $this->interactionHandle, $pending, $success];
}
if (!$success) {
$this->expectException(InteractionPendingNotSetException::class);
$this->expectExceptionMessage("Can not mark interaction '{$this->description}' as pending");
}
$this->expectsSetInteractionPending($this->interactionHandle, $this->description, $pending, $success);
$this->assertClientCalls($calls);
$this->driver->registerInteraction($this->interaction, false);
}
Expand Down Expand Up @@ -194,22 +182,21 @@ public function testSetComments(array $comments, bool $success): void
['pactffi_given_with_param', $this->interactionHandle, 'item exist', 'name', 'abc', null],
['pactffi_upon_receiving', $this->interactionHandle, $this->description, null],
];
foreach ($comments as $key => $value) {
$calls[] = ['pactffi_set_comment', $this->interactionHandle, $key, (is_string($value) || is_null($value)) ? $value : json_encode($value), $success];
if (!$success) {
$this->expectException(InteractionCommentNotSetException::class);
$this->expectExceptionMessage("Can not add comment '$key' to the interaction '{$this->description}'");
}
}
$this->expectsSetComments($this->interactionHandle, $this->description, $comments, $success);
$this->assertClientCalls($calls);
$this->driver->registerInteraction($this->interaction, false);
}

#[TestWith(['comment 1', false])]
#[TestWith(['comment 2', true])]
public function testAddTextComment(string $comment, bool $success): void
/**
* @param string[] $comments
*/
#[TestWith([['comment 1', 'comment 2'], false])]
#[TestWith([['comment 1', 'comment 2'], true])]
public function testAddTextComment(array $comments, bool $success): void
{
$this->interaction->addTextComment($comment);
foreach ($comments as $comment) {
$this->interaction->addTextComment($comment);
}
$this->pactDriver
->expects($this->once())
->method('getPact')
Expand All @@ -220,12 +207,8 @@ public function testAddTextComment(string $comment, bool $success): void
['pactffi_given_with_param', $this->interactionHandle, 'item exist', 'id', '12', null],
['pactffi_given_with_param', $this->interactionHandle, 'item exist', 'name', 'abc', null],
['pactffi_upon_receiving', $this->interactionHandle, $this->description, null],
['pactffi_add_text_comment', $this->interactionHandle, $comment, $success],
];
if (!$success) {
$this->expectException(InteractionCommentNotSetException::class);
$this->expectExceptionMessage("Can not add text comment '$comment' to the interaction '{$this->description}'");
}
$this->expectsAddTextComments($this->interactionHandle, $this->description, $comments, $success);
$this->assertClientCalls($calls);
$this->driver->registerInteraction($this->interaction, false);
}
Expand Down
43 changes: 13 additions & 30 deletions tests/PhpPact/Consumer/Driver/Interaction/MessageDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,7 @@ public function testSetKey(?string $key, bool $success): void
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key1', 'value1', null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key2', 'value2', null],
];
if (is_string($key)) {
$calls[] = ['pactffi_set_key', $this->messageHandle, $key, $success];
}
if (!$success) {
$this->expectException(InteractionKeyNotSetException::class);
$this->expectExceptionMessage("Can not set the key '$key' for the interaction '{$this->description}'");
}
$this->expectsSetInteractionKey($this->messageHandle, $this->description, $key, $success);
$this->assertClientCalls($calls);
$this->driver->registerMessage($this->message);
}
Expand All @@ -159,13 +153,7 @@ public function testSetPending(?bool $pending, bool $success): void
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key1', 'value1', null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key2', 'value2', null],
];
if (is_bool($pending)) {
$calls[] = ['pactffi_set_pending', $this->messageHandle, $pending, $success];
}
if (!$success) {
$this->expectException(InteractionPendingNotSetException::class);
$this->expectExceptionMessage("Can not mark interaction '{$this->description}' as pending");
}
$this->expectsSetInteractionPending($this->messageHandle, $this->description, $pending, $success);
$this->assertClientCalls($calls);
$this->driver->registerMessage($this->message);
}
Expand Down Expand Up @@ -196,22 +184,21 @@ public function testSetComments(array $comments, bool $success): void
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key1', 'value1', null],
['pactffi_message_with_metadata_v2', $this->messageHandle, 'key2', 'value2', null],
];
foreach ($comments as $key => $value) {
$calls[] = ['pactffi_set_comment', $this->messageHandle, $key, (is_string($value) || is_null($value)) ? $value : json_encode($value), $success];
if (!$success) {
$this->expectException(InteractionCommentNotSetException::class);
$this->expectExceptionMessage("Can not add comment '$key' to the interaction '{$this->description}'");
}
}
$this->expectsSetComments($this->messageHandle, $this->description, $comments, $success);
$this->assertClientCalls($calls);
$this->driver->registerMessage($this->message);
}

#[TestWith(['comment 1', false])]
#[TestWith(['comment 2', true])]
public function testAddTextComment(string $comment, bool $success): void
/**
* @param string[] $comments
*/
#[TestWith([['comment 1', 'comment 2'], false])]
#[TestWith([['comment 1', 'comment 2'], true])]
public function testAddTextComment(array $comments, bool $success): void
{
$this->message->addTextComment($comment);
foreach ($comments as $comment) {
$this->message->addTextComment($comment);
}
$this->pactDriver
->expects($this->once())
->method('getPact')
Expand All @@ -224,12 +211,8 @@ public function testAddTextComment(string $comment, bool $success): void
['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],
['pactffi_add_text_comment', $this->messageHandle, $comment, $success],
];
if (!$success) {
$this->expectException(InteractionCommentNotSetException::class);
$this->expectExceptionMessage("Can not add text comment '$comment' to the interaction '{$this->description}'");
}
$this->expectsAddTextComments($this->messageHandle, $this->description, $comments, $success);
$this->assertClientCalls($calls);
$this->driver->registerMessage($this->message);
}
Expand Down
24 changes: 24 additions & 0 deletions tests/PhpPact/FFI/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ public function testWithMultipartFileV2(): void
$this->assertSame('with_multipart_file: Interaction handle is invalid', $result->message);
}

public function testSetKey(): void
{
$result = $this->client->setKey(1, 'test');
$this->assertFalse($result);
}

public function testSetPending(): void
{
$result = $this->client->setPending(1, true);
$this->assertFalse($result);
}

public function testSetComment(): void
{
$result = $this->client->setComment(1, 'key', 'value');
$this->assertFalse($result);
}

public function testAddTextComment(): void
{
$result = $this->client->addTextComment(1, 'test');
$this->assertFalse($result);
}

public function testGetInteractionPartRequest(): void
{
$this->assertSame(0, $this->client->getInteractionPartRequest());
Expand Down
Loading