Skip to content

Commit

Permalink
Add between to AssertableJson (#52479)
Browse files Browse the repository at this point in the history
* Added `between` tests

* Added `between` assertion

* pint

* pint

* formatting

* min max

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
rudashi and taylorotwell authored Aug 16, 2024
1 parent 451bc21 commit 58718c6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
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

0 comments on commit 58718c6

Please sign in to comment.