Skip to content

Commit

Permalink
Merge pull request #649 from tienvx/use-ffi-call-wrappers
Browse files Browse the repository at this point in the history
refactor: Use ffi call wrappers
  • Loading branch information
tienvx authored Sep 26, 2024
2 parents f239bed + 650891f commit 47d0493
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ protected function withHeaders(Interaction $interaction, InteractionPart $intera
{
$headers = $interaction->getHeaders($interactionPart);
$partId = match ($interactionPart) {
InteractionPart::REQUEST => $this->client->get('InteractionPart_Request'),
InteractionPart::RESPONSE => $this->client->get('InteractionPart_Response'),
InteractionPart::REQUEST => $this->client->getInteractionPartRequest(),
InteractionPart::RESPONSE => $this->client->getInteractionPartResponse(),
};
foreach ($headers as $header => $values) {
foreach (array_values($values) as $index => $value) {
Expand Down
6 changes: 3 additions & 3 deletions src/PhpPact/Plugin/Driver/Body/PluginBodyDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function __construct(protected ClientInterface $client)
public function registerBody(Interaction|Message $interaction, InteractionPart $interactionPart): void
{
$body = $interaction instanceof Message ? $interaction->getContents() : $interaction->getBody($interactionPart);
$partId = $interaction instanceof Message ? $this->client->get('InteractionPart_Request') : match ($interactionPart) {
InteractionPart::REQUEST => $this->client->get('InteractionPart_Request'),
InteractionPart::RESPONSE => $this->client->get('InteractionPart_Response'),
$partId = $interaction instanceof Message ? $this->client->getInteractionPartRequest() : match ($interactionPart) {
InteractionPart::REQUEST => $this->client->getInteractionPartRequest(),
InteractionPart::RESPONSE => $this->client->getInteractionPartResponse(),
};
switch (true) {
case $body instanceof Binary:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public function testRegisterRequest(): void
{
$this->client
->expects($this->once())
->method('get')
->with('InteractionPart_Request')
->method('getInteractionPartRequest')
->willReturn($this->requestPartId);
$calls = [
['pactffi_with_header_v2', $this->interactionHandle, $this->requestPartId, 'header1', 0, 'header-value-1'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public function testRegisterResponse(): void
{
$this->client
->expects($this->once())
->method('get')
->with('InteractionPart_Response')
->method('getInteractionPartResponse')
->willReturn($this->responsePartId);
$calls = [
['pactffi_with_header_v2', $this->interactionHandle, $this->responsePartId, 'header1', 0, 'header-value-1'],
Expand Down
34 changes: 26 additions & 8 deletions tests/PhpPact/Plugin/Driver/Body/PluginBodyDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PhpPact\Plugin\Driver\Body\PluginBodyDriver;
use PhpPact\Plugin\Driver\Body\PluginBodyDriverInterface;
use PhpPact\Plugin\Exception\PluginBodyNotAddedException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand All @@ -38,13 +37,6 @@ class PluginBodyDriverTest extends TestCase
public function setUp(): void
{
$this->client = $this->createMock(ClientInterface::class);
$this->client
->expects($this->once())
->method('get')
->willReturnMap([
['InteractionPart_Request', $this->requestPartId],
['InteractionPart_Response', $this->responsePartId],
]);
$this->driver = new PluginBodyDriver($this->client);
$this->interaction = new Interaction();
$this->interaction->setHandle($this->interactionId);
Expand All @@ -62,6 +54,7 @@ public function setUp(): void
#[TestWith([InteractionPart::RESPONSE])]
public function testInteractionBinaryBody(InteractionPart $part): void
{
$this->expectsGetInteractionPartEnumMethods($part);
if ($part === InteractionPart::REQUEST) {
$this->interaction->getRequest()->setBody($this->binary);
} else {
Expand All @@ -79,6 +72,7 @@ public function testInteractionBinaryBody(InteractionPart $part): void
#[TestWith([InteractionPart::RESPONSE])]
public function testMessageBinaryBody(InteractionPart $part): void
{
$this->expectsGetInteractionPartEnumMethods(InteractionPart::REQUEST);
$this->message->setContents($this->binary);
$this->client
->expects($this->never())
Expand All @@ -92,6 +86,7 @@ public function testMessageBinaryBody(InteractionPart $part): void
#[TestWith([InteractionPart::RESPONSE])]
public function testInteractionPlainTextBody(InteractionPart $part): void
{
$this->expectsGetInteractionPartEnumMethods($part);
if ($part === InteractionPart::REQUEST) {
$this->interaction->getRequest()->setBody($this->text);
} else {
Expand All @@ -109,6 +104,7 @@ public function testInteractionPlainTextBody(InteractionPart $part): void
#[TestWith([InteractionPart::RESPONSE])]
public function testMessagePlainTextBody(InteractionPart $part): void
{
$this->expectsGetInteractionPartEnumMethods(InteractionPart::REQUEST);
$this->message->setContents($this->text);
$this->client
->expects($this->never())
Expand Down Expand Up @@ -141,6 +137,7 @@ private function getPluginBodyErrorMessage(int $error): string
#[TestWith([7])]
public function testRequestJsonBody(int $error): void
{
$this->expectsGetInteractionPartEnumMethods(InteractionPart::REQUEST);
$this->interaction->getRequest()->setBody($this->json);
$this->client
->expects($this->once())
Expand All @@ -164,6 +161,7 @@ public function testRequestJsonBody(int $error): void
#[TestWith([7])]
public function testResponseJsonBody(int $error): void
{
$this->expectsGetInteractionPartEnumMethods(InteractionPart::RESPONSE);
$this->interaction->getResponse()->setBody($this->json);
$this->client
->expects($this->once())
Expand All @@ -187,6 +185,7 @@ public function testResponseJsonBody(int $error): void
#[TestWith([7])]
public function testMessageJsonBody(int $error): void
{
$this->expectsGetInteractionPartEnumMethods(InteractionPart::REQUEST);
$this->message->setContents($this->json);
$this->client
->expects($this->once())
Expand All @@ -204,6 +203,7 @@ public function testMessageJsonBody(int $error): void
#[TestWith([InteractionPart::RESPONSE])]
public function testInteractionMultipartBody(InteractionPart $part): void
{
$this->expectsGetInteractionPartEnumMethods($part);
if ($part === InteractionPart::REQUEST) {
$this->interaction->getRequest()->setBody($this->multipart);
} else {
Expand All @@ -221,6 +221,7 @@ public function testInteractionMultipartBody(InteractionPart $part): void
#[TestWith([InteractionPart::RESPONSE])]
public function testEmptyInteractionBody(InteractionPart $part): void
{
$this->expectsGetInteractionPartEnumMethods($part);
$this->client
->expects($this->never())
->method('call');
Expand All @@ -236,4 +237,21 @@ public function testEmptyMessageBody(InteractionPart $part): void
->method('call');
$this->driver->registerBody($this->message, $part);
}

private function expectsGetInteractionPartEnumMethods(InteractionPart $part): void
{
if ($part === InteractionPart::REQUEST) {
$this->client
->expects($this->once())
->method('getInteractionPartRequest')
->willReturn($this->requestPartId);
$this->client->expects($this->never())->method('getInteractionPartResponse');
} else {
$this->client->expects($this->never())->method('getInteractionPartRequest');
$this->client
->expects($this->once())
->method('getInteractionPartResponse')
->willReturn($this->responsePartId);
}
}
}

0 comments on commit 47d0493

Please sign in to comment.