From 856e603c3300b4f400656ed1c9f640bd77736df3 Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Sun, 6 Oct 2024 11:45:54 +0700 Subject: [PATCH] chore: Add eachValue matcher's complex value example --- .../consumer/tests/Service/MatchersTest.php | 42 +++++++++++++++++++ .../matchersConsumer-matchersProvider.json | 34 +++++++++++++-- example/matchers/provider/public/index.php | 14 +++++++ src/PhpPact/Consumer/Matcher/Matcher.php | 8 ++-- 4 files changed, 91 insertions(+), 7 deletions(-) diff --git a/example/matchers/consumer/tests/Service/MatchersTest.php b/example/matchers/consumer/tests/Service/MatchersTest.php index 6078223e..4db6b9d6 100644 --- a/example/matchers/consumer/tests/Service/MatchersTest.php +++ b/example/matchers/consumer/tests/Service/MatchersTest.php @@ -112,6 +112,34 @@ public function testGetMatchers(): void ['vehicle 1' => 'car'], [$this->matcher->regex(null, 'car|bike|motorbike')] ), + 'eachValueComplexValue' => $this->matcher->eachValue( + (object) [ + '95481709-5868-4d67-8d4b-410728b207a0' => [ + 'title' => 'Big Buck Bunny', + 'year' => 2008, + 'length' => '10m', + 'rating' => 6.4, + ], + '46c9c929-50b7-4566-a0bf-114859bdf9ff' => [ + 'title' => 'Wing It!', + 'year' => 2023, + 'length' => '3m', + 'rating' => 7.3, + ], + ], + [ + $this->matcher->like( + // WARNING Example value will be ignored + [ + // therefore, these matchers are not recognized + 'title' => $this->matcher->includes('Open'), + 'year' => $this->matcher->equal(2006), + 'length' => $this->matcher->regex('11m', '\d+m'), + 'rating' => $this->matcher->decimal(5.6), + ] + ) + ] + ), 'url' => $this->matcher->url('http://localhost:8080/users/1234/posts/latest', '.*(\\/users\\/\\d+\\/posts\\/latest)$', false), 'matchAll' => $this->matcher->matchAll( ['desktop' => '2000 usd'], @@ -221,6 +249,20 @@ public function testGetMatchers(): void 'eachValue' => [ 'vehicle 1' => 'car', ], + 'eachValueComplexValue' => [ + '95481709-5868-4d67-8d4b-410728b207a0' => [ + 'title' => 'Big Buck Bunny', + 'year' => 2008, + 'length' => '10m', + 'rating' => 6.4, + ], + '46c9c929-50b7-4566-a0bf-114859bdf9ff' => [ + 'title' => 'Wing It!', + 'year' => 2023, + 'length' => '3m', + 'rating' => 7.3, + ], + ], 'url' => 'http://localhost:8080/users/1234/posts/latest', 'matchAll' => ['desktop' => '2000 usd'], 'atLeast' => [null, null], diff --git a/example/matchers/pacts/matchersConsumer-matchersProvider.json b/example/matchers/pacts/matchersConsumer-matchersProvider.json index 2a0f82a7..7d5618e5 100644 --- a/example/matchers/pacts/matchersConsumer-matchersProvider.json +++ b/example/matchers/pacts/matchersConsumer-matchersProvider.json @@ -173,6 +173,20 @@ "eachValue": { "vehicle 1": "car" }, + "eachValueComplexValue": { + "46c9c929-50b7-4566-a0bf-114859bdf9ff": { + "length": "3m", + "rating": 7.3, + "title": "Wing It!", + "year": 2023 + }, + "95481709-5868-4d67-8d4b-410728b207a0": { + "length": "10m", + "rating": 6.4, + "title": "Big Buck Bunny", + "year": 2008 + } + }, "email": "hello@pact.io", "equal": "exact this value", "equalArray": [ @@ -440,6 +454,20 @@ } ] }, + "$.eachValueComplexValue": { + "combine": "AND", + "matchers": [ + { + "match": "eachValue", + "rules": [ + { + "match": "type" + } + ], + "value": "{\"46c9c929-50b7-4566-a0bf-114859bdf9ff\":{\"length\":\"3m\",\"rating\":7.3,\"title\":\"Wing It!\",\"year\":2023},\"95481709-5868-4d67-8d4b-410728b207a0\":{\"length\":\"10m\",\"rating\":6.4,\"title\":\"Big Buck Bunny\",\"year\":2008}}" + } + ] + }, "$.email": { "combine": "AND", "matchers": [ @@ -726,9 +754,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/matchers/provider/public/index.php b/example/matchers/provider/public/index.php index 426acf7c..42345413 100644 --- a/example/matchers/provider/public/index.php +++ b/example/matchers/provider/public/index.php @@ -72,6 +72,20 @@ 'item 1' => 'bike', 'item 2' => 'motorbike', ], + 'eachValueComplexValue' => [ + '35251397-d0d3-4178-af7d-4eb8ce7d8baa' => [ + 'title' => 'Sintel', + 'year' => 2010, + 'length' => '14', + 'rating' => 7.4, + ], + '4ef94471-9ff5-476f-92f3-0bcf89166427' => [ + 'title' => 'Tears of Steel', + 'year' => 2012, + 'length' => '12', + 'rating' => 5.5, + ], + ], 'url' => 'https://www.example.com/users/1234/posts/latest', 'matchAll' => [ 'tablet' => '300 usd', diff --git a/src/PhpPact/Consumer/Matcher/Matcher.php b/src/PhpPact/Consumer/Matcher/Matcher.php index 92e8629f..f5e5fbc6 100644 --- a/src/PhpPact/Consumer/Matcher/Matcher.php +++ b/src/PhpPact/Consumer/Matcher/Matcher.php @@ -415,10 +415,10 @@ public function contentType(string $contentType): MatcherInterface /** * Allows defining matching rules to apply to the keys in a map * - * @param array $values + * @param array|object $values * @param MatcherInterface[] $rules */ - public function eachKey(array $values, array $rules): MatcherInterface + public function eachKey(array|object $values, array $rules): MatcherInterface { return $this->withFormatter(new EachKey($values, $rules)); } @@ -426,10 +426,10 @@ public function eachKey(array $values, array $rules): MatcherInterface /** * Allows defining matching rules to apply to the values in a collection. For maps, delgates to the Values matcher. * - * @param array $values + * @param array|object $values * @param MatcherInterface[] $rules */ - public function eachValue(array $values, array $rules): MatcherInterface + public function eachValue(array|object $values, array $rules): MatcherInterface { return $this->withFormatter(new EachValue($values, $rules)); }