diff --git a/compatibility-suite/tests/Service/PactWriter.php b/compatibility-suite/tests/Service/PactWriter.php index e56a2a04..e0cc230a 100644 --- a/compatibility-suite/tests/Service/PactWriter.php +++ b/compatibility-suite/tests/Service/PactWriter.php @@ -5,7 +5,6 @@ use PhpPact\Config\PactConfigInterface; use PhpPact\Consumer\Driver\Pact\PactDriver; use PhpPact\Consumer\Registry\Interaction\InteractionRegistry; -use PhpPact\Consumer\Registry\Pact\PactRegistry; use PhpPact\FFI\Client; use PhpPact\Standalone\MockService\MockServerConfig; use PhpPactTest\CompatibilitySuite\Constant\Path; @@ -29,12 +28,10 @@ public function write(int $id, PactPath $pactPath, string $mode = PactConfigInte ->setPactSpecificationVersion($this->specificationVersion) ->setPactFileWriteMode($mode); $client = new Client(); - $pactRegistry = new PactRegistry($client); - $pactDriver = new PactDriver($client, $config, $pactRegistry); - $interactionRegistry = new InteractionRegistry($client, $pactRegistry); + $pactDriver = new PactDriver($client, $config); + $interactionRegistry = new InteractionRegistry($client, $pactDriver); $interaction = $this->storage->get(InteractionsStorageInterface::PACT_WRITER_DOMAIN, $id); - $pactDriver->setUp(); $interactionRegistry->registerInteraction($interaction); $pactDriver->writePact(); $pactDriver->cleanUp(); diff --git a/compatibility-suite/tests/Service/Server.php b/compatibility-suite/tests/Service/Server.php index 6f7d90cf..885cd2cd 100644 --- a/compatibility-suite/tests/Service/Server.php +++ b/compatibility-suite/tests/Service/Server.php @@ -7,7 +7,6 @@ use PhpPact\Consumer\Driver\Pact\PactDriverInterface; use PhpPact\Consumer\Registry\Interaction\InteractionRegistry; use PhpPact\Consumer\Registry\Interaction\InteractionRegistryInterface; -use PhpPact\Consumer\Registry\Pact\PactRegistry; use PhpPact\Consumer\Service\MockServer; use PhpPact\Consumer\Service\MockServerInterface; use PhpPact\FFI\Client; @@ -45,16 +44,14 @@ public function __construct( $this->logger = new Logger(); $client = new Client(); - $pactRegistry = new PactRegistry($client); - $this->pactDriver = new PactDriver($client, $this->config, $pactRegistry); - $this->mockServer = new MockServer($client, $pactRegistry, $this->config, $this->logger); - $this->interactionRegistry = new InteractionRegistry($client, $pactRegistry); + $this->pactDriver = new PactDriver($client, $this->config); + $this->mockServer = new MockServer($client, $this->pactDriver, $this->config, $this->logger); + $this->interactionRegistry = new InteractionRegistry($client, $this->pactDriver); } public function register(int ...$ids): void { $interactions = array_map(fn (int $id) => $this->storage->get(InteractionsStorageInterface::SERVER_DOMAIN, $id), $ids); - $this->pactDriver->setUp(); foreach ($interactions as $interaction) { $this->interactionRegistry->registerInteraction($interaction); } diff --git a/compatibility-suite/tests/Service/SyncMessagePactWriter.php b/compatibility-suite/tests/Service/SyncMessagePactWriter.php index f7b2b923..1ef9a9d9 100644 --- a/compatibility-suite/tests/Service/SyncMessagePactWriter.php +++ b/compatibility-suite/tests/Service/SyncMessagePactWriter.php @@ -5,7 +5,6 @@ use PhpPact\Config\PactConfigInterface; use PhpPact\Consumer\Driver\Pact\PactDriver; use PhpPact\Consumer\Model\Message; -use PhpPact\Consumer\Registry\Pact\PactRegistry; use PhpPact\FFI\Client; use PhpPact\Standalone\MockService\MockServerConfig; use PhpPact\SyncMessage\Registry\Interaction\SyncMessageRegistry; @@ -29,11 +28,9 @@ public function write(Message $message, PactPath $pactPath, string $mode = PactC ->setPactSpecificationVersion($this->specificationVersion) ->setPactFileWriteMode($mode); $client = new Client(); - $pactRegistry = new PactRegistry($client); - $pactDriver = new PactDriver($client, $config, $pactRegistry); - $messageRegistry = new SyncMessageRegistry($client, $pactRegistry); + $pactDriver = new PactDriver($client, $config); + $messageRegistry = new SyncMessageRegistry($client, $pactDriver); - $pactDriver->setUp(); $messageRegistry->registerMessage($message); $pactDriver->writePact(); $pactDriver->cleanUp(); diff --git a/src/PhpPact/Consumer/Driver/Exception/DriverException.php b/src/PhpPact/Consumer/Driver/Exception/DriverException.php new file mode 100644 index 00000000..3b8b4932 --- /dev/null +++ b/src/PhpPact/Consumer/Driver/Exception/DriverException.php @@ -0,0 +1,9 @@ +pactDriver->setUp(); $this->interactionRegistry->registerInteraction($interaction); $this->mockServer->start(); diff --git a/src/PhpPact/Consumer/Driver/Interaction/MessageDriver.php b/src/PhpPact/Consumer/Driver/Interaction/MessageDriver.php index 6ba771ff..80a30661 100644 --- a/src/PhpPact/Consumer/Driver/Interaction/MessageDriver.php +++ b/src/PhpPact/Consumer/Driver/Interaction/MessageDriver.php @@ -31,7 +31,6 @@ public function writePactAndCleanUp(): bool public function registerMessage(Message $message): void { - $this->pactDriver->setUp(); $this->messageRegistry->registerMessage($message); } } diff --git a/src/PhpPact/Consumer/Driver/Pact/PactDriver.php b/src/PhpPact/Consumer/Driver/Pact/PactDriver.php index 427d81ba..a7baf923 100644 --- a/src/PhpPact/Consumer/Driver/Pact/PactDriver.php +++ b/src/PhpPact/Consumer/Driver/Pact/PactDriver.php @@ -4,29 +4,35 @@ use Composer\Semver\Comparator; use PhpPact\Config\PactConfigInterface; +use PhpPact\Consumer\Driver\Exception\MissingPactException; use PhpPact\Consumer\Exception\PactFileNotWroteException; -use PhpPact\Consumer\Registry\Pact\PactRegistryInterface; +use PhpPact\Consumer\Model\Pact\Pact; use PhpPact\FFI\ClientInterface; class PactDriver implements PactDriverInterface { + protected ?Pact $pact = null; + public function __construct( protected ClientInterface $client, - protected PactConfigInterface $config, - protected PactRegistryInterface $pactRegistry + protected PactConfigInterface $config ) { + $this->setUp(); } public function cleanUp(): void { - $this->pactRegistry->deletePact(); + $this->validatePact(); + $this->client->call('pactffi_free_pact_handle', $this->pact->handle); + $this->pact = null; } public function writePact(): void { + $this->validatePact(); $error = $this->client->call( 'pactffi_pact_handle_write_file', - $this->pactRegistry->getId(), + $this->pact->handle, $this->config->getPactDir(), $this->config->getPactFileWriteMode() === PactConfigInterface::MODE_OVERWRITE ); @@ -35,11 +41,18 @@ public function writePact(): void } } - public function setUp(): void + public function getPact(): Pact { - $this - ->initWithLogLevel() - ->registerPact(); + $this->validatePact(); + + return $this->pact; + } + + protected function setUp(): void + { + $this->initWithLogLevel(); + $this->newPact(); + $this->withSpecification(); } protected function getSpecification(): int @@ -58,29 +71,33 @@ protected function getSpecification(): int }; } + protected function validatePact(): void + { + if (!$this->pact) { + throw new MissingPactException(); + } + } + private function versionEqualTo(string $version): bool { return Comparator::equalTo($this->config->getPactSpecificationVersion(), $version); } - private function initWithLogLevel(): self + private function initWithLogLevel(): void { $logLevel = $this->config->getLogLevel(); if ($logLevel) { $this->client->call('pactffi_init_with_log_level', $logLevel); } - - return $this; } - private function registerPact(): self + private function newPact(): void { - $this->pactRegistry->registerPact( - $this->config->getConsumer(), - $this->config->getProvider(), - $this->getSpecification() - ); + $this->pact = new Pact($this->client->call('pactffi_new_pact', $this->config->getConsumer(), $this->config->getProvider())); + } - return $this; + private function withSpecification(): void + { + $this->client->call('pactffi_with_specification', $this->pact->handle, $this->getSpecification()); } } diff --git a/src/PhpPact/Consumer/Driver/Pact/PactDriverInterface.php b/src/PhpPact/Consumer/Driver/Pact/PactDriverInterface.php index 69ccdc42..729d1140 100644 --- a/src/PhpPact/Consumer/Driver/Pact/PactDriverInterface.php +++ b/src/PhpPact/Consumer/Driver/Pact/PactDriverInterface.php @@ -2,11 +2,13 @@ namespace PhpPact\Consumer\Driver\Pact; +use PhpPact\Consumer\Model\Pact\Pact; + interface PactDriverInterface { - public function setUp(): void; - public function cleanUp(): void; public function writePact(): void; + + public function getPact(): Pact; } diff --git a/src/PhpPact/Consumer/Factory/InteractionDriverFactory.php b/src/PhpPact/Consumer/Factory/InteractionDriverFactory.php index f911c5fc..18a7d83e 100644 --- a/src/PhpPact/Consumer/Factory/InteractionDriverFactory.php +++ b/src/PhpPact/Consumer/Factory/InteractionDriverFactory.php @@ -6,7 +6,6 @@ use PhpPact\Consumer\Driver\Interaction\InteractionDriverInterface; use PhpPact\Consumer\Driver\Pact\PactDriver; use PhpPact\Consumer\Registry\Interaction\InteractionRegistry; -use PhpPact\Consumer\Registry\Pact\PactRegistry; use PhpPact\FFI\Client; use PhpPact\Standalone\MockService\MockServerConfigInterface; use PhpPact\Consumer\Service\MockServer; @@ -16,11 +15,10 @@ class InteractionDriverFactory implements InteractionDriverFactoryInterface public function create(MockServerConfigInterface $config): InteractionDriverInterface { $client = new Client(); - $pactRegistry = new PactRegistry($client); - $pactDriver = new PactDriver($client, $config, $pactRegistry); - $mockServer = new MockServer($client, $pactRegistry, $config); - $interactionRegistry = new InteractionRegistry($client, $pactRegistry); + $pactDriver = new PactDriver($client, $config); + $mockServer = new MockServer($client, $pactDriver, $config); + $interactionRegistry = new InteractionRegistry($client, $pactDriver); - return new InteractionDriver($pactDriver, $interactionRegistry, $mockServer); + return new InteractionDriver($interactionRegistry, $mockServer); } } diff --git a/src/PhpPact/Consumer/Factory/MessageDriverFactory.php b/src/PhpPact/Consumer/Factory/MessageDriverFactory.php index af8a5dac..e8a90ac0 100644 --- a/src/PhpPact/Consumer/Factory/MessageDriverFactory.php +++ b/src/PhpPact/Consumer/Factory/MessageDriverFactory.php @@ -7,7 +7,6 @@ use PhpPact\Consumer\Driver\Pact\PactDriver; use PhpPact\Config\PactConfigInterface; use PhpPact\Consumer\Registry\Interaction\MessageRegistry; -use PhpPact\Consumer\Registry\Pact\PactRegistry; use PhpPact\FFI\Client; class MessageDriverFactory implements MessageDriverFactoryInterface @@ -15,9 +14,8 @@ class MessageDriverFactory implements MessageDriverFactoryInterface public function create(PactConfigInterface $config): MessageDriverInterface { $client = new Client(); - $pactRegistry = new PactRegistry($client); - $pactDriver = new PactDriver($client, $config, $pactRegistry); - $messageRegistry = new MessageRegistry($client, $pactRegistry); + $pactDriver = new PactDriver($client, $config); + $messageRegistry = new MessageRegistry($client, $pactDriver); return new MessageDriver($client, $pactDriver, $messageRegistry); } diff --git a/src/PhpPact/Consumer/Model/Pact/Pact.php b/src/PhpPact/Consumer/Model/Pact/Pact.php new file mode 100644 index 00000000..dfe3ec75 --- /dev/null +++ b/src/PhpPact/Consumer/Model/Pact/Pact.php @@ -0,0 +1,10 @@ +requestRegistry = $requestRegistry ?? new RequestRegistry($client, $this); $this->responseRegistry = $responseRegistry ?? new ResponseRegistry($client, $this); } @@ -43,7 +43,7 @@ public function registerInteraction(Interaction $interaction): bool protected function newInteraction(string $description): self { - $this->id = $this->client->call('pactffi_new_interaction', $this->pactRegistry->getId(), $description); + $this->id = $this->client->call('pactffi_new_interaction', $this->pactDriver->getPact()->handle, $description); return $this; } diff --git a/src/PhpPact/Consumer/Registry/Interaction/MessageRegistry.php b/src/PhpPact/Consumer/Registry/Interaction/MessageRegistry.php index fa1f693b..021fab3f 100644 --- a/src/PhpPact/Consumer/Registry/Interaction/MessageRegistry.php +++ b/src/PhpPact/Consumer/Registry/Interaction/MessageRegistry.php @@ -2,13 +2,13 @@ namespace PhpPact\Consumer\Registry\Interaction; +use PhpPact\Consumer\Driver\Pact\PactDriverInterface; use PhpPact\Consumer\Model\Body\Binary; use PhpPact\Consumer\Model\Body\Text; use PhpPact\Consumer\Model\Message; use PhpPact\Consumer\Model\ProviderState; use PhpPact\Consumer\Registry\Interaction\Body\BodyRegistryInterface; use PhpPact\Consumer\Registry\Interaction\Body\MessageContentsRegistry; -use PhpPact\Consumer\Registry\Pact\PactRegistryInterface; use PhpPact\FFI\ClientInterface; class MessageRegistry extends AbstractRegistry implements MessageRegistryInterface @@ -17,10 +17,10 @@ class MessageRegistry extends AbstractRegistry implements MessageRegistryInterfa public function __construct( ClientInterface $client, - PactRegistryInterface $pactRegistry, + PactDriverInterface $pactDriver, ?BodyRegistryInterface $messageContentsRegistry = null ) { - parent::__construct($client, $pactRegistry); + parent::__construct($client, $pactDriver); $this->messageContentsRegistry = $messageContentsRegistry ?? new MessageContentsRegistry($client, $this); } @@ -37,7 +37,7 @@ public function registerMessage(Message $message): void protected function newInteraction(string $description): self { - $this->id = $this->client->call('pactffi_new_message_interaction', $this->pactRegistry->getId(), $description); + $this->id = $this->client->call('pactffi_new_message_interaction', $this->pactDriver->getPact()->handle, $description); return $this; } diff --git a/src/PhpPact/Consumer/Registry/Pact/PactRegistry.php b/src/PhpPact/Consumer/Registry/Pact/PactRegistry.php deleted file mode 100644 index 138b4d1c..00000000 --- a/src/PhpPact/Consumer/Registry/Pact/PactRegistry.php +++ /dev/null @@ -1,50 +0,0 @@ -id)) { - throw new PactNotRegisteredException('New pact must be registered.'); - } - return $this->id; - } - - public function deletePact(): void - { - $this->client->call('pactffi_free_pact_handle', $this->id); - unset($this->id); - } - - public function registerPact(string $consumer, string $provider, int $specification): void - { - $this - ->newPact($consumer, $provider) - ->withSpecification($specification); - } - - private function newPact(string $consumer, string $provider): self - { - $this->id = $this->client->call('pactffi_new_pact', $consumer, $provider); - - return $this; - } - - private function withSpecification(int $specification): self - { - $this->client->call('pactffi_with_specification', $this->id, $specification); - - return $this; - } -} diff --git a/src/PhpPact/Consumer/Registry/Pact/PactRegistryInterface.php b/src/PhpPact/Consumer/Registry/Pact/PactRegistryInterface.php deleted file mode 100644 index c9ffb883..00000000 --- a/src/PhpPact/Consumer/Registry/Pact/PactRegistryInterface.php +++ /dev/null @@ -1,12 +0,0 @@ -client->call( 'pactffi_create_mock_server_for_transport', - $this->pactRegistry->getId(), + $this->pactDriver->getPact()->handle, $this->config->getHost(), $this->config->getPort(), $this->getTransport(), @@ -81,7 +81,7 @@ private function writePact(): void private function cleanUp(): void { $this->client->call('pactffi_cleanup_mock_server', $this->config->getPort()); - $this->pactRegistry->deletePact(); + $this->pactDriver->cleanUp(); } private function isMatched(): bool diff --git a/src/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriver.php b/src/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriver.php index 75d490d7..7844f5a3 100644 --- a/src/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriver.php +++ b/src/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriver.php @@ -9,11 +9,12 @@ abstract class AbstractPluginPactDriver extends PactDriver { public function cleanUp(): void { - $this->client->call('pactffi_cleanup_plugins', $this->pactRegistry->getId()); + $this->validatePact(); + $this->client->call('pactffi_cleanup_plugins', $this->pact->handle); parent::cleanUp(); } - public function setUp(): void + protected function setUp(): void { parent::setUp(); $this->usingPlugin(); @@ -32,7 +33,7 @@ private function usingPlugin(): self throw new PluginNotSupportedBySpecificationException($this->config->getPactSpecificationVersion()); } - $this->client->call('pactffi_using_plugin', $this->pactRegistry->getId(), $this->getPluginName(), $this->getPluginVersion()); + $this->client->call('pactffi_using_plugin', $this->pact->handle, $this->getPluginName(), $this->getPluginVersion()); return $this; } diff --git a/src/PhpPact/Plugins/Csv/Factory/CsvInteractionDriverFactory.php b/src/PhpPact/Plugins/Csv/Factory/CsvInteractionDriverFactory.php index 1546638b..d9d9cb0e 100644 --- a/src/PhpPact/Plugins/Csv/Factory/CsvInteractionDriverFactory.php +++ b/src/PhpPact/Plugins/Csv/Factory/CsvInteractionDriverFactory.php @@ -5,7 +5,6 @@ use PhpPact\Consumer\Driver\Interaction\InteractionDriver; use PhpPact\Consumer\Driver\Interaction\InteractionDriverInterface; use PhpPact\Consumer\Factory\InteractionDriverFactoryInterface; -use PhpPact\Consumer\Registry\Pact\PactRegistry; use PhpPact\FFI\Client; use PhpPact\Standalone\MockService\MockServerConfigInterface; use PhpPact\Consumer\Service\MockServer; @@ -17,11 +16,10 @@ class CsvInteractionDriverFactory implements InteractionDriverFactoryInterface public function create(MockServerConfigInterface $config): InteractionDriverInterface { $client = new Client(); - $pactRegistry = new PactRegistry($client); - $pactDriver = new CsvPactDriver($client, $config, $pactRegistry); - $mockServer = new MockServer($client, $pactRegistry, $config); - $interactionRegistry = new CsvInteractionRegistry($client, $pactRegistry); + $pactDriver = new CsvPactDriver($client, $config); + $mockServer = new MockServer($client, $pactDriver, $config); + $interactionRegistry = new CsvInteractionRegistry($client, $pactDriver); - return new InteractionDriver($pactDriver, $interactionRegistry, $mockServer); + return new InteractionDriver($interactionRegistry, $mockServer); } } diff --git a/src/PhpPact/Plugins/Csv/Registry/Interaction/CsvInteractionRegistry.php b/src/PhpPact/Plugins/Csv/Registry/Interaction/CsvInteractionRegistry.php index 615d50c7..2801470f 100644 --- a/src/PhpPact/Plugins/Csv/Registry/Interaction/CsvInteractionRegistry.php +++ b/src/PhpPact/Plugins/Csv/Registry/Interaction/CsvInteractionRegistry.php @@ -2,11 +2,11 @@ namespace PhpPact\Plugins\Csv\Registry\Interaction; +use PhpPact\Consumer\Driver\Pact\PactDriverInterface; use PhpPact\Consumer\Registry\Interaction\InteractionRegistry; use PhpPact\Consumer\Registry\Interaction\Part\RequestRegistryInterface; use PhpPact\Consumer\Registry\Interaction\Part\ResponseRegistry; use PhpPact\Consumer\Registry\Interaction\Part\ResponseRegistryInterface; -use PhpPact\Consumer\Registry\Pact\PactRegistryInterface; use PhpPact\FFI\ClientInterface; use PhpPact\Plugins\Csv\Registry\Interaction\Body\CsvResponseBodyRegistry; @@ -14,11 +14,11 @@ class CsvInteractionRegistry extends InteractionRegistry { public function __construct( ClientInterface $client, - PactRegistryInterface $pactRegistry, + PactDriverInterface $pactDriver, ?RequestRegistryInterface $requestRegistry = null, ?ResponseRegistryInterface $responseRegistry = null, ) { $responseRegistry = $responseRegistry ?? new ResponseRegistry($client, $this, new CsvResponseBodyRegistry($client, $this)); - parent::__construct($client, $pactRegistry, $requestRegistry, $responseRegistry); + parent::__construct($client, $pactDriver, $requestRegistry, $responseRegistry); } } diff --git a/src/PhpPact/Plugins/Protobuf/Factory/ProtobufMessageDriverFactory.php b/src/PhpPact/Plugins/Protobuf/Factory/ProtobufMessageDriverFactory.php index b07f1d58..8bb3dc87 100644 --- a/src/PhpPact/Plugins/Protobuf/Factory/ProtobufMessageDriverFactory.php +++ b/src/PhpPact/Plugins/Protobuf/Factory/ProtobufMessageDriverFactory.php @@ -6,7 +6,6 @@ use PhpPact\Consumer\Driver\Interaction\MessageDriver; use PhpPact\Consumer\Driver\Interaction\MessageDriverInterface; use PhpPact\Consumer\Factory\MessageDriverFactoryInterface; -use PhpPact\Consumer\Registry\Pact\PactRegistry; use PhpPact\FFI\Client; use PhpPact\Plugins\Protobuf\Driver\Pact\ProtobufPactDriver; use PhpPact\Plugins\Protobuf\Registry\Interaction\ProtobufMessageRegistry; @@ -16,9 +15,8 @@ class ProtobufMessageDriverFactory implements MessageDriverFactoryInterface public function create(PactConfigInterface $config): MessageDriverInterface { $client = new Client(); - $pactRegistry = new PactRegistry($client); - $pactDriver = new ProtobufPactDriver($client, $config, $pactRegistry); - $messageRegistry = new ProtobufMessageRegistry($client, $pactRegistry); + $pactDriver = new ProtobufPactDriver($client, $config); + $messageRegistry = new ProtobufMessageRegistry($client, $pactDriver); return new MessageDriver($client, $pactDriver, $messageRegistry); } diff --git a/src/PhpPact/Plugins/Protobuf/Factory/ProtobufSyncMessageDriverFactory.php b/src/PhpPact/Plugins/Protobuf/Factory/ProtobufSyncMessageDriverFactory.php index 79ee8eea..00ebba0b 100644 --- a/src/PhpPact/Plugins/Protobuf/Factory/ProtobufSyncMessageDriverFactory.php +++ b/src/PhpPact/Plugins/Protobuf/Factory/ProtobufSyncMessageDriverFactory.php @@ -2,7 +2,6 @@ namespace PhpPact\Plugins\Protobuf\Factory; -use PhpPact\Consumer\Registry\Pact\PactRegistry; use PhpPact\FFI\Client; use PhpPact\Plugins\Protobuf\Driver\Pact\ProtobufPactDriver; use PhpPact\Plugins\Protobuf\Registry\Interaction\ProtobufSyncMessageRegistry; @@ -17,11 +16,10 @@ class ProtobufSyncMessageDriverFactory implements SyncMessageDriverFactoryInterf public function create(MockServerConfigInterface $config): SyncMessageDriverInterface { $client = new Client(); - $pactRegistry = new PactRegistry($client); - $pactDriver = new ProtobufPactDriver($client, $config, $pactRegistry); - $grpcMockServer = new GrpcMockServer($client, $pactRegistry, $config); - $syncMessageRegistry = new ProtobufSyncMessageRegistry($client, $pactRegistry); + $pactDriver = new ProtobufPactDriver($client, $config); + $grpcMockServer = new GrpcMockServer($client, $pactDriver, $config); + $syncMessageRegistry = new ProtobufSyncMessageRegistry($client, $pactDriver); - return new SyncMessageDriver($pactDriver, $syncMessageRegistry, $grpcMockServer); + return new SyncMessageDriver($syncMessageRegistry, $grpcMockServer); } } diff --git a/src/PhpPact/Plugins/Protobuf/Registry/Interaction/ProtobufMessageRegistry.php b/src/PhpPact/Plugins/Protobuf/Registry/Interaction/ProtobufMessageRegistry.php index e8134b81..2a3ac0f3 100644 --- a/src/PhpPact/Plugins/Protobuf/Registry/Interaction/ProtobufMessageRegistry.php +++ b/src/PhpPact/Plugins/Protobuf/Registry/Interaction/ProtobufMessageRegistry.php @@ -2,9 +2,9 @@ namespace PhpPact\Plugins\Protobuf\Registry\Interaction; +use PhpPact\Consumer\Driver\Pact\PactDriverInterface; use PhpPact\Consumer\Registry\Interaction\Body\BodyRegistryInterface; use PhpPact\Consumer\Registry\Interaction\MessageRegistry; -use PhpPact\Consumer\Registry\Pact\PactRegistryInterface; use PhpPact\FFI\ClientInterface; use PhpPact\Plugins\Protobuf\Registry\Interaction\Body\ProtobufMessageContentsRegistry; @@ -12,9 +12,9 @@ class ProtobufMessageRegistry extends MessageRegistry { public function __construct( ClientInterface $client, - PactRegistryInterface $pactRegistry, + PactDriverInterface $pactDriver, ?BodyRegistryInterface $messageContentsRegistry = null ) { - parent::__construct($client, $pactRegistry, $messageContentsRegistry ?? new ProtobufMessageContentsRegistry($client, $this)); + parent::__construct($client, $pactDriver, $messageContentsRegistry ?? new ProtobufMessageContentsRegistry($client, $this)); } } diff --git a/src/PhpPact/Plugins/Protobuf/Registry/Interaction/ProtobufSyncMessageRegistry.php b/src/PhpPact/Plugins/Protobuf/Registry/Interaction/ProtobufSyncMessageRegistry.php index fca59408..aa32d8a5 100644 --- a/src/PhpPact/Plugins/Protobuf/Registry/Interaction/ProtobufSyncMessageRegistry.php +++ b/src/PhpPact/Plugins/Protobuf/Registry/Interaction/ProtobufSyncMessageRegistry.php @@ -2,8 +2,8 @@ namespace PhpPact\Plugins\Protobuf\Registry\Interaction; +use PhpPact\Consumer\Driver\Pact\PactDriverInterface; use PhpPact\Consumer\Registry\Interaction\Body\BodyRegistryInterface; -use PhpPact\Consumer\Registry\Pact\PactRegistryInterface; use PhpPact\FFI\ClientInterface; use PhpPact\Plugins\Protobuf\Registry\Interaction\Body\ProtobufMessageContentsRegistry; use PhpPact\SyncMessage\Registry\Interaction\SyncMessageRegistry; @@ -12,9 +12,9 @@ class ProtobufSyncMessageRegistry extends SyncMessageRegistry { public function __construct( ClientInterface $client, - PactRegistryInterface $pactRegistry, + PactDriverInterface $pactDriver, ?BodyRegistryInterface $messageContentsRegistry = null ) { - parent::__construct($client, $pactRegistry, $messageContentsRegistry ?? new ProtobufMessageContentsRegistry($client, $this)); + parent::__construct($client, $pactDriver, $messageContentsRegistry ?? new ProtobufMessageContentsRegistry($client, $this)); } } diff --git a/src/PhpPact/SyncMessage/Driver/Interaction/SyncMessageDriver.php b/src/PhpPact/SyncMessage/Driver/Interaction/SyncMessageDriver.php index a0260adc..f46735bb 100644 --- a/src/PhpPact/SyncMessage/Driver/Interaction/SyncMessageDriver.php +++ b/src/PhpPact/SyncMessage/Driver/Interaction/SyncMessageDriver.php @@ -2,7 +2,6 @@ namespace PhpPact\SyncMessage\Driver\Interaction; -use PhpPact\Consumer\Driver\Pact\PactDriverInterface; use PhpPact\Consumer\Model\Message; use PhpPact\Consumer\Registry\Interaction\MessageRegistryInterface; use PhpPact\Consumer\Service\MockServerInterface; @@ -10,7 +9,6 @@ class SyncMessageDriver implements SyncMessageDriverInterface { public function __construct( - private PactDriverInterface $pactDriver, private MessageRegistryInterface $messageRegistry, private MockServerInterface $mockServer ) { @@ -23,7 +21,6 @@ public function verifyMessage(): bool public function registerMessage(Message $message): void { - $this->pactDriver->setUp(); $this->messageRegistry->registerMessage($message); $this->mockServer->start(); diff --git a/src/PhpPact/SyncMessage/Factory/SyncMessageDriverFactory.php b/src/PhpPact/SyncMessage/Factory/SyncMessageDriverFactory.php index 8b9c116b..49ed97ca 100644 --- a/src/PhpPact/SyncMessage/Factory/SyncMessageDriverFactory.php +++ b/src/PhpPact/SyncMessage/Factory/SyncMessageDriverFactory.php @@ -3,7 +3,6 @@ namespace PhpPact\SyncMessage\Factory; use PhpPact\Consumer\Driver\Pact\PactDriver; -use PhpPact\Consumer\Registry\Pact\PactRegistry; use PhpPact\Consumer\Service\MockServer; use PhpPact\FFI\Client; use PhpPact\SyncMessage\Driver\Interaction\SyncMessageDriver; @@ -16,11 +15,10 @@ class SyncMessageDriverFactory implements SyncMessageDriverFactoryInterface public function create(MockServerConfigInterface $config): SyncMessageDriverInterface { $client = new Client(); - $pactRegistry = new PactRegistry($client); - $pactDriver = new PactDriver($client, $config, $pactRegistry); - $messageRegistry = new SyncMessageRegistry($client, $pactRegistry); - $mockServer = new MockServer($client, $pactRegistry, $config); + $pactDriver = new PactDriver($client, $config); + $messageRegistry = new SyncMessageRegistry($client, $pactDriver); + $mockServer = new MockServer($client, $pactDriver, $config); - return new SyncMessageDriver($pactDriver, $messageRegistry, $mockServer); + return new SyncMessageDriver($messageRegistry, $mockServer); } } diff --git a/src/PhpPact/SyncMessage/Registry/Interaction/SyncMessageRegistry.php b/src/PhpPact/SyncMessage/Registry/Interaction/SyncMessageRegistry.php index 1f22d653..364747c2 100644 --- a/src/PhpPact/SyncMessage/Registry/Interaction/SyncMessageRegistry.php +++ b/src/PhpPact/SyncMessage/Registry/Interaction/SyncMessageRegistry.php @@ -9,7 +9,7 @@ class SyncMessageRegistry extends MessageRegistry { protected function newInteraction(string $description): self { - $this->id = $this->client->call('pactffi_new_sync_message_interaction', $this->pactRegistry->getId(), $description); + $this->id = $this->client->call('pactffi_new_sync_message_interaction', $this->pactDriver->getPact()->handle, $description); return $this; } diff --git a/tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php b/tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php new file mode 100644 index 00000000..9dfd54a9 --- /dev/null +++ b/tests/PhpPact/Consumer/Driver/Pact/PactDriverTest.php @@ -0,0 +1,169 @@ +client = $this->createMock(ClientInterface::class); + $this->config = $this->createMock(PactConfigInterface::class); + $this->client + ->expects($this->any()) + ->method('get') + ->willReturnMap([ + ['PactSpecification_Unknown', self::SPEC_UNKNOWN], + ['PactSpecification_V1', self::SPEC_V1], + ['PactSpecification_V1_1', self::SPEC_V1_1], + ['PactSpecification_V2', self::SPEC_V2], + ['PactSpecification_V3', self::SPEC_V3], + ['PactSpecification_V4', self::SPEC_V4], + ]); + } + + #[TestWith([null , '1.0.0', self::SPEC_V1])] + #[TestWith(['trace', '1.1.0', self::SPEC_V1_1])] + #[TestWith(['debug', '2.0.0', self::SPEC_V2])] + #[TestWith(['info' , '3.0.0', self::SPEC_V3])] + #[TestWith(['warn' , '4.0.0', self::SPEC_V4])] + #[TestWith(['error', '1.0.0', self::SPEC_V1])] + #[TestWith(['off' , '1.1.0', self::SPEC_V1_1])] + #[TestWith(['none' , '2.0.0', self::SPEC_V2])] + public function testSetUp(?string $logLevel, string $version, int $specificationHandle): void + { + $this->assertConfig($logLevel, $version); + $calls = $logLevel ? [ + ['pactffi_init_with_log_level', $logLevel, null], + ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], + ['pactffi_with_specification', $this->pactHandle, $specificationHandle, null], + ] : [ + ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], + ['pactffi_with_specification', $this->pactHandle, $specificationHandle, null], + ]; + $this->assertClientCalls($calls); + $this->driver = new PactDriver($this->client, $this->config); + $this->assertSame($this->pactHandle, $this->driver->getPact()->handle); + } + + #[TestWith([false, false])] + #[TestWith([true, false])] + #[TestWith([false, true])] + public function testCleanUp(bool $getPactAfterCleanUp, bool $cleanUpAfterCleanUp): void + { + $this->assertConfig(null, '1.0.0'); + $calls = [ + ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], + ['pactffi_with_specification', $this->pactHandle, self::SPEC_V1, null], + ['pactffi_free_pact_handle', $this->pactHandle, null], + ]; + $this->assertClientCalls($calls); + $this->driver = new PactDriver($this->client, $this->config); + $this->driver->cleanUp(); + if ($getPactAfterCleanUp || $cleanUpAfterCleanUp) { + $this->expectException(MissingPactException::class); + if ($getPactAfterCleanUp) { + $this->driver->getPact(); + } else { + $this->driver->cleanUp(); + } + } + } + + #[TestWith([0, PactConfigInterface::MODE_OVERWRITE])] + #[TestWith([1, PactConfigInterface::MODE_OVERWRITE])] + #[TestWith([2, PactConfigInterface::MODE_OVERWRITE])] + #[TestWith([3, PactConfigInterface::MODE_OVERWRITE])] + #[TestWith([4, PactConfigInterface::MODE_OVERWRITE])] + #[TestWith([0, PactConfigInterface::MODE_MERGE])] + #[TestWith([1, PactConfigInterface::MODE_MERGE])] + #[TestWith([2, PactConfigInterface::MODE_MERGE])] + #[TestWith([3, PactConfigInterface::MODE_MERGE])] + #[TestWith([4, PactConfigInterface::MODE_MERGE])] + public function testWritePact(int $error, string $writeMode): void + { + $this->assertConfig(null, '1.0.0'); + $this->config + ->expects($this->once()) + ->method('getPactDir') + ->willReturn($this->pactDir); + $this->config + ->expects($this->once()) + ->method('getPactFileWriteMode') + ->willReturn($writeMode); + $calls = [ + ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], + ['pactffi_with_specification', $this->pactHandle, self::SPEC_V1, null], + ['pactffi_pact_handle_write_file', $this->pactHandle, $this->pactDir, $writeMode === PactConfigInterface::MODE_OVERWRITE, $error], + ]; + $this->assertClientCalls($calls); + $this->driver = new PactDriver($this->client, $this->config); + if ($error) { + $this->expectException(PactFileNotWroteException::class); + $this->expectExceptionMessage(match ($error) { + 1 => 'The function panicked.', + 2 => 'The pact file was not able to be written.', + 3 => 'The pact for the given handle was not found.', + default => 'Unknown error', + }); + } + $this->driver->writePact(); + } + + protected function assertConfig(?string $logLevel, string $version): void + { + $this->config + ->expects($this->once()) + ->method('getLogLevel') + ->willReturn($logLevel); + $this->config + ->expects($this->any()) + ->method('getPactSpecificationVersion') + ->willReturn($version); + $this->config + ->expects($this->once()) + ->method('getConsumer') + ->willReturn($this->consumer); + $this->config + ->expects($this->once()) + ->method('getProvider') + ->willReturn($this->provider); + } + + protected function assertClientCalls(array $calls): void + { + $this->client + ->expects($this->exactly(count($calls))) + ->method('call') + ->willReturnCallback(function (...$args) use (&$calls) { + $call = array_shift($calls); + $return = array_pop($call); + $this->assertSame($call, $args); + + return $return; + }); + } +} diff --git a/tests/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriverTestCase.php b/tests/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriverTestCase.php new file mode 100644 index 00000000..ebdaf972 --- /dev/null +++ b/tests/PhpPact/Plugin/Driver/Pact/AbstractPluginPactDriverTestCase.php @@ -0,0 +1,57 @@ +assertConfig(null, $version); + $calls = $supported ? [ + ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], + ['pactffi_with_specification', $this->pactHandle, $specificationHandle, null], + ['pactffi_using_plugin', $this->pactHandle, $this->getPluginName(), null, null], + ] : [ + ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], + ['pactffi_with_specification', $this->pactHandle, $specificationHandle, null], + ]; + $this->assertClientCalls($calls); + if (!$supported) { + $this->expectException(PluginNotSupportedBySpecificationException::class); + $this->expectExceptionMessage(sprintf( + 'Plugin is not supported by specification %s, use 4.0.0 or above', + $version, + )); + } + $this->driver = $this->createPactDriver(); + } + + public function testCleanUpPlugin(): void + { + $this->assertConfig(null, '4.0.0'); + $calls = [ + ['pactffi_new_pact', $this->consumer, $this->provider, $this->pactHandle], + ['pactffi_with_specification', $this->pactHandle, self::SPEC_V4, null], + ['pactffi_using_plugin', $this->pactHandle, $this->getPluginName(), null, null], + ['pactffi_cleanup_plugins', $this->pactHandle, null], + ['pactffi_free_pact_handle', $this->pactHandle, null], + ]; + $this->assertClientCalls($calls); + $this->driver = $this->createPactDriver(); + $this->driver->cleanUp(); + } + + abstract protected function createPactDriver(): AbstractPluginPactDriver; + + abstract protected function getPluginName(): string; +} diff --git a/tests/PhpPact/Plugins/Csv/Driver/Pact/CsvPactDriverTest.php b/tests/PhpPact/Plugins/Csv/Driver/Pact/CsvPactDriverTest.php new file mode 100644 index 00000000..dfa26d9f --- /dev/null +++ b/tests/PhpPact/Plugins/Csv/Driver/Pact/CsvPactDriverTest.php @@ -0,0 +1,19 @@ +client, $this->config); + } + + protected function getPluginName(): string + { + return 'csv'; + } +} diff --git a/tests/PhpPact/Plugins/Protobuf/Driver/Pact/ProtobufPactDriverTest.php b/tests/PhpPact/Plugins/Protobuf/Driver/Pact/ProtobufPactDriverTest.php new file mode 100644 index 00000000..ccb321eb --- /dev/null +++ b/tests/PhpPact/Plugins/Protobuf/Driver/Pact/ProtobufPactDriverTest.php @@ -0,0 +1,19 @@ +client, $this->config); + } + + protected function getPluginName(): string + { + return 'protobuf'; + } +}