Skip to content

Commit

Permalink
Merge pull request #363 from tienvx/get-mock-server-output
Browse files Browse the repository at this point in the history
feat: Allow capture mock server mismatches
  • Loading branch information
tienvx committed Nov 22, 2023
2 parents 7b3cf33 + 83dfdb8 commit a20bcd3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
27 changes: 22 additions & 5 deletions src/PhpPact/Consumer/Service/MockServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

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
{
public function __construct(
private ClientInterface $client,
private PactRegistryInterface $pactRegistry,
private MockServerConfigInterface $config
private MockServerConfigInterface $config,
private ?LoggerInterface $logger = null
) {
}

Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
8 changes: 8 additions & 0 deletions src/PhpPact/Service/LoggerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace PhpPact\Service;

interface LoggerInterface
{
public function log(string $output): void;
}

This file was deleted.

4 changes: 2 additions & 2 deletions src/PhpPact/Standalone/ProviderVerifier/Verifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
use PhpPact\FFI\Client;
use PhpPact\FFI\ClientInterface;
use PhpPact\FFI\Model\ArrayData;
use PhpPact\Service\LoggerInterface;
use PhpPact\Standalone\ProviderVerifier\Model\Source\BrokerInterface;
use PhpPact\Standalone\ProviderVerifier\Model\Source\UrlInterface;
use PhpPact\Standalone\ProviderVerifier\Model\VerifierConfigInterface;
use PhpPact\Standalone\ProviderVerifier\Model\VerifierLoggerInterface;

class Verifier
{
protected ClientInterface $client;
protected CData $handle;

public function __construct(VerifierConfigInterface $config, private ?VerifierLoggerInterface $logger = null)
public function __construct(VerifierConfigInterface $config, private ?LoggerInterface $logger = null)
{
$this->client = new Client();
$this
Expand Down

0 comments on commit a20bcd3

Please sign in to comment.