Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow like null #350

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@
"XmlConsumer\\": "example/xml/consumer/src",
"XmlConsumer\\Tests\\": "example/xml/consumer/tests",
"XmlProvider\\": "example/xml/provider/src",
"XmlProvider\\Tests\\": "example/xml/provider/tests"
"XmlProvider\\Tests\\": "example/xml/provider/tests",
"MatchersConsumer\\": "example/matchers/consumer/src",
"MatchersConsumer\\Tests\\": "example/matchers/consumer/tests",
"MatchersProvider\\Tests\\": "example/matchers/provider/tests"
}
},
"scripts": {
Expand Down
11 changes: 11 additions & 0 deletions example/matchers/consumer/phpunit.xml
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 example/matchers/consumer/src/Service/HttpClientService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace MatchersConsumer\Service;

use GuzzleHttp\Client;

class HttpClientService
{
private Client $httpClient;

private string $baseUri;

public function __construct(string $baseUri)
{
$this->httpClient = new Client();
$this->baseUri = $baseUri;
}

public function getMatchers(): array
{
$response = $this->httpClient->get("{$this->baseUri}/matchers", [
'headers' => ['Accept' => 'application/json'],
'query' => ['ignore' => 'statusCode'],
]);

return \json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
}
}
142 changes: 142 additions & 0 deletions example/matchers/consumer/tests/Service/MatchersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace MatchersConsumer\Tests\Service;

use MatchersConsumer\Service\HttpClientService;
use PhpPact\Consumer\InteractionBuilder;
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 MatchersTest extends TestCase
{
private Matcher $matcher;

public function setUp(): void
{
$this->matcher = new Matcher();
}

public function testGetMatchers()
{
$request = new ConsumerRequest();
$request
->setMethod('GET')
->setPath($this->matcher->regex('/matchers', '^\/matchers$'))
->setQuery([
'ignore' => 'statusCode',
])
->addHeader('Accept', 'application/json');

$response = new ProviderResponse();
$response
->setStatus(200)
->addHeader('Content-Type', 'application/json')
->setBody([
'like' => $this->matcher->like(['key' => 'value']),
'likeNull' => $this->matcher->like(null),
'eachLike' => $this->matcher->eachLike('item'),
'atLeastLike' => $this->matcher->atLeastLike(1, 5),
'atMostLike' => $this->matcher->atMostLike(1, 3),
'constrainedArrayLike' => $this->matcher->constrainedArrayLike('item', 2, 4),
'regex' => $this->matcher->regex('500 miles', '^\d+ (miles|kilometers)$'),
'dateISO8601' => $this->matcher->dateISO8601(),
'timeISO8601' => $this->matcher->timeISO8601(),
'dateTimeISO8601' => $this->matcher->dateTimeISO8601(),
'dateTimeWithMillisISO8601' => $this->matcher->dateTimeWithMillisISO8601(),
'timestampRFC3339' => $this->matcher->timestampRFC3339(),
'likeBool' => $this->matcher->boolean(),
'likeInt' => $this->matcher->integer(),
'likeDecimal' => $this->matcher->decimal(),
'boolean' => $this->matcher->booleanV3(false),
'integer' => $this->matcher->integerV3(9),
'decimal' => $this->matcher->decimalV3(79.01),
'hexadecimal' => $this->matcher->hexadecimal('F7A16'),
'uuid' => $this->matcher->uuid('52c9585e-f345-4964-aa28-a45c64b2b2eb'),
'ipv4Address' => $this->matcher->ipv4Address(),
'ipv6Address' => $this->matcher->ipv6Address(),
'email' => $this->matcher->email(),
'nullValue' => $this->matcher->nullValue(),
'date' => $this->matcher->date('yyyy-MM-dd', '2015-05-16'),
'time' => $this->matcher->time('HH:mm::ss', '23:59::58'),
'datetime' => $this->matcher->datetime("YYYY-mm-DD'T'HH:mm:ss", '2000-10-31T01:30:00'),
'likeString' => $this->matcher->string('some string'),
'equal' => $this->matcher->equal('exact this value'),
'includes' => $this->matcher->includes('lazy dog'),
'number' => $this->matcher->number(123),
'arrayContaining' => $this->matcher->arrayContaining([
$this->matcher->string('some text'),
$this->matcher->number(111),
$this->matcher->uuid('2fbd41cc-4bbc-44ea-a419-67f767691407'),
]),
'notEmpty' => $this->matcher->notEmpty(['1','2','3']),
'semver' => $this->matcher->semver('10.0.0-alpha4'),
'contentType' => $this->matcher->contentType('text/html'),
]);

$config = new MockServerConfig();
$config
->setConsumer('matchersConsumer')
->setProvider('matchersProvider')
->setPactDir(__DIR__.'/../../../pacts')
->setPactSpecificationVersion('4.0.0');
if ($logLevel = \getenv('PACT_LOGLEVEL')) {
$config->setLogLevel($logLevel);
}
$builder = new InteractionBuilder($config);
$builder
->given('Get Matchers')
->uponReceiving('A get request to /matchers')
->with($request)
->willRespondWith($response);

$service = new HttpClientService($config->getBaseUri());
$matchersResult = $service->getMatchers();
$verifyResult = $builder->verify();

$this->assertTrue($verifyResult);
$this->assertEquals([
'like' => ['key' => 'value'],
'likeNull' => null,
'eachLike' => ['item'],
'atLeastLike' => [1, 1, 1, 1, 1],
'atMostLike' => [1],
'constrainedArrayLike' => ['item', 'item'],
'regex' => '500 miles',
'dateISO8601' => '2013-02-01',
'timeISO8601' => 'T22:44:30.652Z',
'dateTimeISO8601' => '2015-08-06T16:53:10+01:00',
'dateTimeWithMillisISO8601' => '2015-08-06T16:53:10.123+01:00',
'timestampRFC3339' => 'Mon, 31 Oct 2016 15:21:41 -0400',
'likeBool' => true,
'likeInt' => 13,
'likeDecimal' => 13.01,
'boolean' => false,
'integer' => 9,
'decimal' => 79.01,
'hexadecimal' => 'F7A16',
'uuid' => '52c9585e-f345-4964-aa28-a45c64b2b2eb',
'ipv4Address' => '127.0.0.13',
'ipv6Address' => '::ffff:192.0.2.128',
'email' => '[email protected]',
'nullValue' => null,
'date' => '2015-05-16',
'time' => '23:59::58',
'datetime' => '2000-10-31T01:30:00',
'likeString' => 'some string',
'equal' => 'exact this value',
'includes' => 'lazy dog',
'number' => 123,
'arrayContaining' => [
'some text',
111,
'2fbd41cc-4bbc-44ea-a419-67f767691407',
],
'notEmpty' => ['1', '2', '3'],
'semver' => '10.0.0-alpha4',
'contentType' => 'text/html',
], $matchersResult);
}
}
Loading