Skip to content

Commit

Permalink
Merge pull request #494 from tienvx/refactor-pact-driver
Browse files Browse the repository at this point in the history
refactor: Merge PactDriver and PactRegistry
  • Loading branch information
tienvx committed Feb 29, 2024
2 parents bcee663 + e876d5e commit 0795996
Show file tree
Hide file tree
Showing 32 changed files with 385 additions and 165 deletions.
7 changes: 2 additions & 5 deletions compatibility-suite/tests/Service/PactWriter.php
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
9 changes: 3 additions & 6 deletions compatibility-suite/tests/Service/Server.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
7 changes: 2 additions & 5 deletions compatibility-suite/tests/Service/SyncMessagePactWriter.php
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions src/PhpPact/Consumer/Driver/Exception/DriverException.php
@@ -0,0 +1,9 @@
<?php

namespace PhpPact\Consumer\Driver\Exception;

use PhpPact\Consumer\Exception\ConsumerException;

class DriverException extends ConsumerException
{
}
@@ -0,0 +1,7 @@
<?php

namespace PhpPact\Consumer\Driver\Exception;

class MissingPactException extends DriverException
{
}
3 changes: 0 additions & 3 deletions src/PhpPact/Consumer/Driver/Interaction/InteractionDriver.php
Expand Up @@ -2,15 +2,13 @@

namespace PhpPact\Consumer\Driver\Interaction;

use PhpPact\Consumer\Driver\Pact\PactDriverInterface;
use PhpPact\Consumer\Model\Interaction;
use PhpPact\Consumer\Registry\Interaction\InteractionRegistryInterface;
use PhpPact\Consumer\Service\MockServerInterface;

class InteractionDriver implements InteractionDriverInterface
{
public function __construct(
private PactDriverInterface $pactDriver,
private InteractionRegistryInterface $interactionRegistry,
private MockServerInterface $mockServer,
) {
Expand All @@ -23,7 +21,6 @@ public function verifyInteractions(): bool

public function registerInteraction(Interaction $interaction): bool
{
$this->pactDriver->setUp();
$this->interactionRegistry->registerInteraction($interaction);
$this->mockServer->start();

Expand Down
1 change: 0 additions & 1 deletion src/PhpPact/Consumer/Driver/Interaction/MessageDriver.php
Expand Up @@ -31,7 +31,6 @@ public function writePactAndCleanUp(): bool

public function registerMessage(Message $message): void
{
$this->pactDriver->setUp();
$this->messageRegistry->registerMessage($message);
}
}
55 changes: 36 additions & 19 deletions src/PhpPact/Consumer/Driver/Pact/PactDriver.php
Expand Up @@ -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
);
Expand All @@ -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
Expand All @@ -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());
}
}
6 changes: 4 additions & 2 deletions src/PhpPact/Consumer/Driver/Pact/PactDriverInterface.php
Expand Up @@ -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;
}
10 changes: 4 additions & 6 deletions src/PhpPact/Consumer/Factory/InteractionDriverFactory.php
Expand Up @@ -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;
Expand All @@ -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);
}
}
6 changes: 2 additions & 4 deletions src/PhpPact/Consumer/Factory/MessageDriverFactory.php
Expand Up @@ -7,17 +7,15 @@
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
{
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);
}
Expand Down
10 changes: 10 additions & 0 deletions src/PhpPact/Consumer/Model/Pact/Pact.php
@@ -0,0 +1,10 @@
<?php

namespace PhpPact\Consumer\Model\Pact;

class Pact
{
public function __construct(public readonly int $handle)
{
}
}
Expand Up @@ -2,7 +2,7 @@

namespace PhpPact\Consumer\Registry\Interaction;

use PhpPact\Consumer\Registry\Pact\PactRegistryInterface;
use PhpPact\Consumer\Driver\Pact\PactDriverInterface;
use PhpPact\FFI\ClientInterface;

abstract class AbstractRegistry implements RegistryInterface
Expand All @@ -11,7 +11,7 @@ abstract class AbstractRegistry implements RegistryInterface

public function __construct(
protected ClientInterface $client,
protected PactRegistryInterface $pactRegistry
protected PactDriverInterface $pactDriver
) {
}

Expand Down
Expand Up @@ -2,11 +2,11 @@

namespace PhpPact\Consumer\Registry\Interaction;

use PhpPact\Consumer\Driver\Pact\PactDriverInterface;
use PhpPact\Consumer\Registry\Interaction\Part\RequestRegistry;
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\Consumer\Model\ConsumerRequest;
use PhpPact\Consumer\Model\Interaction;
use PhpPact\Consumer\Model\ProviderResponse;
Expand All @@ -20,11 +20,11 @@ class InteractionRegistry extends AbstractRegistry implements InteractionRegistr

public function __construct(
ClientInterface $client,
PactRegistryInterface $pactRegistry,
PactDriverInterface $pactDriver,
?RequestRegistryInterface $requestRegistry = null,
?ResponseRegistryInterface $responseRegistry = null,
) {
parent::__construct($client, $pactRegistry);
parent::__construct($client, $pactDriver);
$this->requestRegistry = $requestRegistry ?? new RequestRegistry($client, $this);
$this->responseRegistry = $responseRegistry ?? new ResponseRegistry($client, $this);
}
Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/PhpPact/Consumer/Registry/Interaction/MessageRegistry.php
Expand Up @@ -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
Expand All @@ -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);
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 0795996

Please sign in to comment.