Skip to content

Commit

Permalink
Extract interaction part drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed May 8, 2023
1 parent 693daad commit d960221
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 77 deletions.
5 changes: 5 additions & 0 deletions src/PhpPact/Consumer/Driver/Interaction/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public function __construct(
protected PactDriverInterface $pactDriver
) {
}

public function getId(): int
{
return $this->id;
}
}
2 changes: 2 additions & 0 deletions src/PhpPact/Consumer/Driver/Interaction/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@

interface DriverInterface
{
public function getId(): int;

public function newInteraction(string $description): void;
}
49 changes: 0 additions & 49 deletions src/PhpPact/Consumer/Driver/Interaction/InteractionDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

namespace PhpPact\Consumer\Driver\Interaction;

use PhpPact\Consumer\Exception\InteractionBodyNotAddedException;

class InteractionDriver extends AbstractDriver implements InteractionDriverInterface
{
private const REQUEST = 'InteractionPart_Request';
private const RESPONSE = 'InteractionPart_Response';

public function newInteraction(string $description): void
{
$this->id = $this->client->call('pactffi_new_interaction', $this->pactDriver->getId(), $description);
Expand All @@ -19,17 +14,6 @@ public function uponReceiving(string $description): void
$this->client->call('pactffi_upon_receiving', $this->id, $description);
}

public function withBody(bool $isRequest, ?string $contentType = null, ?string $body = null): void
{
if (is_null($body)) {
return;
}
$success = $this->client->call('pactffi_with_body', $this->id, $this->getPart($isRequest), $contentType, $body);
if (!$success) {
throw new InteractionBodyNotAddedException();
}
}

public function given(array $providerStates): void
{
foreach ($providerStates as $providerState) {
Expand All @@ -39,37 +23,4 @@ public function given(array $providerStates): void
}
}
}

public function withHeaders(bool $isRequest, array $headers): void
{
foreach ($headers as $header => $values) {
foreach (array_values($values) as $index => $value) {
$this->client->call('pactffi_with_header_v2', $this->id, $this->getPart($isRequest), (string) $header, (int) $index, (string) $value);
}
}
}

public function withQueryParameters(array $queryParams): void
{
foreach ($queryParams as $key => $values) {
foreach (array_values($values) as $index => $value) {
$this->client->call('pactffi_with_query_parameter_v2', $this->id, (string) $key, (int) $index, (string) $value);
}
}
}

public function withRequest(string $method, string $path): void
{
$this->client->call('pactffi_with_request', $this->id, $method, $path);
}

public function withResponse(int $status): void
{
$this->client->call('pactffi_response_status', $this->id, $status);
}

