Skip to content

Boolean Assertions

Boolean assertions validate boolean values and truthiness.

Assert that a value is boolean true.

use Cline\Assert\Assertions\Assertion;
Assertion::true($value);
Assertion::true($isActive, 'Must be active');

Assert that a value is boolean false.

Assertion::false($value);
Assertion::false($isDeleted, 'Must not be deleted');

Assert that a value is a boolean (true or false).

Assertion::boolean($value);
Assertion::boolean($flag, 'Value must be boolean');

These assertions check for actual boolean values only, not truthy/falsy values:

// Pass
Assertion::true(true);
Assertion::false(false);
Assertion::boolean(true);
// Fail - These are NOT booleans
Assertion::true(1); // integer, not boolean
Assertion::false(0); // integer, not boolean
Assertion::false(null); // null, not boolean
use Cline\Assert\Assert;
Assert::that($isActive)
->boolean()
->true('User must be active');
Assert::that($isDeleted)
->boolean()
->false('Record must not be deleted');
Assert::that($config['feature_enabled'])
->boolean()
->true('Feature must be enabled');
Assert::that($user->is_active)
->boolean('is_active must be boolean')
->true('User account must be active');
Assert::lazy()
->that($config['debug'], 'debug')->boolean()
->that($config['cache_enabled'], 'cache_enabled')->boolean()
->verifyNow();
Assert::that($user->hasPermission('admin'))
->boolean('Permission check must return boolean')
->true('User must have admin permission');

If you need to accept truthy/falsy values, convert them first:

$isActive = (bool) $value;
Assertion::boolean($isActive);

Many databases store booleans as integers:

// Database returns 1/0
$row['is_active'] = 1;
// Convert first
$isActive = (bool) $row['is_active'];
Assertion::boolean($isActive);
function setActive(bool $value) {
Assertion::boolean($value);
$this->isActive = $value;
}
Assertion::true($isVerified, 'Email address must be verified before proceeding');