From 9f20a8e6fb271dec00c85d3912adcb4a778dd044 Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Thu, 10 Oct 2024 07:44:39 +0700 Subject: [PATCH] feat: Support generators in more matchers --- .../consumer/tests/Service/GeneratorsTest.php | 29 ++++-- ...generatorsConsumer-generatorsProvider.json | 95 +++++++++++++++++-- example/generators/provider/public/index.php | 12 ++- .../consumer/tests/Service/MatchersTest.php | 6 +- src/PhpPact/Consumer/Matcher/Matcher.php | 18 ++-- .../Consumer/Matcher/Matchers/Equality.php | 9 +- .../Consumer/Matcher/Matchers/NotEmpty.php | 9 +- .../Consumer/Matcher/Matchers/Type.php | 9 +- .../PhpPact/Consumer/Matcher/MatcherTest.php | 37 ++++++-- .../Matcher/Matchers/EqualityTest.php | 15 +-- .../Matcher/Matchers/NotEmptyTest.php | 15 +-- .../Consumer/Matcher/Matchers/TypeTest.php | 16 ++-- tests/PhpPact/Xml/XmlBuilderTest.php | 2 +- 13 files changed, 211 insertions(+), 61 deletions(-) diff --git a/example/generators/consumer/tests/Service/GeneratorsTest.php b/example/generators/consumer/tests/Service/GeneratorsTest.php index e166208f..c56a783d 100644 --- a/example/generators/consumer/tests/Service/GeneratorsTest.php +++ b/example/generators/consumer/tests/Service/GeneratorsTest.php @@ -5,6 +5,8 @@ use DateTime; use GeneratorsConsumer\Service\HttpClientService; use PhpPact\Consumer\InteractionBuilder; +use PhpPact\Consumer\Matcher\Generators\RandomString; +use PhpPact\Consumer\Matcher\Generators\Uuid; use PhpPact\Consumer\Matcher\HttpStatus; use PhpPact\Consumer\Matcher\Matcher; use PhpPact\Consumer\Matcher\Matchers\StringValue; @@ -39,9 +41,9 @@ public function testGetGenerators(): void ->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), + 'boolean_v3' => $this->matcher->booleanV3(null), + 'integer_v3' => $this->matcher->integerV3(null), + 'decimal_v3' => $this->matcher->decimalV3(null), 'hexadecimal' => $this->matcher->hexadecimal(null), 'uuid' => $this->matcher->uuid(null), 'date' => $this->matcher->date('yyyy-MM-dd', null), @@ -50,6 +52,12 @@ public function testGetGenerators(): void 'string' => $this->matcher->string(null), 'number' => $this->matcher->number(null), 'url' => $this->matcher->url('http://localhost/users/1234/posts/latest', '.*(\\/users\\/\\d+\\/posts\\/latest)$'), + 'notEmpty' => $this->matcher->notEmpty('text')->withGenerator(new RandomString()), + 'equality' => $this->matcher->equal('Hello World!')->withGenerator(new RandomString()), + 'like' => $this->matcher->like('6057401b-c539-4948-971a-24b702d79882')->withGenerator(new Uuid()), + 'boolean' => $this->matcher->boolean(null), + 'integer' => $this->matcher->integer(null), + 'decimal' => $this->matcher->decimal(null), 'requestId' => 222, ]); @@ -85,9 +93,9 @@ public function testGetGenerators(): void ) ); $this->assertMatchesRegularExpression('/^' . $regexWithoutAnchors . '$/', $body['regex']); - $this->assertIsBool($body['boolean']); - $this->assertIsInt($body['integer']); - $this->assertIsFloat($body['decimal'] + 0); + $this->assertIsBool($body['boolean_v3']); + $this->assertIsInt($body['integer_v3']); + $this->assertIsFloat($body['decimal_v3'] + 0); $this->assertMatchesRegularExpression('/' . Matcher::HEX_FORMAT . '/', $body['hexadecimal']); $this->assertMatchesRegularExpression('/' . Matcher::UUID_V4_FORMAT . '/', $body['uuid']); $this->assertTrue($this->validateDateTime($body['date'], 'Y-m-d')); @@ -98,6 +106,15 @@ public function testGetGenerators(): void $this->assertIsNumeric($body['number']); $this->assertNotSame('http://localhost/users/1234/posts/latest', $body['url']); $this->assertMatchesRegularExpression('/.*(\\/users\\/\\d+\\/posts\\/latest)$/', $body['url']); + $this->assertIsString($body['notEmpty']); + $this->assertNotEmpty($body['notEmpty']); + $this->assertIsString($body['equality']); + $this->assertNotSame('Hello World!', $body['equality']); + $this->assertIsString($body['like']); + $this->assertMatchesRegularExpression('/' . Matcher::UUID_V4_FORMAT . '/', $body['like']); + $this->assertIsBool($body['boolean']); + $this->assertIsInt($body['integer']); + $this->assertIsFloat($body['decimal'] + 0); $this->assertSame(222, $body['requestId']); } diff --git a/example/generators/pacts/generatorsConsumer-generatorsProvider.json b/example/generators/pacts/generatorsConsumer-generatorsProvider.json index 51eac746..0a1031a8 100644 --- a/example/generators/pacts/generatorsConsumer-generatorsProvider.json +++ b/example/generators/pacts/generatorsConsumer-generatorsProvider.json @@ -45,8 +45,7 @@ } ] } - }, - "header": {} + } }, "method": "GET", "path": "/generators" @@ -54,12 +53,18 @@ "response": { "body": { "content": { - "boolean": null, + "boolean": true, + "boolean_v3": null, "date": null, "datetime": null, - "decimal": null, + "decimal": 13.01, + "decimal_v3": null, + "equality": "Hello World!", "hexadecimal": null, - "integer": null, + "integer": 13, + "integer_v3": null, + "like": "6057401b-c539-4948-971a-24b702d79882", + "notEmpty": "text", "number": null, "regex": null, "requestId": 222, @@ -76,6 +81,9 @@ "$.boolean": { "type": "RandomBoolean" }, + "$.boolean_v3": { + "type": "RandomBoolean" + }, "$.date": { "format": "yyyy-MM-dd", "type": "Date" @@ -88,6 +96,14 @@ "digits": 10, "type": "RandomDecimal" }, + "$.decimal_v3": { + "digits": 10, + "type": "RandomDecimal" + }, + "$.equality": { + "size": 10, + "type": "RandomString" + }, "$.hexadecimal": { "digits": 10, "type": "RandomHexadecimal" @@ -97,6 +113,18 @@ "min": 0, "type": "RandomInt" }, + "$.integer_v3": { + "max": 10, + "min": 0, + "type": "RandomInt" + }, + "$.like": { + "type": "Uuid" + }, + "$.notEmpty": { + "size": 10, + "type": "RandomString" + }, "$.number": { "max": 10, "min": 0, @@ -137,6 +165,14 @@ "matchingRules": { "body": { "$.boolean": { + "combine": "AND", + "matchers": [ + { + "match": "type" + } + ] + }, + "$.boolean_v3": { "combine": "AND", "matchers": [ { @@ -163,6 +199,14 @@ ] }, "$.decimal": { + "combine": "AND", + "matchers": [ + { + "match": "type" + } + ] + }, + "$.decimal_v3": { "combine": "AND", "matchers": [ { @@ -170,6 +214,14 @@ } ] }, + "$.equality": { + "combine": "AND", + "matchers": [ + { + "match": "equality" + } + ] + }, "$.hexadecimal": { "combine": "AND", "matchers": [ @@ -180,6 +232,16 @@ ] }, "$.integer": { + "combine": "AND", + "matchers": [ + { + "match": "type", + "max": 10, + "min": 0 + } + ] + }, + "$.integer_v3": { "combine": "AND", "matchers": [ { @@ -187,6 +249,22 @@ } ] }, + "$.like": { + "combine": "AND", + "matchers": [ + { + "match": "type" + } + ] + }, + "$.notEmpty": { + "combine": "AND", + "matchers": [ + { + "match": "notEmpty" + } + ] + }, "$.number": { "combine": "AND", "matchers": [ @@ -240,7 +318,6 @@ ] } }, - "header": {}, "status": { "$": { "combine": "AND", @@ -261,9 +338,9 @@ ], "metadata": { "pactRust": { - "ffi": "0.4.22", - "mockserver": "1.2.9", - "models": "1.2.3" + "ffi": "0.4.23", + "mockserver": "1.2.10", + "models": "1.2.5" }, "pactSpecification": { "version": "4.0" diff --git a/example/generators/provider/public/index.php b/example/generators/provider/public/index.php index 7dfd6e49..21614bc6 100644 --- a/example/generators/provider/public/index.php +++ b/example/generators/provider/public/index.php @@ -12,9 +12,9 @@ return Response::json([ 'regex' => '800 kilometers', - 'boolean' => true, - 'integer' => 11, - 'decimal' => 25.1, + 'boolean_v3' => true, + 'integer_v3' => 11, + 'decimal_v3' => 25.1, 'hexadecimal' => '20AC', 'uuid' => 'e9d2f3a5-6ecc-4bff-8935-84bb6141325a', 'date' => '1997-12-11', @@ -23,6 +23,12 @@ 'string' => 'another string', 'number' => 112.3, 'url' => 'https://www.example.com/users/1234/posts/latest', + 'notEmpty' => 123, + 'equality' => 'Hello World!', + 'like' => 'not uuid', + 'boolean' => false, + 'integer' => -99, + 'decimal' => -810.22, 'requestId' => $body['id'], ]) ->withStatus(400); diff --git a/example/matchers/consumer/tests/Service/MatchersTest.php b/example/matchers/consumer/tests/Service/MatchersTest.php index 6078223e..7c5b9c73 100644 --- a/example/matchers/consumer/tests/Service/MatchersTest.php +++ b/example/matchers/consumer/tests/Service/MatchersTest.php @@ -72,9 +72,9 @@ public function testGetMatchers(): void 'dateTimeISO8601' => $this->matcher->dateTimeISO8601(), 'dateTimeWithMillisISO8601' => $this->matcher->dateTimeWithMillisISO8601(), 'timestampRFC3339' => $this->matcher->timestampRFC3339(), - 'likeBool' => $this->matcher->boolean(), - 'likeInt' => $this->matcher->integer(), - 'likeDecimal' => $this->matcher->decimal(), + 'likeBool' => $this->matcher->boolean(true), + 'likeInt' => $this->matcher->integer(13), + 'likeDecimal' => $this->matcher->decimal(13.01), 'boolean' => $this->matcher->booleanV3(false), 'integer' => $this->matcher->integerV3(9), 'decimal' => $this->matcher->decimalV3(79.01), diff --git a/src/PhpPact/Consumer/Matcher/Matcher.php b/src/PhpPact/Consumer/Matcher/Matcher.php index ac63860b..bc847c60 100644 --- a/src/PhpPact/Consumer/Matcher/Matcher.php +++ b/src/PhpPact/Consumer/Matcher/Matcher.php @@ -8,7 +8,10 @@ use PhpPact\Consumer\Matcher\Formatters\Json\JsonFormatter; use PhpPact\Consumer\Matcher\Generators\MockServerURL; use PhpPact\Consumer\Matcher\Generators\ProviderState; +use PhpPact\Consumer\Matcher\Generators\RandomBoolean; +use PhpPact\Consumer\Matcher\Generators\RandomDecimal; use PhpPact\Consumer\Matcher\Generators\RandomHexadecimal; +use PhpPact\Consumer\Matcher\Generators\RandomInt; use PhpPact\Consumer\Matcher\Generators\Uuid; use PhpPact\Consumer\Matcher\Matchers\ArrayContains; use PhpPact\Consumer\Matcher\Matchers\Boolean; @@ -213,19 +216,22 @@ public function timestampRFC3339(string $value = 'Mon, 31 Oct 2016 15:21:41 -040 return $this->term($value, self::RFC3339_TIMESTAMP_FORMAT); } - public function boolean(): Type + public function boolean(?bool $value = null): Type { - return $this->like(true); + return $this->like($value ?? true) + ->withGenerator(is_null($value) ? new RandomBoolean() : null); } - public function integer(int $int = 13): Type + public function integer(?int $value = null): Type { - return $this->like($int); + return $this->like($value ?? 13) + ->withGenerator(is_null($value) ? new RandomInt() : null); } - public function decimal(float $float = 13.01): Type + public function decimal(?float $value = null): Type { - return $this->like($float); + return $this->like($value ?? 13.01) + ->withGenerator(is_null($value) ? new RandomDecimal() : null); } public function booleanV3(?bool $value = null): Boolean diff --git a/src/PhpPact/Consumer/Matcher/Matchers/Equality.php b/src/PhpPact/Consumer/Matcher/Matchers/Equality.php index 2ac4bb76..dac85217 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/Equality.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/Equality.php @@ -6,12 +6,15 @@ use PhpPact\Consumer\Matcher\Model\Expression; use PhpPact\Consumer\Matcher\Model\Matcher\ExpressionFormattableInterface; use PhpPact\Consumer\Matcher\Model\Matcher\JsonFormattableInterface; +use PhpPact\Consumer\Matcher\Trait\JsonFormattableTrait; /** * This is the default matcher, and relies on the equals operator */ -class Equality extends AbstractMatcher implements JsonFormattableInterface, ExpressionFormattableInterface +class Equality extends GeneratorAwareMatcher implements JsonFormattableInterface, ExpressionFormattableInterface { + use JsonFormattableTrait; + public function __construct(private mixed $value) { parent::__construct(); @@ -19,10 +22,10 @@ public function __construct(private mixed $value) public function formatJson(): Attributes { - return new Attributes([ + return $this->mergeJson(new Attributes([ 'pact:matcher:type' => 'equality', 'value' => $this->value, - ]); + ])); } public function formatExpression(): Expression diff --git a/src/PhpPact/Consumer/Matcher/Matchers/NotEmpty.php b/src/PhpPact/Consumer/Matcher/Matchers/NotEmpty.php index d82e0d3c..ce9f0c08 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/NotEmpty.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/NotEmpty.php @@ -6,12 +6,15 @@ use PhpPact\Consumer\Matcher\Model\Expression; use PhpPact\Consumer\Matcher\Model\Matcher\ExpressionFormattableInterface; use PhpPact\Consumer\Matcher\Model\Matcher\JsonFormattableInterface; +use PhpPact\Consumer\Matcher\Trait\JsonFormattableTrait; /** * Value must be present and not empty (not null or the empty string) */ -class NotEmpty extends AbstractMatcher implements JsonFormattableInterface, ExpressionFormattableInterface +class NotEmpty extends GeneratorAwareMatcher implements JsonFormattableInterface, ExpressionFormattableInterface { + use JsonFormattableTrait; + public function __construct(private mixed $value) { parent::__construct(); @@ -19,10 +22,10 @@ public function __construct(private mixed $value) public function formatJson(): Attributes { - return new Attributes([ + return $this->mergeJson(new Attributes([ 'pact:matcher:type' => 'notEmpty', 'value' => $this->value, - ]); + ])); } public function formatExpression(): Expression diff --git a/src/PhpPact/Consumer/Matcher/Matchers/Type.php b/src/PhpPact/Consumer/Matcher/Matchers/Type.php index fc7f4e43..7560dcb8 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/Type.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/Type.php @@ -6,12 +6,15 @@ use PhpPact\Consumer\Matcher\Model\Expression; use PhpPact\Consumer\Matcher\Model\Matcher\ExpressionFormattableInterface; use PhpPact\Consumer\Matcher\Model\Matcher\JsonFormattableInterface; +use PhpPact\Consumer\Matcher\Trait\JsonFormattableTrait; /** * This executes a type based match against the values, that is, they are equal if they are the same type. */ -class Type extends AbstractMatcher implements JsonFormattableInterface, ExpressionFormattableInterface +class Type extends GeneratorAwareMatcher implements JsonFormattableInterface, ExpressionFormattableInterface { + use JsonFormattableTrait; + public function __construct(private mixed $value) { parent::__construct(); @@ -19,10 +22,10 @@ public function __construct(private mixed $value) public function formatJson(): Attributes { - return new Attributes([ + return $this->mergeJson(new Attributes([ 'pact:matcher:type' => 'type', 'value' => $this->value, - ]); + ])); } public function formatExpression(): Expression diff --git a/tests/PhpPact/Consumer/Matcher/MatcherTest.php b/tests/PhpPact/Consumer/Matcher/MatcherTest.php index 6c0e7cbe..18a84d39 100644 --- a/tests/PhpPact/Consumer/Matcher/MatcherTest.php +++ b/tests/PhpPact/Consumer/Matcher/MatcherTest.php @@ -8,7 +8,10 @@ use PhpPact\Consumer\Matcher\Formatters\Json\JsonFormatter; use PhpPact\Consumer\Matcher\Generators\MockServerURL; use PhpPact\Consumer\Matcher\Generators\ProviderState; +use PhpPact\Consumer\Matcher\Generators\RandomBoolean; +use PhpPact\Consumer\Matcher\Generators\RandomDecimal; use PhpPact\Consumer\Matcher\Generators\RandomHexadecimal; +use PhpPact\Consumer\Matcher\Generators\RandomInt; use PhpPact\Consumer\Matcher\Generators\Uuid; use PhpPact\Consumer\Matcher\Matcher; use PhpPact\Consumer\Matcher\Matchers\ArrayContains; @@ -161,22 +164,44 @@ public function testTimestampRFC3339(): void $this->assertInstanceOf(JsonFormatter::class, $result->getFormatter()); } - public function testInteger(): void + #[TestWith([null, true])] + #[TestWith([123, false])] + public function testInteger(?int $value, bool $hasGenerator): void { - $this->assertInstanceOf(Type::class, $result = $this->matcher->integer()); + $this->assertInstanceOf(Type::class, $result = $this->matcher->integer($value)); $this->assertInstanceOf(JsonFormatter::class, $result->getFormatter()); + if ($hasGenerator) { + $this->assertInstanceOf(RandomInt::class, $result->getGenerator()); + } else { + $this->assertNull($result->getGenerator()); + } } - public function testBoolean(): void + #[TestWith([null, true])] + #[TestWith([true, false])] + #[TestWith([false, false])] + public function testBoolean(?bool $value, bool $hasGenerator): void { - $this->assertInstanceOf(Type::class, $result = $this->matcher->boolean()); + $this->assertInstanceOf(Type::class, $result = $this->matcher->boolean($value)); $this->assertInstanceOf(JsonFormatter::class, $result->getFormatter()); + if ($hasGenerator) { + $this->assertInstanceOf(RandomBoolean::class, $result->getGenerator()); + } else { + $this->assertNull($result->getGenerator()); + } } - public function testDecimal(): void + #[TestWith([null, true])] + #[TestWith([1.01, false])] + public function testDecimal(?float $value, bool $hasGenerator): void { - $this->assertInstanceOf(Type::class, $result = $this->matcher->decimal()); + $this->assertInstanceOf(Type::class, $result = $this->matcher->decimal($value)); $this->assertInstanceOf(JsonFormatter::class, $result->getFormatter()); + if ($hasGenerator) { + $this->assertInstanceOf(RandomDecimal::class, $result->getGenerator()); + } else { + $this->assertNull($result->getGenerator()); + } } public function testIntegerV3(): void diff --git a/tests/PhpPact/Consumer/Matcher/Matchers/EqualityTest.php b/tests/PhpPact/Consumer/Matcher/Matchers/EqualityTest.php index 35a857e2..1187f8e3 100644 --- a/tests/PhpPact/Consumer/Matcher/Matchers/EqualityTest.php +++ b/tests/PhpPact/Consumer/Matcher/Matchers/EqualityTest.php @@ -4,20 +4,23 @@ use PhpPact\Consumer\Matcher\Exception\InvalidValueException; use PhpPact\Consumer\Matcher\Formatters\Expression\ExpressionFormatter; +use PhpPact\Consumer\Matcher\Generators\RandomString; use PhpPact\Consumer\Matcher\Matchers\Equality; +use PhpPact\Consumer\Matcher\Model\GeneratorInterface; use PhpPact\Consumer\Matcher\Model\MatcherInterface; use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; class EqualityTest extends TestCase { - public function testFormatJson(): void + #[TestWith([new Equality('exact this string'), new RandomString(), '{"pact:matcher:type":"equality","pact:generator:type":"RandomString","size": 10,"value":"exact this string"}'])] + #[TestWith([new Equality('exact this string'), null, '{"pact:matcher:type":"equality","value":"exact this string"}'])] + public function testFormatJson(Equality $matcher, ?GeneratorInterface $generator, string $json): void { - $string = new Equality('exact this string'); - $this->assertSame( - '{"pact:matcher:type":"equality","value":"exact this string"}', - json_encode($string) - ); + $matcher = $matcher->withGenerator($generator); + $jsonEncoded = json_encode($matcher); + $this->assertIsString($jsonEncoded); + $this->assertJsonStringEqualsJsonString($json, $jsonEncoded); } #[TestWith([new Equality(new \stdClass()), 'object'])] diff --git a/tests/PhpPact/Consumer/Matcher/Matchers/NotEmptyTest.php b/tests/PhpPact/Consumer/Matcher/Matchers/NotEmptyTest.php index cfb65db0..70bb979e 100644 --- a/tests/PhpPact/Consumer/Matcher/Matchers/NotEmptyTest.php +++ b/tests/PhpPact/Consumer/Matcher/Matchers/NotEmptyTest.php @@ -4,20 +4,23 @@ use PhpPact\Consumer\Matcher\Exception\InvalidValueException; use PhpPact\Consumer\Matcher\Formatters\Expression\ExpressionFormatter; +use PhpPact\Consumer\Matcher\Generators\RandomString; use PhpPact\Consumer\Matcher\Matchers\NotEmpty; +use PhpPact\Consumer\Matcher\Model\GeneratorInterface; use PhpPact\Consumer\Matcher\Model\MatcherInterface; use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; class NotEmptyTest extends TestCase { - public function testFormatJson(): void + #[TestWith([new NotEmpty(['some text']), new RandomString(), '{"pact:matcher:type":"notEmpty","pact:generator:type":"RandomString","size": 10,"value":["some text"]}'])] + #[TestWith([new NotEmpty(['some text']), null, '{"pact:matcher:type":"notEmpty","value":["some text"]}'])] + public function testFormatJson(NotEmpty $matcher, ?GeneratorInterface $generator, string $json): void { - $array = new NotEmpty(['some text']); - $this->assertSame( - '{"pact:matcher:type":"notEmpty","value":["some text"]}', - json_encode($array) - ); + $matcher = $matcher->withGenerator($generator); + $jsonEncoded = json_encode($matcher); + $this->assertIsString($jsonEncoded); + $this->assertJsonStringEqualsJsonString($json, $jsonEncoded); } #[TestWith([new NotEmpty(new \stdClass()), 'object'])] diff --git a/tests/PhpPact/Consumer/Matcher/Matchers/TypeTest.php b/tests/PhpPact/Consumer/Matcher/Matchers/TypeTest.php index f821b7d6..1dd9942e 100644 --- a/tests/PhpPact/Consumer/Matcher/Matchers/TypeTest.php +++ b/tests/PhpPact/Consumer/Matcher/Matchers/TypeTest.php @@ -4,21 +4,25 @@ use PhpPact\Consumer\Matcher\Exception\InvalidValueException; use PhpPact\Consumer\Matcher\Formatters\Expression\ExpressionFormatter; +use PhpPact\Consumer\Matcher\Generators\RandomString; use PhpPact\Consumer\Matcher\Matchers\Type; +use PhpPact\Consumer\Matcher\Model\GeneratorInterface; use PhpPact\Consumer\Matcher\Model\MatcherInterface; use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; class TypeTest extends TestCase { - public function testFormatJson(): void + #[TestWith([new RandomString(), '{"pact:matcher:type":"type","pact:generator:type":"RandomString","size": 10,"value":{"key":"value"}}'])] + #[TestWith([null, '{"pact:matcher:type":"type","value":{"key":"value"}}'])] + public function testFormatJson(?GeneratorInterface $generator, string $json): void { $value = (object) ['key' => 'value']; - $object = new Type($value); - $this->assertSame( - '{"pact:matcher:type":"type","value":{"key":"value"}}', - json_encode($object) - ); + $matcher = new Type($value); + $matcher = $matcher->withGenerator($generator); + $jsonEncoded = json_encode($matcher); + $this->assertIsString($jsonEncoded); + $this->assertJsonStringEqualsJsonString($json, $jsonEncoded); } #[TestWith([new Type(new \stdClass()), 'object'])] diff --git a/tests/PhpPact/Xml/XmlBuilderTest.php b/tests/PhpPact/Xml/XmlBuilderTest.php index e5325d43..5ee528f1 100644 --- a/tests/PhpPact/Xml/XmlBuilderTest.php +++ b/tests/PhpPact/Xml/XmlBuilderTest.php @@ -59,7 +59,7 @@ public function testJsonSerialize(): void $this->builder->name('ns1:task'), $this->builder->attribute('id', $this->matcher->integerV3(1)), $this->builder->attribute('name', $this->matcher->string('Task 1')), - $this->builder->attribute('done', $this->matcher->boolean()), + $this->builder->attribute('done', $this->matcher->boolean(true)), $this->builder->contentLike('Task 1 description'), ), ),