-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
551 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,5 @@ | ||
<?php | ||
|
||
$loader = require __DIR__ . '/../../../vendor/autoload.php'; | ||
$loader->addPsr4('FormUrlEncodedConsumer\\', __DIR__ . '/src'); | ||
$loader->addPsr4('FormUrlEncodedConsumer\\Tests\\', __DIR__ . '/tests'); |
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="./autoload.php" colors="true"> | ||
<testsuites> | ||
<testsuite name="PhpPact Example Tests"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<php> | ||
<env name="PACT_LOGLEVEL" value="DEBUG"/> | ||
</php> | ||
</phpunit> |
50 changes: 50 additions & 0 deletions
50
example/form-urlencoded/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,50 @@ | ||
<?php | ||
|
||
namespace FormUrlEncodedConsumer\Service; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Psr7\Uri; | ||
|
||
class HttpClientService | ||
{ | ||
private Client $httpClient; | ||
|
||
private string $baseUri; | ||
|
||
public function __construct(string $baseUri) | ||
{ | ||
$this->httpClient = new Client(); | ||
$this->baseUri = $baseUri; | ||
} | ||
|
||
public function createUser(): string | ||
{ | ||
$response = $this->httpClient->post(new Uri("{$this->baseUri}/users"), [ | ||
'body' => http_build_query([ | ||
'empty' => '', | ||
'agree' => 'true', | ||
'fullname' => 'First Last Name', | ||
'email' => 'user@example.test', | ||
'password' => 'very@secure&password123', | ||
'age' => 41, | ||
'ampersand' => '&', | ||
'slash' => '/', | ||
'question-mark' => '?', | ||
'equals-sign' => '=', | ||
'&' => 'ampersand', | ||
'/' => 'slash', | ||
'?' => 'question-mark', | ||
'=' => 'equals-sign', | ||
]) . | ||
'&=first&=second&=third' . | ||
'&roles[]=User&roles[]=Manager' . | ||
'&orders[]=&orders[]=ASC&orders[]=DESC', | ||
'headers' => [ | ||
'Accept' => 'application/x-www-form-urlencoded', | ||
'Content-Type' => 'application/x-www-form-urlencoded', | ||
], | ||
]); | ||
|
||
return $response->getBody(); | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
example/form-urlencoded/consumer/tests/Service/HttpClientServiceTest.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,119 @@ | ||
<?php | ||
|
||
namespace FormUrlEncodedConsumer\Tests\Service; | ||
|
||
use PhpPact\Consumer\Matcher\Generators\RandomInt; | ||
use PhpPact\Consumer\Matcher\Generators\RandomString; | ||
use PhpPact\Consumer\Matcher\Generators\Regex; | ||
use PhpPact\Consumer\Matcher\Generators\Uuid; | ||
use PhpPact\Consumer\Matcher\Matcher; | ||
use FormUrlEncodedConsumer\Service\HttpClientService; | ||
use PhpPact\Consumer\InteractionBuilder; | ||
use PhpPact\Consumer\Model\Body\Text; | ||
use PhpPact\Consumer\Model\ConsumerRequest; | ||
use PhpPact\Consumer\Model\ProviderResponse; | ||
use PhpPact\Standalone\MockService\MockServerConfig; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class HttpClientServiceTest extends TestCase | ||
{ | ||
public function testGetMovies() | ||
{ | ||
$matcher = new Matcher(); | ||
|
||
$request = new ConsumerRequest(); | ||
$request | ||
->setMethod('POST') | ||
->setPath('/users') | ||
->addHeader('Content-Type', 'application/x-www-form-urlencoded') | ||
->addHeader('Accept', 'application/x-www-form-urlencoded') | ||
->setBody( | ||
new Text( | ||
json_encode([ | ||
'empty' => $matcher->equal(''), | ||
'agree' => $matcher->regex('false', 'true|false'), | ||
'fullname' => $matcher->string('User name'), | ||
'email' => $matcher->email('user@email.test'), | ||
'password' => $matcher->regex('user@password111', '^[\w\d@$!%*#?&^_-]{8,}$'), | ||
'age' => $matcher->number(27), | ||
'roles[]' => $matcher->eachValue(['User'], [$matcher->regex('User', 'Admin|User|Manager')]), | ||
'orders[]' => $matcher->arrayContaining([ | ||
$matcher->equal('DESC'), | ||
$matcher->equal('ASC'), | ||
$matcher->equal(''), | ||
]), | ||
// Empty string keys are supported | ||
'' => ['first', 'second', 'third'], | ||
// Null, boolean and object values are not supported, so the values and matchers will be ignored | ||
'null' => $matcher->nullValue(), | ||
'boolean' => $matcher->booleanV3(true), | ||
'object' => $matcher->like([ | ||
'key' => $matcher->string('value') | ||
]), | ||
// special characters are encoded | ||
'ampersand' => $matcher->equal('&'), | ||
'slash' => '/', | ||
'question-mark' => '?', | ||
'equals-sign' => '=', | ||
'&' => 'ampersand', | ||
'/' => 'slash', | ||
'?' => 'question-mark', | ||
'=' => 'equals-sign', | ||
]), | ||
'application/x-www-form-urlencoded' | ||
) | ||
) | ||
; | ||
|
||
$response = new ProviderResponse(); | ||
$response | ||
->setStatus(201) | ||
->addHeader('Content-Type', 'application/x-www-form-urlencoded') | ||
->setBody( | ||
new Text( | ||
json_encode([ | ||
'id' => $matcher->uuid('')->withGenerator(new Uuid()), | ||
'age' => $matcher->integerV3(1)->withGenerator(new RandomInt(0, 130)), | ||
'name[]' => [ | ||
$matcher->regex('', $gender = 'Mr\.|Mrs\.|Miss|Ms\.')->withGenerator(new Regex($gender)), | ||
$matcher->string('')->withGenerator(new RandomString()), | ||
$matcher->string('')->withGenerator(new RandomString()), | ||
$matcher->string('')->withGenerator(new RandomString()), | ||
], | ||
]), | ||
'application/x-www-form-urlencoded' | ||
) | ||
); | ||
|
||
$config = new MockServerConfig(); | ||
$config | ||
->setConsumer('formUrlEncodedConsumer') | ||
->setProvider('formUrlEncodedProvider') | ||
->setPactDir(__DIR__.'/../../../pacts'); | ||
if ($logLevel = \getenv('PACT_LOGLEVEL')) { | ||
$config->setLogLevel($logLevel); | ||
} | ||
$builder = new InteractionBuilder($config); | ||
$builder | ||
->given('Endpoint is protected') | ||
->uponReceiving('A post request to /users') | ||
->with($request) | ||
->willRespondWith($response); | ||
|
||
$service = new HttpClientService($config->getBaseUri()); | ||
parse_str($service->createUser(), $params); | ||
$verifyResult = $builder->verify(); | ||
|
||
$this->assertTrue(condition: $verifyResult); | ||
$this->assertArrayHasKey('id', $params); | ||
$pattern = Matcher::UUID_V4_FORMAT; | ||
$this->assertMatchesRegularExpression("/{$pattern}/", $params['id']); | ||
$this->assertArrayHasKey('age', $params); | ||
$this->assertLessThanOrEqual(130, $params['age']); | ||
$this->assertGreaterThanOrEqual(0, $params['age']); | ||
$this->assertArrayHasKey('name', $params); | ||
$this->assertIsArray($params['name']); | ||
$this->assertCount(4, $params['name']); | ||
$this->assertMatchesRegularExpression("/{$gender}/", $params['name'][0]); | ||
} | ||
} |
Oops, something went wrong.