-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #365 from tienvx/add-generators-example
chore: Add generators example
- Loading branch information
Showing
9 changed files
with
516 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="../../../vendor/autoload.php" colors="true"> | ||
<testsuites> | ||
<testsuite name="PhpPact Example Tests"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<php> | ||
<env name="PACT_LOGLEVEL" value="DEBUG"/> | ||
</php> | ||
</phpunit> |
28 changes: 28 additions & 0 deletions
28
example/generators/consumer/src/Service/HttpClientService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace GeneratorsConsumer\Service; | ||
|
||
use GuzzleHttp\Client; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class HttpClientService | ||
{ | ||
private Client $httpClient; | ||
|
||
private string $baseUri; | ||
|
||
public function __construct(string $baseUri) | ||
{ | ||
$this->httpClient = new Client(); | ||
$this->baseUri = $baseUri; | ||
} | ||
|
||
public function sendRequest(): ResponseInterface | ||
{ | ||
return $this->httpClient->get("{$this->baseUri}/generators", [ | ||
'headers' => ['Accept' => 'application/json'], | ||
'json' => ['id' => 112], | ||
'http_errors' => false, | ||
]); | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
example/generators/consumer/tests/Service/GeneratorsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
namespace GeneratorsConsumer\Tests\Service; | ||
|
||
use DateTime; | ||
use GeneratorsConsumer\Service\HttpClientService; | ||
use PhpPact\Consumer\InteractionBuilder; | ||
use PhpPact\Consumer\Matcher\HttpStatus; | ||
use PhpPact\Consumer\Matcher\Matcher; | ||
use PhpPact\Consumer\Model\ConsumerRequest; | ||
use PhpPact\Consumer\Model\ProviderResponse; | ||
use PhpPact\Standalone\MockService\MockServerConfig; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class GeneratorsTest extends TestCase | ||
{ | ||
private Matcher $matcher; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->matcher = new Matcher(); | ||
} | ||
|
||
public function testGetMatchers() | ||
{ | ||
$request = new ConsumerRequest(); | ||
$request | ||
->setMethod('GET') | ||
->setPath('/generators') | ||
->addHeader('Accept', 'application/json') | ||
->setBody([ | ||
'id' => $this->matcher->fromProviderState($this->matcher->integer(), '${id}') | ||
]); | ||
|
||
$response = new ProviderResponse(); | ||
$response | ||
->setStatus($this->matcher->statusCode(HttpStatus::CLIENT_ERROR)) | ||
->addHeader('Content-Type', 'application/json') | ||
->setBody([ | ||
'regex' => $this->matcher->regex(null, $regexWithoutAnchors = '\d+ (miles|kilometers)'), | ||
'boolean' => $this->matcher->booleanV3(null), | ||
'integer' => $this->matcher->integerV3(null), | ||
'decimal' => $this->matcher->decimalV3(null), | ||
'hexadecimal' => $this->matcher->hexadecimal(null), | ||
'uuid' => $this->matcher->uuid(null), | ||
'date' => $this->matcher->date('yyyy-MM-dd', null), | ||
'time' => $this->matcher->time('HH:mm::ss', null), | ||
'datetime' => $this->matcher->datetime("YYYY-MM-D'T'HH:mm:ss", null), | ||
'string' => $this->matcher->string(null), | ||
'number' => $this->matcher->number(null), | ||
'requestId' => 222, | ||
]); | ||
|
||
$config = new MockServerConfig(); | ||
$config | ||
->setConsumer('generatorsConsumer') | ||
->setProvider('generatorsProvider') | ||
->setPactDir(__DIR__.'/../../../pacts') | ||
->setPactSpecificationVersion('4.0.0'); | ||
if ($logLevel = \getenv('PACT_LOGLEVEL')) { | ||
$config->setLogLevel($logLevel); | ||
} | ||
$builder = new InteractionBuilder($config); | ||
$builder | ||
->given('Get Generators') | ||
->uponReceiving('A get request to /generators') | ||
->with($request) | ||
->willRespondWith($response); | ||
|
||
$service = new HttpClientService($config->getBaseUri()); | ||
$response = $service->sendRequest(); | ||
$verifyResult = $builder->verify(); | ||
|
||
$statusCode = $response->getStatusCode(); | ||
$body = \json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR); | ||
|
||
$this->assertTrue($verifyResult); | ||
$this->assertThat( | ||
$statusCode, | ||
$this->logicalAnd( | ||
$this->greaterThanOrEqual(400), | ||
$this->lessThanOrEqual(499) | ||
) | ||
); | ||
$this->assertRegExp('/^' . $regexWithoutAnchors . '$/', $body['regex']); | ||
$this->assertIsBool($body['boolean']); | ||
$this->assertIsInt($body['integer']); | ||
$this->assertIsFloat($body['decimal'] + 0); | ||
$this->assertRegExp('/' . Matcher::HEX_FORMAT . '/', $body['hexadecimal']); | ||
$this->assertRegExp('/' . Matcher::UUID_V4_FORMAT . '/', $body['uuid']); | ||
$this->assertTrue($this->validateDateTime($body['date'], 'Y-m-d')); | ||
$this->assertTrue($this->validateDateTime($body['time'], 'H:i::s')); | ||
$this->assertTrue($this->validateDateTime($body['datetime'], "Y-m-z\TH:i:s")); | ||
$this->assertIsString($body['string']); | ||
$this->assertIsNumeric($body['number']); | ||
$this->assertSame(222, $body['requestId']); | ||
} | ||
|
||
private function validateDateTime(string $datetime, string $format): bool | ||
{ | ||
$value = DateTime::createFromFormat($format, $datetime); | ||
|
||
return $value && $value->format($format) === $datetime; | ||
} | ||
} |
Oops, something went wrong.