Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Replace atLeastAndMostLike by constrainedArrayLike #327

Merged
merged 5 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ timestampRFC3339 | Regex match a timestamp using the RFC3339 format. | Value (De
like | Match a value against its data type. | Value | $matcher->like(12)
somethingLike | Alias to like matcher. | Value | $matcher->somethingLike(12)
eachLike | Match on an object like the example. | Value, Min (Defaults to 1) | $matcher->eachLike(12)
constrainedArrayLike | Behaves like the `eachLike` matcher, but also applies a minimum and maximum length validation on the length of the array. The optional `count` parameter controls the number of examples generated. | Value, Min, Max, count (Defaults to null) | $matcher->constrainedArrayLike('test', 1, 5, 3)
boolean | Match against boolean true. | none | $matcher->boolean()
integer | Match a value against integer. | Value (Defaults to 13) | $matcher->integer()
decimal | Match a value against float. | Value (Defaults to 13.01) | $matcher->decimal()
Expand Down
35 changes: 24 additions & 11 deletions src/PhpPact/Consumer/Matcher/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,35 @@ public function atMostLike(mixed $value, int $max): array
}

/**
* @param mixed $value example of what the expected data would be
* @param int $min minimum number of objects to verify against
*
* @return array<string, mixed>
*/
public function atLeastAndMostLike(mixed $value, int $min, int $max): array
{
if ($min <= 0 || $min > $max) {
throw new Exception('Invalid minimum number of elements');
* @param mixed $value example of what the expected data would be
* @param int $min minimum number of objects to verify against
* @param int $max maximum number of objects to verify against
* @param int|null $count number of examples to generate, defaults to one
*
* @return array<string, mixed>
*/
public function constrainedArrayLike(mixed $value, int $min, int $max, ?int $count = null): array
{
$elements = $count ?? $min;
if ($count !== null) {
if ($count < $min) {
throw new Exception(
"constrainedArrayLike has a minimum of {$min} but {$count} elements where requested." .
' Make sure the count is greater than or equal to the min.'
);
} elseif ($count > $max) {
throw new Exception(
"constrainedArrayLike has a maximum of {$max} but {$count} elements where requested." .
' Make sure the count is less than or equal to the max.'
);
}
}

return [
'value' => array_fill(0, $min, $value),
'pact:matcher:type' => 'type',
'min' => $min,
'max' => $max,
'pact:matcher:type' => 'type',
'value' => array_fill(0, $elements, $value),
];
}

Expand Down
29 changes: 21 additions & 8 deletions tests/PhpPact/Consumer/Matcher/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,29 @@ public function testAtMostLike(object|array $value)
/**
* @throws Exception
*/
public function testAtLeastAndMostLikeInvalidMin()
public function testConstrainedArrayLikeCountLessThanMin()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid minimum number of elements');
$this->matcher->atLeastAndMostLike('text', 10, 1);
$this->expectExceptionMessage('constrainedArrayLike has a minimum of 2 but 1 elements where requested.' .
' Make sure the count is greater than or equal to the min.');
$this->matcher->constrainedArrayLike('text', 2, 4, 1);
}

/**
* @throws Exception
*/
public function testConstrainedArrayLikeCountLargerThanMax()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('constrainedArrayLike has a maximum of 5 but 7 elements where requested.' .
' Make sure the count is less than or equal to the max.');
$this->matcher->constrainedArrayLike('text', 3, 5, 7);
}

/**
* @dataProvider dataProviderForEachLikeTest
*/
public function testAtLeastAndMostLike(object|array $value)
public function testConstrainedArrayLike(object|array $value)
{
$eachValueMatcher = [
'value1' => [
Expand All @@ -154,16 +166,17 @@ public function testAtLeastAndMostLike(object|array $value)
'value2' => 2,
];
$expected = \json_encode([
'min' => 2,
'max' => 4,
'pact:matcher:type' => 'type',
'value' => [
$eachValueMatcher,
$eachValueMatcher,
$eachValueMatcher,
],
'pact:matcher:type' => 'type',
'min' => 2,
'max' => 4,
]);

$actual = \json_encode($this->matcher->atLeastAndMostLike($value, 2, 4));
$actual = \json_encode($this->matcher->constrainedArrayLike($value, 2, 4, 3));

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