diff --git a/src/PhpPact/Consumer/Driver/Interaction/AbstractDriver.php b/src/PhpPact/Consumer/Driver/Interaction/AbstractDriver.php index 105e413b..1a035e7f 100644 --- a/src/PhpPact/Consumer/Driver/Interaction/AbstractDriver.php +++ b/src/PhpPact/Consumer/Driver/Interaction/AbstractDriver.php @@ -14,4 +14,9 @@ public function __construct( protected PactDriverInterface $pactDriver ) { } + + public function getId(): int + { + return $this->id; + } } diff --git a/src/PhpPact/Consumer/Driver/Interaction/DriverInterface.php b/src/PhpPact/Consumer/Driver/Interaction/DriverInterface.php index d5a87739..4020bf1d 100644 --- a/src/PhpPact/Consumer/Driver/Interaction/DriverInterface.php +++ b/src/PhpPact/Consumer/Driver/Interaction/DriverInterface.php @@ -4,5 +4,7 @@ interface DriverInterface { + public function getId(): int; + public function newInteraction(string $description): void; } diff --git a/src/PhpPact/Consumer/Driver/Interaction/InteractionDriver.php b/src/PhpPact/Consumer/Driver/Interaction/InteractionDriver.php index 66279746..aeb51c27 100644 --- a/src/PhpPact/Consumer/Driver/Interaction/InteractionDriver.php +++ b/src/PhpPact/Consumer/Driver/Interaction/InteractionDriver.php @@ -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); @@ -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) { @@ -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); - } } diff --git a/src/PhpPact/Consumer/Driver/Interaction/InteractionDriverInterface.php b/src/PhpPact/Consumer/Driver/Interaction/InteractionDriverInterface.php index d868cc14..72554701 100644 --- a/src/PhpPact/Consumer/Driver/Interaction/InteractionDriverInterface.php +++ b/src/PhpPact/Consumer/Driver/Interaction/InteractionDriverInterface.php @@ -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 $headers - */ - public function withHeaders(bool $isRequest, array $headers): void; - - /** - * @param array $queryParams - */ - public function withQueryParameters(array $queryParams): void; - - public function withRequest(string $method, string $path): void; - - public function withResponse(int $status): void; } diff --git a/src/PhpPact/Consumer/Driver/Interaction/Part/AbstractPartDriver.php b/src/PhpPact/Consumer/Driver/Interaction/Part/AbstractPartDriver.php new file mode 100644 index 00000000..a9fc2326 --- /dev/null +++ b/src/PhpPact/Consumer/Driver/Interaction/Part/AbstractPartDriver.php @@ -0,0 +1,43 @@ +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; +} diff --git a/src/PhpPact/Consumer/Driver/Interaction/Part/PartDriverInterface.php b/src/PhpPact/Consumer/Driver/Interaction/Part/PartDriverInterface.php new file mode 100644 index 00000000..37d58fc8 --- /dev/null +++ b/src/PhpPact/Consumer/Driver/Interaction/Part/PartDriverInterface.php @@ -0,0 +1,13 @@ + $headers + */ + public function withHeaders(array $headers): void; +} diff --git a/src/PhpPact/Consumer/Driver/Interaction/Part/RequestDriver.php b/src/PhpPact/Consumer/Driver/Interaction/Part/RequestDriver.php new file mode 100644 index 00000000..23345a40 --- /dev/null +++ b/src/PhpPact/Consumer/Driver/Interaction/Part/RequestDriver.php @@ -0,0 +1,25 @@ + $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'); + } +} diff --git a/src/PhpPact/Consumer/Driver/Interaction/Part/RequestDriverInterface.php b/src/PhpPact/Consumer/Driver/Interaction/Part/RequestDriverInterface.php new file mode 100644 index 00000000..de19cbb0 --- /dev/null +++ b/src/PhpPact/Consumer/Driver/Interaction/Part/RequestDriverInterface.php @@ -0,0 +1,13 @@ + $queryParams + */ + public function withQueryParameters(array $queryParams): void; + + public function withRequest(string $method, string $path): void; +} diff --git a/src/PhpPact/Consumer/Driver/Interaction/Part/ResponseDriver.php b/src/PhpPact/Consumer/Driver/Interaction/Part/ResponseDriver.php new file mode 100644 index 00000000..481fd00d --- /dev/null +++ b/src/PhpPact/Consumer/Driver/Interaction/Part/ResponseDriver.php @@ -0,0 +1,16 @@ +client->call('pactffi_response_status', $this->getInteractionId(), $status); + } + + protected function getPart(): int + { + return $this->client->get('InteractionPart_Response'); + } +} diff --git a/src/PhpPact/Consumer/Driver/Interaction/Part/ResponseDriverInterface.php b/src/PhpPact/Consumer/Driver/Interaction/Part/ResponseDriverInterface.php new file mode 100644 index 00000000..71e5a62c --- /dev/null +++ b/src/PhpPact/Consumer/Driver/Interaction/Part/ResponseDriverInterface.php @@ -0,0 +1,8 @@ +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; } @@ -75,10 +79,10 @@ private function uponReceiving(Interaction $interaction): self 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; } @@ -86,9 +90,9 @@ private function with(Interaction $interaction): self 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; }