Core\Test::assertNull
Core\Test::assertNull(mixed $actual, {message?: string}): voidCore\Test::assertNull( mixed $actual, {message?: string}, ): void
Core\Test::assertNull() checks that a value is null. The value may have any type. When it is null, the check passes and nothing happens.
When the value is not null, the check throws a Core\Test\Failure. The message shows the value. The option message adds your own text in front of it.
The check only tests for null. The values 0, "", false and an empty array are not null, so the check fails for each of them.
Use it to test code that returns null when it finds nothing, for example a search for a user.
The examples below show a check that passes, the message of a failed check, and a test of a search with no result.
Parameters
$actualmixedThe value under test.
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$actualis notnull. The failure is recorded before it is thrown, so acatchdoes not remove it from the test's result.
Examples
Check a missing value
<?nvs// Shows how to check that a value is `null`.
array<string> $colors = ['red' => '#ff0000', 'green' => '#00ff00'];
// "blue" is not a key of the array, so the result is `null`.?string $blue = $colors['blue'] ?? null;
// The value is `null`, so the check passes and nothing is thrown.Core\Test::assertNull($blue);echo "the check passed\n";the check passedRead a failure
<?nvs// Shows what happens when the value is not `null`, and how to add your own message.
// `0`, `""` and `false` are not `null`. Each check throws a `Core\Test\Failure`.// The message shows the value.array<mixed> $values = [0, '', false];foreach ($values as mixed $value) { try { Core\Test::assertNull($value); } 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::assertNull('admin', { message: 'a guest has no role' });} catch (Core\Test\Failure $failure) { echo $failure->message, "\n";}Core\Test::assertNull failed: `$actual` is 0Core\Test::assertNull failed: `$actual` is ""Core\Test::assertNull failed: `$actual` is falsea guest has no role: Core\Test::assertNull failed: `$actual` is "admin"Test a search with no result
<?nvs// Shows a test of a search that must return `null` when nothing matches.
final class User{ public function constructor(public string $email, public string $name) {}}
final class Users{ // Returns the user with this email address, or `null` when there is none. public static function findByEmail(array<User> $users, string $email): ?User { foreach ($users as User $user) { if (Core\Str::lower($user->email) == Core\Str::lower($email)) { return $user; } } return null; }}
array<User> $users = [ new User('ana@example.com', 'Ana'), new User('ben@example.com', 'Ben'),];
// The search finds Ben, even with capital letters in the address.?User $ben = Users::findByEmail($users, 'Ben@Example.com');echo 'found: ', $ben?->name ?? 'nobody', "\n";
// Nobody has this address, so the search returns `null` and the check passes.Core\Test::assertNull(Users::findByEmail($users, 'carl@example.com'));echo "no user for carl@example.com\n";found: Benno user for carl@example.com