Skip to content

Commit

Permalink
Merge pull request #681 from tienvx/add-each-value-matcher-complex-va…
Browse files Browse the repository at this point in the history
…lue-example

chore: Add eachValue matcher's complex value example
  • Loading branch information
tienvx authored Oct 14, 2024
2 parents db10175 + d5f5f5c commit ff2d234
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
42 changes: 42 additions & 0 deletions example/matchers/consumer/tests/Service/MatchersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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],
Expand Down
34 changes: 31 additions & 3 deletions example/matchers/pacts/matchersConsumer-matchersProvider.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]",
"equal": "exact this value",
"equalArray": [
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions example/matchers/provider/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/PhpPact/Consumer/Matcher/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ public function contentType(string $contentType): ContentType
* Allows defining matching rules to apply to the keys in a map
*
* @param array<string, mixed>|object $values
* @param MatcherInterface[] $rules
* @param MatcherInterface[] $rules
*/
public function eachKey(array|object $values, array $rules): EachKey
{
Expand Down

0 comments on commit ff2d234

Please sign in to comment.