private function getPart(bool $isRequest): int
{
return $this->client->get($isRequest ? self::REQUEST : self::RESPONSE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,8 @@ interface InteractionDriverInterface extends DriverInterface
{
public function uponReceiving(string $description): void;

public function withBody(bool $isRequest, ?string $contentType = null, ?string $body = null): void;

/**
* @param ProviderState[] $providerStates
*/
public function given(array $providerStates): void;

/**
* @param array<string, string[]> $headers
*/
public function withHeaders(bool $isRequest, array $headers): void;

/**
* @param array<string, string[]> $queryParams
*/
public function withQueryParameters(array $queryParams): void;

public function withRequest(string $method, string $path): void;

public function withResponse(int $status): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace PhpPact\Consumer\Driver\Interaction\Part;

use PhpPact\Consumer\Driver\Interaction\InteractionDriverInterface;
use PhpPact\Consumer\Exception\InteractionBodyNotAddedException;
use PhpPact\FFI\ClientInterface;

abstract class AbstractPartDriver implements PartDriverInterface
{
public function __construct(
protected ClientInterface $client,
protected InteractionDriverInterface $interactionDriver
) {
}

public function withBody(?string $contentType = null, ?string $body = null): void
{
if (is_null($body)) {
return;
}
$success = $this->client->call('pactffi_with_body', $this->getInteractionId(), $this->getPart(), $contentType, $body);
if (!$success) {
throw new InteractionBodyNotAddedException();
}
}

public function withHeaders(array $headers): void
{
foreach ($headers as $header => $values) {
foreach (array_values($values) as $index => $value) {
$this->client->call('pactffi_with_header_v2', $this->getInteractionId(), $this->getPart(), (string) $header, (int) $index, (string) $value);
}
}
}

protected function getInteractionId(): int
{
return $this->interactionDriver->getId();
}

abstract protected function getPart(): int;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace PhpPact\Consumer\Driver\Interaction\Part;

interface PartDriverInterface
{
public function withBody(?string $contentType = null, ?string $body = null): void;

/**
* @param array<string, string[]> $headers
*/
public function withHeaders(array $headers): void;
}
25 changes: 25 additions & 0 deletions src/PhpPact/Consumer/Driver/Interaction/Part/RequestDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace PhpPact\Consumer\Driver\Interaction\Part;

class RequestDriver extends AbstractPartDriver implements RequestDriverInterface
{
public function withQueryParameters(array $queryParams): void
{
foreach ($queryParams as $key => $values) {
foreach (array_values($values) as $index => $value) {
$this->client->call('pactffi_with_query_parameter_v2', $this->getInteractionId(), (string) $key, (int) $index, (string) $value);
}
}
}

public function withRequest(string $method, string $path): void
{
$this->client->call('pactffi_with_request', $this->getInteractionId(), $method, $path);
}

protected function getPart(): int
{
return $this->client->get('InteractionPart_Request');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace PhpPact\Consumer\Driver\Interaction\Part;

interface RequestDriverInterface extends PartDriverInterface
{
/**
* @param array<string, string[]> $queryParams
*/
public function withQueryParameters(array $queryParams): void;

public function withRequest(string $method, string $path): void;
}
16 changes: 16 additions & 0 deletions src/PhpPact/Consumer/Driver/Interaction/Part/ResponseDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace PhpPact\Consumer\Driver\Interaction\Part;

class ResponseDriver extends AbstractPartDriver implements ResponseDriverInterface
{
public function withResponse(int $status): void
{
$this->client->call('pactffi_response_status', $this->getInteractionId(), $status);
}

protected function getPart(): int
{
return $this->client->get('InteractionPart_Response');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace PhpPact\Consumer\Driver\Interaction\Part;

interface ResponseDriverInterface extends PartDriverInterface
{
public function withResponse(int $status): void;
}
6 changes: 5 additions & 1 deletion src/PhpPact/Consumer/Factory/InteractionRegistryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PhpPact\Consumer\Factory;

use PhpPact\Consumer\Driver\Interaction\InteractionDriver;
use PhpPact\Consumer\Driver\Interaction\Part\RequestDriver;
use PhpPact\Consumer\Driver\Interaction\Part\ResponseDriver;
use PhpPact\Consumer\Driver\Pact\PactDriver;
use PhpPact\Consumer\Service\InteractionRegistry;
use PhpPact\Consumer\Service\InteractionRegistryInterface;
Expand All @@ -17,8 +19,10 @@ public static function create(MockServerConfigInterface $config): InteractionReg
$client = new Client();
$pactDriver = new PactDriver($client, $config);
$interactionDriver = new InteractionDriver($client, $pactDriver);
$requestDriver = new RequestDriver($client, $interactionDriver);
$responseDriver = new ResponseDriver($client, $interactionDriver);
$mockServer = new MockServer($client, $pactDriver, $config);

return new InteractionRegistry($interactionDriver, $mockServer);
return new InteractionRegistry($interactionDriver, $requestDriver, $responseDriver, $mockServer);
}
}
26 changes: 15 additions & 11 deletions src/PhpPact/Consumer/Service/InteractionRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
namespace PhpPact\Consumer\Service;

use PhpPact\Consumer\Driver\Interaction\InteractionDriverInterface;
use PhpPact\Consumer\Driver\Interaction\Part\RequestDriverInterface;
use PhpPact\Consumer\Driver\Interaction\Part\ResponseDriverInterface;
use PhpPact\Consumer\Model\Interaction;

class InteractionRegistry implements InteractionRegistryInterface
{
public function __construct(
private InteractionDriverInterface $driver,
private InteractionDriverInterface $interactionDriver,
private RequestDriverInterface $requestDriver,
private ResponseDriverInterface $responseDriver,
private MockServerInterface $mockServer
) {
}
Expand Down Expand Up @@ -53,42 +57,42 @@ private function writePact(): void

private function newInteraction(Interaction $interaction): self
{
$this->driver->newInteraction($interaction->getDescription());
$this->interactionDriver->newInteraction($interaction->getDescription());

return $this;
}

private function given(Interaction $interaction): self
{
$this->driver->given($interaction->getProviderStates());
$this->interactionDriver->given($interaction->getProviderStates());

return $this;
}

private function uponReceiving(Interaction $interaction): self
{
$this->driver->uponReceiving($interaction->getDescription());
$this->interactionDriver->uponReceiving($interaction->getDescription());

return $this;
}

private function with(Interaction $interaction): self
{
$request = $interaction->getRequest();
$this->driver->withRequest($request->getMethod(), $request->getPath());
$this->driver->withHeaders(true, $request->getHeaders());
$this->driver->withQueryParameters($request->getQuery());
$this->driver->withBody(true, null, $request->getBody());
$this->requestDriver->withRequest($request->getMethod(), $request->getPath());
$this->requestDriver->withHeaders($request->getHeaders());
$this->requestDriver->withQueryParameters($request->getQuery());
$this->requestDriver->withBody(null, $request->getBody());

return $this;
}

private function willRespondWith(Interaction $interaction): self
{
$response = $interaction->getResponse();
$this->driver->withResponse($response->getStatus());
$this->driver->withHeaders(false, $response->getHeaders());
$this->driver->withBody(false, null, $response->getBody());
$this->responseDriver->withResponse($response->getStatus());
$this->responseDriver->withHeaders($response->getHeaders());
$this->responseDriver->withBody(null, $response->getBody());

return $this;
}
Expand Down

0 comments on commit d960221

Please sign in to comment.