diff --git a/src/Illuminate/Testing/Fluent/Concerns/Has.php b/src/Illuminate/Testing/Fluent/Concerns/Has.php index 20bfe9d189e3..23f6b308db37 100644 --- a/src/Illuminate/Testing/Fluent/Concerns/Has.php +++ b/src/Illuminate/Testing/Fluent/Concerns/Has.php @@ -40,6 +40,38 @@ public function count($key, ?int $length = null): self return $this; } + /** + * Assert that the prop size is between a given minimum and maximum. + * + * @param int|string $min + * @param int|string $max + * @return $this + */ + public function countBetween(int|string $min, int|string $max): self + { + $path = $this->dotPath(); + + $prop = $this->prop(); + + PHPUnit::assertGreaterThanOrEqual( + $min, + count($prop), + $path + ? sprintf('Property [%s] size is not greater than or equal to [%s].', $path, $min) + : sprintf('Root level size is not greater than or equal to [%s].', $min) + ); + + PHPUnit::assertLessThanOrEqual( + $max, + count($prop), + $path + ? sprintf('Property [%s] size is not less than or equal to [%s].', $path, $max) + : sprintf('Root level size is not less than or equal to [%s].', $max) + ); + + return $this; + } + /** * Ensure that the given prop exists. * diff --git a/tests/Testing/Fluent/AssertTest.php b/tests/Testing/Fluent/AssertTest.php index c96b4bf21218..c4771c583285 100644 --- a/tests/Testing/Fluent/AssertTest.php +++ b/tests/Testing/Fluent/AssertTest.php @@ -285,6 +285,63 @@ public function testAssertCountFailsScoped() }); } + public function testAssertBetween() + { + $assert = AssertableJson::fromArray([ + 'foo', + 'bar', + 'baz', + ]); + + $assert->countBetween(1, 3); + } + + public function testAssertBetweenFails() + { + $assert = AssertableJson::fromArray([ + 'foo', + 'bar', + 'baz', + ]); + + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessage('Root level size is not less than or equal to [2].'); + + $assert->countBetween(1, 2); + } + + public function testAssertBetweenLowestValueFails() + { + $assert = AssertableJson::fromArray([ + 'foo', + 'bar', + 'baz', + ]); + + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessage('Root level size is not greater than or equal to [4].'); + + $assert->countBetween(4, 3); + } + + public function testAssertBetweenFailsScoped() + { + $assert = AssertableJson::fromArray([ + 'bar' => [ + 'baz' => 'example', + 'prop' => 'value', + 'foo' => 'value', + ], + ]); + + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessage('Property [bar] size is not less than or equal to [2].'); + + $assert->has('bar', function (AssertableJson $bar) { + $bar->countBetween(1, 2); + }); + } + public function testAssertMissing() { $assert = AssertableJson::fromArray([