-
Notifications
You must be signed in to change notification settings - Fork 92
/
Server.php
90 lines (77 loc) · 3.03 KB
/
Server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace PhpPactTest\CompatibilitySuite\Service;
use PhpPact\Config\PactConfigInterface;
use PhpPact\Consumer\Driver\Pact\PactDriver;
use PhpPact\Consumer\Driver\Pact\PactDriverInterface;
use PhpPact\Consumer\Registry\Interaction\InteractionRegistry;
use PhpPact\Consumer\Registry\Interaction\InteractionRegistryInterface;
use PhpPact\Consumer\Service\MockServer;
use PhpPact\Consumer\Service\MockServerInterface;
use PhpPact\FFI\Client;
use PhpPact\Standalone\MockService\MockServerConfig;
use PhpPact\Standalone\MockService\MockServerConfigInterface;
use PhpPactTest\CompatibilitySuite\Constant\Path;
use PhpPactTest\CompatibilitySuite\Model\Logger;
use PhpPactTest\CompatibilitySuite\Model\PactPath;
use PhpPactTest\CompatibilitySuite\Model\VerifyResult;
use Psr\Http\Message\UriInterface;
final class Server implements ServerInterface
{
private MockServerConfigInterface $config;
private PactDriverInterface $pactDriver;
private InteractionRegistryInterface $interactionRegistry;
private MockServerInterface $mockServer;
private VerifyResult $verifyResult;
private Logger $logger;
private PactPath $pactPath;
public function __construct(
string $specificationVersion,
private InteractionsStorageInterface $storage
) {
$this->pactPath = new PactPath(sprintf('server_specification_%s', $specificationVersion));
$this->config = new MockServerConfig();
$this->config
->setConsumer($this->pactPath->getConsumer())
->setProvider(PactPath::PROVIDER)
->setPactDir(Path::PACTS_PATH)
->setPactSpecificationVersion($specificationVersion)
->setPactFileWriteMode(PactConfigInterface::MODE_OVERWRITE);
$this->logger = new Logger();
$client = new Client();
$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);
foreach ($interactions as $interaction) {
$this->interactionRegistry->registerInteraction($interaction);
}
$this->mockServer->start();
}
public function getBaseUri(): UriInterface
{
return $this->config->getBaseUri();
}
public function verify(): void
{
$success = $this->mockServer->verify();
$this->verifyResult = new VerifyResult($success, !$success ? $this->logger->getOutput() : '');
}
public function getVerifyResult(): VerifyResult
{
if (!isset($this->verifyResult)) {
$this->verify();
}
return $this->verifyResult;
}
public function getPactPath(): PactPath
{
return $this->pactPath;
}
public function getPort(): int
{
return $this->config->getPort();
}
}