Skip to content

Commit

Permalink
Merge pull request #358 from tienvx/each-value-matcher
Browse files Browse the repository at this point in the history
feat: Add eachValue matcher
  • Loading branch information
tienvx authored Oct 31, 2023
2 parents 61a48bf + f08a32a commit 0cfd132
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
7 changes: 7 additions & 0 deletions example/matchers/consumer/tests/Service/MatchersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public function testGetMatchers()
'c' => 'ccc',
]),
'contentType' => $this->matcher->contentType('text/html'),
'eachValue' => $this->matcher->eachValue(
['vehicle 1' => 'car'],
[$this->matcher->regex(null, 'car|bike|motorbike')]
),
]);

$config = new MockServerConfig();
Expand Down Expand Up @@ -147,6 +151,9 @@ public function testGetMatchers()
'ccc',
],
'contentType' => 'text/html',
'eachValue' => [
'vehicle 1' => 'car',
],
], $matchersResult);
}
}
18 changes: 18 additions & 0 deletions example/matchers/pacts/matchersConsumer-matchersProvider.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
"eachLike": [
"item"
],
"eachValue": {
"vehicle 1": "car"
},
"email": "[email protected]",
"equal": "exact this value",
"hexadecimal": "F7A16",
Expand Down Expand Up @@ -272,6 +275,21 @@
}
]
},
"$.eachValue": {
"combine": "AND",
"matchers": [
{
"match": "eachValue",
"rules": [
{
"match": "regex",
"regex": "car|bike|motorbike"
}
],
"value": "{\"vehicle 1\":\"car\"}"
}
]
},
"$.email": {
"combine": "AND",
"matchers": [
Expand Down
4 changes: 4 additions & 0 deletions example/matchers/provider/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
</body>
</html>
HTML,
'eachValue' => [
'item 1' => 'bike',
'item 2' => 'motorbike',
],
]));

return $response->withHeader('Content-Type', 'application/json');
Expand Down
17 changes: 17 additions & 0 deletions src/PhpPact/Consumer/Matcher/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,4 +660,21 @@ public function contentType(string $contentType): array
'pact:matcher:type' => 'contentType',
];
}

/**
* Allows defining matching rules to apply to the values in a collection. For maps, delgates to the Values matcher.
*
* @param array<string, mixed> $values
* @param array<mixed> $rules
*
* @return array<string, mixed>
*/
public function eachValue(array $values, array $rules): array
{
return [
'rules' => $rules,
'value' => $values,
'pact:matcher:type' => 'eachValue',
];
}
}
20 changes: 20 additions & 0 deletions tests/PhpPact/Consumer/Matcher/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -887,4 +887,24 @@ public function testContentType()

$this->assertEquals($expected, $actual);
}

public function testEachValue()
{
$values = [
'vehicle 1' => 'car',
'vehicle 2' => 'bike',
'vehicle 3' => 'motorbike'
];
$rules = [
$this->matcher->regex('car', 'car|bike|motorbike'),
];
$expected = [
'rules' => $rules,
'value' => $values,
'pact:matcher:type' => 'eachValue',
];
$actual = $this->matcher->eachValue($values, $rules);

$this->assertEquals($expected, $actual);
}
}

0 comments on commit 0cfd132

Please sign in to comment.