From ec0c70aa1f833297ff9f03fcb00ca279385d2d6f Mon Sep 17 00:00:00 2001 From: tienvx Date: Sat, 29 Jul 2023 00:20:42 +0700 Subject: [PATCH] chore: Replace arrayLike by constrainedArrayLike --- src/PhpPact/Consumer/Matcher/Matcher.php | 40 ++++++++++------ .../PhpPact/Consumer/Matcher/MatcherTest.php | 48 ++++++++++--------- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/src/PhpPact/Consumer/Matcher/Matcher.php b/src/PhpPact/Consumer/Matcher/Matcher.php index 4b0ed3fb..0fbbf393 100644 --- a/src/PhpPact/Consumer/Matcher/Matcher.php +++ b/src/PhpPact/Consumer/Matcher/Matcher.php @@ -93,24 +93,36 @@ public function atMostLike(mixed $value, int $max): array } /** - * @param array $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 - */ - 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 + */ + 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), + ]; } /** diff --git a/tests/PhpPact/Consumer/Matcher/MatcherTest.php b/tests/PhpPact/Consumer/Matcher/MatcherTest.php index 9c1fe35b..f5ab12eb 100644 --- a/tests/PhpPact/Consumer/Matcher/MatcherTest.php +++ b/tests/PhpPact/Consumer/Matcher/MatcherTest.php @@ -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); }