-
Notifications
You must be signed in to change notification settings - Fork 92
/
InteractionRegistry.php
93 lines (77 loc) · 3.16 KB
/
InteractionRegistry.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
91
92
93
<?php
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\Model\ConsumerRequest;
use PhpPact\Consumer\Model\Interaction;
use PhpPact\Consumer\Model\ProviderResponse;
use PhpPact\Consumer\Model\ProviderState;
use PhpPact\FFI\ClientInterface;
class InteractionRegistry extends AbstractRegistry implements InteractionRegistryInterface
{
private RequestRegistryInterface $requestRegistry;
private ResponseRegistryInterface $responseRegistry;
public function __construct(
ClientInterface $client,
PactDriverInterface $pactDriver,
?RequestRegistryInterface $requestRegistry = null,
?ResponseRegistryInterface $responseRegistry = null,
) {
parent::__construct($client, $pactDriver);
$this->requestRegistry = $requestRegistry ?? new RequestRegistry($client, $this);
$this->responseRegistry = $responseRegistry ?? new ResponseRegistry($client, $this);
}
public function registerInteraction(Interaction $interaction): bool
{
$this
->newInteraction($interaction->getDescription())
->given($interaction->getProviderStates())
->uponReceiving($interaction->getDescription())
->with($interaction->getRequest())
->willRespondWith($interaction->getResponse());
return true;
}
protected function newInteraction(string $description): self
{
$this->id = $this->client->call('pactffi_new_interaction', $this->pactDriver->getPact()->handle, $description);
return $this;
}
private function uponReceiving(string $description): self
{
$this->client->call('pactffi_upon_receiving', $this->id, $description);
return $this;
}
/**
* @param ProviderState[] $providerStates
*/
private function given(array $providerStates): self
{
foreach ($providerStates as $providerState) {
$this->client->call('pactffi_given', $this->id, $providerState->getName());
foreach ($providerState->getParams() as $key => $value) {
$this->client->call('pactffi_given_with_param', $this->id, $providerState->getName(), (string) $key, (string) $value);
}
}
return $this;
}
private function with(ConsumerRequest $request): self
{
$this->requestRegistry
->withRequest($request->getMethod(), $request->getPath())
->withQueryParameters($request->getQuery())
->withHeaders($request->getHeaders())
->withBody($request->getBody());
return $this;
}
private function willRespondWith(ProviderResponse $response): self
{
$this->responseRegistry
->withResponse($response->getStatus())
->withHeaders($response->getHeaders())
->withBody($response->getBody());
return $this;
}
}