Skip to content

Commit

Permalink
chore: Replace arrayLike by constrainedArrayLike
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Jul 28, 2023
1 parent 0b002bf commit ec0c70a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 deletions.
40 changes: 26 additions & 14 deletions src/PhpPact/Consumer/Matcher/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,36 @@ public function atMostLike(mixed $value, int $max): array
}

/**
* @param array<mixed> $values example of what the expected data would be
* @param int $min minimum number of objects to verify against
* @param int $min maximum number of objects to verify against
*
* @return array<string, mixed>
*/
public function arrayLike(array $values, ?int $min = null, ?int $max = null): array
{
if (($min !== null && $min <= 0) || ($min !== null && $max !== null && $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 = null, int $max = null, ?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' => $values,
'min' => $min,
'max' => $max,
'pact:matcher:type' => 'type',
] +
($min === null ? [] : ['min' => $min]) +
($max === null ? [] : ['max' => $max]);
'value' => array_fill(0, $elements, $value),
];
}

/**
Expand Down
48 changes: 26 additions & 22 deletions tests/PhpPact/Consumer/Matcher/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,45 +134,49 @@ public function testAtMostLike(object|array $value)
/**
* @throws Exception
*/
public function testArrayLikeNegativeMin()
public function testConstrainedArrayLikeCountLessThanMin()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid minimum number of elements');
$this->matcher->arrayLike(['text'], -2, 9);
$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 testArrayLikeMinLargerThanMax()
public function testConstrainedArrayLikeCountLargerThanMax()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid minimum number of elements');
$this->matcher->arrayLike(['text'], 10, 1);
}

public function dataProviderForArrayLikeTest()
{
return [
[null, null, []],
[2, null, ['min' => 2]],
[null, 5, ['max' => 5]],
[3, 8, ['min' => 3, 'max' => 8]],
];
$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 dataProviderForArrayLikeTest
* @dataProvider dataProviderForEachLikeTest
*/
public function testArrayLike(?int $min, ?int $max, array $expectedMinMax)
public function testConstrainedArrayLike(object|array $value)
{
$values = ['test1', 'test2'];
$eachValueMatcher = [
'value1' => [
'value' => 1,
'pact:matcher:type' => 'type',
],
'value2' => 2,
];
$expected = \json_encode([
'value' => $values,
'min' => 2,
'max' => 4,
'pact:matcher:type' => 'type',
] + $expectedMinMax);
'value' => [
$eachValueMatcher,
$eachValueMatcher,
$eachValueMatcher,
],
]);

$actual = \json_encode($this->matcher->arrayLike($values, $min, $max));
$actual = \json_encode($this->matcher->constrainedArrayLike($value, 2, 4, 3));

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

0 comments on commit ec0c70a

Please sign in to comment.