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 > setup interaction methods #655

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
55 changes: 55 additions & 0 deletions helper/FFI/ClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpPact\Consumer\Driver\Exception\InteractionCommentNotSetException;
use PhpPact\Consumer\Driver\Exception\InteractionKeyNotSetException;
use PhpPact\Consumer\Driver\Exception\InteractionNotModifiedException;
use PhpPact\Consumer\Driver\Exception\InteractionPendingNotSetException;
use PhpPact\FFI\ClientInterface;
use PHPUnit\Framework\Constraint\Constraint;
Expand Down Expand Up @@ -141,4 +142,58 @@ protected function expectsNewSyncMessageInteraction(int $pact, string $descripti
->with($pact, $description)
->willReturn($interaction);
}

protected function expectsGiven(int $interaction, string $name, bool $result): void
{
$this->client
->expects($this->once())
->method('given')
->with($interaction, $name)
->willReturn($result);
if (!$result) {
$this->expectException(InteractionNotModifiedException::class);
$this->expectExceptionMessage("The interaction or Pact can't be modified (i.e. the mock server for it has already started)");
}
}

/**
* @param array<string, string> $params
*/
protected function expectsGivenWithParam(int $interaction, string $name, array $params, bool $result): void
{
$calls = [];
$lastKey = array_key_last($params);
foreach ($params as $key => $value) {
$calls[] = [$interaction, $name, $key, $value, $key === $lastKey ? $result : true];
}
$this->client
->expects($this->exactly(count($calls)))
->method('givenWithParam')
->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(InteractionNotModifiedException::class);
$this->expectExceptionMessage("The interaction or Pact can't be modified (i.e. the mock server for it has already started)");
}
}

