Skip to content
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
30 changes: 30 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,36 @@ public static function null(
: StaticState::fail(AssertException::compare(null, $actual, $message));
}

/**
* Checks if a value is blank.
*
* Successful only for values representing absence of data:
* null, empty string, empty array or countable object with no elements.
*
* Unlike empty(), does not consider false, 0, and "0" as blank,
* since they represent valid data.
* @param mixed $actual The actual value to check for blank.
* @param string $message Short description about what exactly is being asserted.
* @throws AssertException when the assertion fails.
*/
public static function blank(
mixed $actual,
string $message = '',
): void {
if (
$actual === null || $actual === '' || $actual === [] || ($actual instanceof \Countable && \count($actual) === 0)
) {
StaticState::log('Assert `blank`', $message);
return;
}
StaticState::fail(AssertException::compare(
null,
$actual,
$message,
'Failed asserting that `%2$s` does not contain any data',
));
}

/**
* Fails the test.
*
Expand Down
44 changes: 44 additions & 0 deletions tests/Assert/Self/AssertBlank.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Tests\Assert\Self;

use Testo\Assert;
use Testo\Attribute\Test;

/**
* Assertion examples.
*/
final class AssertBlank
{
#[Test]
public function checkBlankData(): void
{
Assert::blank([]);
Assert::blank("");
Assert::blank(null);
Assert::blank(new \ArrayIterator());
}

#[Test]
public function checkZeroIsNotBlank(): void
{
Assert::exception(Assert\State\AssertException::class);
Assert::blank(0);
}

#[Test]
public function checkZeroStringIsNotBlank(): void
{
Assert::exception(Assert\State\AssertException::class);
Assert::blank("0");
}

#[Test]
public function checkFalseIsNotBlank(): void
{
Assert::exception(Assert\State\AssertException::class);
Assert::blank(false);
}
}