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

Add between to AssertableJson #52479

Merged
merged 6 commits into from
Aug 16, 2024
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
32 changes: 32 additions & 0 deletions src/Illuminate/Testing/Fluent/Concerns/Has.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
57 changes: 57 additions & 0 deletions tests/Testing/Fluent/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
Loading