Skip to content

Commit

Permalink
Merge pull request #357 from tienvx/each-key-matcher
Browse files Browse the repository at this point in the history
feat: Add eachKey matcher
  • Loading branch information
tienvx authored Oct 31, 2023
2 parents 0cfd132 + b6db77e commit 9c6a042
Show file tree
Hide file tree
Showing 5 changed files with 65 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'),
'eachKey' => $this->matcher->eachKey(
['page 3' => 'example text'],
[$this->matcher->regex(null, '^page \d+$')]
),
'eachValue' => $this->matcher->eachValue(
['vehicle 1' => 'car'],
[$this->matcher->regex(null, 'car|bike|motorbike')]
Expand Down Expand Up @@ -151,6 +155,9 @@ public function testGetMatchers()
'ccc',
],
'contentType' => 'text/html',
'eachKey' => [
'page 3' => 'example text',
],
'eachValue' => [
'vehicle 1' => 'car',
],
Expand Down
18 changes: 18 additions & 0 deletions example/matchers/pacts/matchersConsumer-matchersProvider.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
"dateTimeWithMillisISO8601": "2015-08-06T16:53:10.123+01:00",
"datetime": "2000-10-31T01:30:00",
"decimal": 79.01,
"eachKey": {
"page 3": "example text"
},
"eachLike": [
"item"
],
Expand Down Expand Up @@ -266,6 +269,21 @@
}
]
},
"$.eachKey": {
"combine": "AND",
"matchers": [
{
"match": "eachKey",
"rules": [
{
"match": "regex",
"regex": "^page \\d+$"
}
],
"value": "{\"page 3\":\"example text\"}"
}
]
},
"$.eachLike": {
"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,
'eachKey' => [
'page 1' => 'Hello',
'page 2' => 'World',
],
'eachValue' => [
'item 1' => 'bike',
'item 2' => 'motorbike',
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 @@ -661,6 +661,23 @@ public function contentType(string $contentType): array
];
}

/**
* Allows defining matching rules to apply to the keys in a map
*
* @param array<string, mixed> $values
* @param array<mixed> $rules
*
* @return array<string, mixed>
*/
public function eachKey(array $values, array $rules): array
{
return [
'rules' => $rules,
'value' => $values,
'pact:matcher:type' => 'eachKey',
];
}

/**
* Allows defining matching rules to apply to the values in a collection. For maps, delgates to the Values matcher.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/PhpPact/Consumer/Matcher/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,25 @@ public function testContentType()
$this->assertEquals($expected, $actual);
}

public function testEachKey()
{
$values = [
'page 1' => 'Hello',
'page 2' => 'World',
];
$rules = [
$this->matcher->regex('page 3', '^page \d+$'),
];
$expected = [
'rules' => $rules,
'value' => $values,
'pact:matcher:type' => 'eachKey',
];
$actual = $this->matcher->eachKey($values, $rules);

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

public function testEachValue()
{
$values = [
Expand Down

0 comments on commit 9c6a042

Please sign in to comment.