Core\Test::assertTrue
Core\Test::assertTrue(bool $actual, {message?: string}): voidCore\Test::assertTrue( bool $actual, {message?: string}, ): void
Core\Test::assertTrue() checks that a value is true. The value must be a bool. A call with a number, a string or an array does not compile. Write a comparison instead, for example $count > 0.
When the value is true, the check passes and nothing happens. When the value is false, the check throws a Core\Test\Failure. The message only says that the value is false. The option message adds your own text at the start, so you can say what you expected.
Use it for a condition that no other check tests, for example a rule about passwords or a date that must be in the future.
The examples below show checks that pass, the message of a failed check, and a test of a password rule.
Parameters
$actualboolTheboolto check.
Options
messagestringoptionalYour own text, added at the start of the failure message. The default is no text.
Return value
Returns void. Nothing. The check is recorded as passed.
Errors
Core\Test\Failure$actualisfalse. The failure is recorded before it is thrown, so acatchdoes not remove it from the test's result.
Examples
Check a condition
<?nvs// Shows how to check that a condition is `true`.
array<int> $scores = [72, 85, 91];
// Every score is 50 or more, so each comparison is `true` and each check passes.foreach ($scores as int $score) { Core\Test::assertTrue($score >= 50);}
// `Core\Arr::count` returns 3, so this comparison is `true` too.Core\Test::assertTrue(Core\Arr::count($scores) == 3);echo "every check passed\n";every check passedRead a failure
<?nvs// Shows what happens when the value is `false`, and how to add your own message.
array<string> $cart = [];
// The cart is empty, so the comparison is `false`.// The check throws a `Core\Test\Failure`.try { Core\Test::assertTrue(Core\Arr::count($cart) > 0);} catch (Core\Test\Failure $failure) { echo $failure->message, "\n";}
// The option `message` adds your own text at the start of the message.try { Core\Test::assertTrue(Core\Arr::count($cart) > 0, { message: 'the cart has a product' });} catch (Core\Test\Failure $failure) { echo $failure->message, "\n";}Core\Test::assertTrue failed: `$actual` is falsethe cart has a product: Core\Test::assertTrue failed: `$actual` is falseTest a password rule
<?nvs// Shows a test of a rule that decides whether a password is strong enough.
final class Password{ // A strong password has 12 or more characters and at least one digit. public static function isStrong(string $password): bool { if (Core\Str::length($password) < 12) { return false; } array<string> $digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; foreach ($digits as string $digit) { if (Core\Str::contains($password, $digit)) { return true; } } return false; }}
// These passwords follow the rule, so each check passes.Core\Test::assertTrue(Password::isStrong('blue-horse-42'));Core\Test::assertTrue(Password::isStrong('1234567890ab'));
// These do not. `!` turns `false` into `true`, so each check passes.Core\Test::assertTrue(!Password::isStrong('short1'), { message: 'too short' });Core\Test::assertTrue(!Password::isStrong('no-digits-at-all'), { message: 'no digit' });
echo "the password rule works\n";the password rule works