protected function expectsUponReceiving(int $interaction, string $description, bool $result): void
{
$this->client
->expects($this->once())
->method('uponReceiving')
->with($interaction, $description)
->willReturn($result);
if (!$result) {
$this->expectException(InteractionNotModifiedException::class);
$this->expectExceptionMessage("The interaction or Pact can't be modified (i.e. the mock server for it has already started)");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace PhpPact\Consumer\Driver\Exception;

class InteractionNotModifiedException extends DriverException
{
public function __construct()
{
parent::__construct("The interaction or Pact can't be modified (i.e. the mock server for it has already started)");
}
}
16 changes: 13 additions & 3 deletions src/PhpPact/Consumer/Driver/Interaction/InteractionDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpPact\Consumer\Driver\Interaction;

use PhpPact\Consumer\Driver\Exception\InteractionNotModifiedException;
use PhpPact\Consumer\Driver\InteractionPart\RequestDriver;
use PhpPact\Consumer\Driver\InteractionPart\RequestDriverInterface;
use PhpPact\Consumer\Driver\InteractionPart\ResponseDriver;
Expand Down Expand Up @@ -67,15 +68,24 @@ protected function newInteraction(Interaction $interaction): void

private function uponReceiving(Interaction $interaction): void
{
$this->client->call('pactffi_upon_receiving', $interaction->getHandle(), $interaction->getDescription());
$success = $this->client->uponReceiving($interaction->getHandle(), $interaction->getDescription());
if (!$success) {
throw new InteractionNotModifiedException();
}
}

private function given(Interaction $interaction): void
{
foreach ($interaction->getProviderStates() as $providerState) {
$this->client->call('pactffi_given', $interaction->getHandle(), $providerState->getName());
$success = $this->client->given($interaction->getHandle(), $providerState->getName());
if (!$success) {
throw new InteractionNotModifiedException();
}
foreach ($providerState->getParams() as $key => $value) {
$this->client->call('pactffi_given_with_param', $interaction->getHandle(), $providerState->getName(), (string) $key, (string) $value);
$success = $this->client->givenWithParam($interaction->getHandle(), $providerState->getName(), (string) $key, (string) $value);
if (!$success) {
throw new InteractionNotModifiedException();
}
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/PhpPact/FFI/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,36 @@ public function newSyncMessageInteraction(int $pact, string $description): int
return $result;
}

public function given(int $interaction, string $name): bool
{
$method = 'pactffi_given';
$result = $this->call($method, $interaction, $name);
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 givenWithParam(int $interaction, string $name, string $key, string $value): bool
{
$method = 'pactffi_given_with_param';
$result = $this->call($method, $interaction, $name, $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 uponReceiving(int $interaction, string $description): bool
{
$method = 'pactffi_upon_receiving';
$result = $this->call($method, $interaction, $description);
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
6 changes: 6 additions & 0 deletions src/PhpPact/FFI/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public function newMessageInteraction(int $pact, string $description): int;

public function newSyncMessageInteraction(int $pact, string $description): int;

public function given(int $interaction, string $name): bool;

public function givenWithParam(int $interaction, string $name, string $key, string $value): bool;

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

public function getInteractionPartRequest(): int;

public function getInteractionPartResponse(): int;
Expand Down
11 changes: 9 additions & 2 deletions src/PhpPact/SyncMessage/Driver/Interaction/SyncMessageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpPact\SyncMessage\Driver\Interaction;

use PhpPact\Consumer\Driver\Body\MessageBodyDriverInterface;
use PhpPact\Consumer\Driver\Exception\InteractionNotModifiedException;
use PhpPact\Consumer\Driver\Interaction\AbstractMessageDriver;
use PhpPact\Consumer\Driver\Pact\PactDriverInterface;
use PhpPact\Consumer\Model\Message;
Expand Down Expand Up @@ -48,9 +49,15 @@ protected function newInteraction(Message $message): void
protected function given(Message $message): void
{
foreach ($message->getProviderStates() as $providerState) {
$this->client->call('pactffi_given', $message->getHandle(), $providerState->getName());
$success = $this->client->given($message->getHandle(), $providerState->getName());
if (!$success) {
throw new InteractionNotModifiedException();
}
foreach ($providerState->getParams() as $key => $value) {
$this->client->call('pactffi_given_with_param', $message->getHandle(), $providerState->getName(), (string) $key, (string) $value);
$success = $this->client->givenWithParam($message->getHandle(), $providerState->getName(), (string) $key, (string) $value);
if (!$success) {
throw new InteractionNotModifiedException();
}
}
}
}
Expand Down
110 changes: 72 additions & 38 deletions tests/PhpPact/Consumer/Driver/Interaction/InteractionDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace PhpPactTest\Consumer\Driver\Interaction;

use PhpPact\Consumer\Driver\Exception\InteractionCommentNotSetException;
use PhpPact\Consumer\Driver\Exception\InteractionKeyNotSetException;
use PhpPact\Consumer\Driver\Exception\InteractionPendingNotSetException;
use PhpPact\Consumer\Driver\Interaction\InteractionDriver;
use PhpPact\Consumer\Driver\Interaction\InteractionDriverInterface;
use PhpPact\Consumer\Driver\InteractionPart\RequestDriverInterface;
Expand Down Expand Up @@ -96,13 +93,12 @@ public function testRegisterInteraction(bool $startMockServer): void
->method('registerResponse')
->with($this->interaction);
$this->expectsNewInteraction($this->pactHandle, $this->description, $this->interactionHandle);
$calls = [
['pactffi_given', $this->interactionHandle, 'item exist', null],
['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],
];
$this->assertClientCalls($calls);
$this->expectsGiven($this->interactionHandle, 'item exist', true);
$this->expectsGivenWithParam($this->interactionHandle, 'item exist', [
'id' => '12',
'name' => 'abc',
], true);
$this->expectsUponReceiving($this->interactionHandle, $this->description, true);
$this->mockServer
->expects($startMockServer ? $this->once() : $this->never())
->method('start');
Expand All @@ -122,14 +118,13 @@ public function testSetKey(?string $key, bool $success): void
->method('getPact')
->willReturn(new Pact($this->pactHandle));
$this->expectsNewInteraction($this->pactHandle, $this->description, $this->interactionHandle);
$calls = [
['pactffi_given', $this->interactionHandle, 'item exist', null],
['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],
];
$this->expectsGiven($this->interactionHandle, 'item exist', true);
$this->expectsGivenWithParam($this->interactionHandle, 'item exist', [
'id' => '12',
'name' => 'abc',
], true);
$this->expectsUponReceiving($this->interactionHandle, $this->description, true);
$this->expectsSetInteractionKey($this->interactionHandle, $this->description, $key, $success);
$this->assertClientCalls($calls);
$this->driver->registerInteraction($this->interaction, false);
}

Expand All @@ -147,14 +142,13 @@ public function testSetPending(?bool $pending, bool $success): void
->method('getPact')
->willReturn(new Pact($this->pactHandle));
$this->expectsNewInteraction($this->pactHandle, $this->description, $this->interactionHandle);
$calls = [
['pactffi_given', $this->interactionHandle, 'item exist', null],
['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],
];
$this->expectsGiven($this->interactionHandle, 'item exist', true);
$this->expectsGivenWithParam($this->interactionHandle, 'item exist', [
'id' => '12',
'name' => 'abc',
], true);
$this->expectsUponReceiving($this->interactionHandle, $this->description, true);
$this->expectsSetInteractionPending($this->interactionHandle, $this->description, $pending, $success);
$this->assertClientCalls($calls);
$this->driver->registerInteraction($this->interaction, false);
}

Expand All @@ -176,14 +170,13 @@ public function testSetComments(array $comments, bool $success): void
->method('getPact')
->willReturn(new Pact($this->pactHandle));
$this->expectsNewInteraction($this->pactHandle, $this->description, $this->interactionHandle);
$calls = [
['pactffi_given', $this->interactionHandle, 'item exist', null],
['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],
];
$this->expectsGiven($this->interactionHandle, 'item exist', true);
$this->expectsGivenWithParam($this->interactionHandle, 'item exist', [
'id' => '12',
'name' => 'abc',
], true);
$this->expectsUponReceiving($this->interactionHandle, $this->description, true);
$this->expectsSetComments($this->interactionHandle, $this->description, $comments, $success);
$this->assertClientCalls($calls);
$this->driver->registerInteraction($this->interaction, false);
}

Expand All @@ -202,14 +195,55 @@ public function testAddTextComment(array $comments, bool $success): void
->method('getPact')
->willReturn(new Pact($this->pactHandle));
$this->expectsNewInteraction($this->pactHandle, $this->description, $this->interactionHandle);
$calls = [
['pactffi_given', $this->interactionHandle, 'item exist', null],
['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],
];
$this->expectsGiven($this->interactionHandle, 'item exist', true);
$this->expectsGivenWithParam($this->interactionHandle, 'item exist', [
'id' => '12',
'name' => 'abc',
], true);
$this->expectsUponReceiving($this->interactionHandle, $this->description, true);
$this->expectsAddTextComments($this->interactionHandle, $this->description, $comments, $success);
$this->assertClientCalls($calls);
$this->driver->registerInteraction($this->interaction, false);
}

public function testGivenCanNotModifyInteraction(): void
{
$this->pactDriver
->expects($this->once())
->method('getPact')
->willReturn(new Pact($this->pactHandle));
$this->expectsNewInteraction($this->pactHandle, $this->description, $this->interactionHandle);
$this->expectsGiven($this->interactionHandle, 'item exist', false);
$this->driver->registerInteraction($this->interaction, false);
}

public function testGivenWithParamCanNotModifyInteraction(): void
{
$this->pactDriver
->expects($this->once())
->method('getPact')
->willReturn(new Pact($this->pactHandle));
$this->expectsNewInteraction($this->pactHandle, $this->description, $this->interactionHandle);
$this->expectsGiven($this->interactionHandle, 'item exist', true);
$this->expectsGivenWithParam($this->interactionHandle, 'item exist', [
'id' => '12',
'name' => 'abc',
], false);
$this->driver->registerInteraction($this->interaction, false);
}

public function testUponReceivingCanNotModifyInteraction(): void
{
$this->pactDriver
->expects($this->once())
->method('getPact')
->willReturn(new Pact($this->pactHandle));
$this->expectsNewInteraction($this->pactHandle, $this->description, $this->interactionHandle);
$this->expectsGiven($this->interactionHandle, 'item exist', true);
$this->expectsGivenWithParam($this->interactionHandle, 'item exist', [
'id' => '12',
'name' => 'abc',
], true);
$this->expectsUponReceiving($this->interactionHandle, $this->description, false);
$this->driver->registerInteraction($this->interaction, false);
}
}
18 changes: 18 additions & 0 deletions tests/PhpPact/FFI/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ public function testNewSyncMessageInteraction(): void
$this->assertIsInt($result);
}

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

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

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

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