From 83dfdb8339f9acd5a06f38987248d3f82bc330d5 Mon Sep 17 00:00:00 2001 From: tienvx Date: Tue, 21 Nov 2023 15:32:52 +0700 Subject: [PATCH] feat: Allow capture mock server mismatches --- src/PhpPact/Consumer/Service/MockServer.php | 27 +++++++++++++++---- src/PhpPact/Service/LoggerInterface.php | 8 ++++++ .../Model/VerifierLoggerInterface.php | 8 ------ .../Standalone/ProviderVerifier/Verifier.php | 4 +-- 4 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 src/PhpPact/Service/LoggerInterface.php delete mode 100644 src/PhpPact/Standalone/ProviderVerifier/Model/VerifierLoggerInterface.php diff --git a/src/PhpPact/Consumer/Service/MockServer.php b/src/PhpPact/Consumer/Service/MockServer.php index 44aa9179..4d0a7386 100644 --- a/src/PhpPact/Consumer/Service/MockServer.php +++ b/src/PhpPact/Consumer/Service/MockServer.php @@ -2,11 +2,13 @@ namespace PhpPact\Consumer\Service; +use FFI; use PhpPact\Config\PactConfigInterface; use PhpPact\Consumer\Exception\MockServerNotStartedException; use PhpPact\Consumer\Exception\MockServerNotWrotePactFileException; use PhpPact\Consumer\Registry\Pact\PactRegistryInterface; use PhpPact\FFI\ClientInterface; +use PhpPact\Service\LoggerInterface; use PhpPact\Standalone\MockService\MockServerConfigInterface; class MockServer implements MockServerInterface @@ -14,7 +16,8 @@ class MockServer implements MockServerInterface public function __construct( private ClientInterface $client, private PactRegistryInterface $pactRegistry, - private MockServerConfigInterface $config + private MockServerConfigInterface $config, + private ?LoggerInterface $logger = null ) { } @@ -37,17 +40,19 @@ public function start(): void public function verify(): bool { - $matched = $this->client->call('pactffi_mock_server_matched', $this->config->getPort()); - try { + $matched = $this->isMatched(); + if ($matched) { $this->writePact(); + } elseif ($this->logger) { + $this->logger->log($this->getMismatches()); } + + return $matched; } finally { $this->cleanUp(); } - - return $matched; } protected function getTransport(): string @@ -78,4 +83,16 @@ private function cleanUp(): void $this->client->call('pactffi_cleanup_mock_server', $this->config->getPort()); $this->pactRegistry->deletePact(); } + + private function isMatched(): bool + { + return $this->client->call('pactffi_mock_server_matched', $this->config->getPort()); + } + + private function getMismatches(): string + { + $cData = $this->client->call('pactffi_mock_server_mismatches', $this->config->getPort()); + + return FFI::string($cData); + } } diff --git a/src/PhpPact/Service/LoggerInterface.php b/src/PhpPact/Service/LoggerInterface.php new file mode 100644 index 00000000..9e2e9511 --- /dev/null +++ b/src/PhpPact/Service/LoggerInterface.php @@ -0,0 +1,8 @@ +client = new Client(); $this