/
PactTestListener.php
131 lines (113 loc) · 4.33 KB
/
PactTestListener.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace PhpPact\Consumer\Listener;
use GuzzleHttp\Psr7\Uri;
use PhpPact\Broker\Service\BrokerHttpClient;
use PhpPact\Http\GuzzleClient;
use PhpPact\Standalone\Exception\MissingEnvVariableException;
use PhpPact\Standalone\MockService\MockServer;
use PhpPact\Standalone\MockService\MockServerEnvConfig;
use PhpPact\Standalone\MockService\Service\MockServerHttpService;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestListenerDefaultImplementation;
use PHPUnit\Framework\TestSuite;
/**
* PACT listener that can be used with environment variables and easily attached to PHPUnit configuration.
* Class PactTestListener
*/
class PactTestListener implements TestListener
{
use TestListenerDefaultImplementation;
/** @var MockServer */
private $server;
/**
* Name of the test suite configured in your phpunit config.
*
* @var string[]
*/
private $testSuiteNames;
/** @var MockServerEnvConfig */
private $mockServerConfig;
/** @var bool */
private $failed;
/**
* PactTestListener constructor.
*
* @param string[] $testSuiteNames test suite names that need evaluated with the listener
*
* @throws MissingEnvVariableException
*/
public function __construct(array $testSuiteNames)
{
$this->testSuiteNames = $testSuiteNames;
$this->mockServerConfig = new MockServerEnvConfig();
}
/**
* @param TestSuite $suite
*
* @throws \Exception
*/
public function startTestSuite(TestSuite $suite): void
{
if (\in_array($suite->getName(), $this->testSuiteNames)) {
$this->server = new MockServer($this->mockServerConfig);
$this->server->start();
}
}
public function addError(Test $test, \Throwable $t, float $time): void
{
$this->failed = true;
}
public function addFailure(Test $test, AssertionFailedError $e, float $time): void
{
$this->failed = true;
}
/**
* Publish JSON results to PACT Broker and stop the Mock Server.
*
* @param TestSuite $suite
*/
public function endTestSuite(TestSuite $suite): void
{
if (\in_array($suite->getName(), $this->testSuiteNames)) {
try {
$httpService = new MockServerHttpService(new GuzzleClient(), $this->mockServerConfig);
$httpService->verifyInteractions();
$json = $httpService->getPactJson();
} finally {
$this->server->stop();
}
if ($this->failed === true) {
print 'A unit test has failed. Skipping PACT file upload.';
} elseif (!($pactBrokerUri = \getenv('PACT_BROKER_URI'))) {
print 'PACT_BROKER_URI environment variable was not set. Skipping PACT file upload.';
} elseif (!($consumerVersion = \getenv('PACT_CONSUMER_VERSION'))) {
print 'PACT_CONSUMER_VERSION environment variable was not set. Skipping PACT file upload.';
} elseif (!($tag = \getenv('PACT_CONSUMER_TAG'))) {
print 'PACT_CONSUMER_TAG environment variable was not set. Skipping PACT file upload.';
} else {
$clientConfig = [];
if (($user = \getenv('PACT_BROKER_HTTP_AUTH_USER')) &&
($pass = \getenv('PACT_BROKER_HTTP_AUTH_PASS'))
) {
$clientConfig = [
'auth' => [$user, $pass],
];
}
if (($sslVerify = \getenv('PACT_BROKER_SSL_VERIFY'))) {
$clientConfig['verify'] = $sslVerify !== 'no';
}
$headers = [];
if ($bearerToken = \getenv('PACT_BROKER_BEARER_TOKEN')) {
$headers['Authorization'] = 'Bearer ' . $bearerToken;
}
$client = new GuzzleClient($clientConfig);
$brokerHttpService = new BrokerHttpClient($client, new Uri($pactBrokerUri), $headers);
$brokerHttpService->tag($this->mockServerConfig->getConsumer(), $consumerVersion, $tag);
$brokerHttpService->publishJson($json, $consumerVersion);
print 'Pact file has been uploaded to the Broker successfully.';
}
}
}
}