forked from chuso/pact-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsumerServiceHelloTest.php
54 lines (45 loc) · 1.9 KB
/
ConsumerServiceHelloTest.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
<?php
namespace Consumer\Service;
use PhpPact\Consumer\InteractionBuilder;
use PhpPact\Consumer\Matcher\Matcher;
use PhpPact\Consumer\Model\ConsumerRequest;
use PhpPact\Consumer\Model\ProviderResponse;
use PhpPact\Standalone\MockService\MockServerEnvConfig;
use PHPUnit\Framework\TestCase;
class ConsumerServiceHelloTest extends TestCase
{
/**
* Example PACT test.
*
* @throws \Exception
*/
public function testGetHelloString()
{
$matcher = new Matcher();
// Create your expected request from the consumer.
$request = new ConsumerRequest();
$request
->setMethod('GET')
->setPath('/hello/Bob')
->addHeader('Content-Type', 'application/json');
// Create your expected response from the provider.
$response = new ProviderResponse();
$response
->setStatus(200)
->addHeader('Content-Type', 'application/json')
->setBody([
'message' => $matcher->term('Hello, Bob', '(Hello, )[A-Za-z]')
]);
// Create a configuration that reflects the server that was started. You can create a custom MockServerConfigInterface if needed.
$config = new MockServerEnvConfig();
$builder = new InteractionBuilder($config);
$builder
->uponReceiving('A get request to /hello/{name}')
->with($request)
->willRespondWith($response); // This has to be last. This is what makes an API request to the Mock Server to set the interaction.
$service = new HttpClientService($config->getBaseUri()); // Pass in the URL to the Mock Server.
$result = $service->getHelloString('Bob'); // Make the real API request against the Mock Server.
$builder->verify(); // This will verify that the interactions took place.
$this->assertEquals('Hello, Bob', $result); // Make your assertions.
}
}