Skip to content

Core\Test::assertDoesNotThrow

Core\Test::assertDoesNotThrow(
    callable(): mixed $body,
    {message?: string},
): void

Core\Test::assertDoesNotThrow() checks that a piece of code runs without an error. You pass the code as a function with no parameters. The check calls it once. The value it returns is not used.

When the function throws an exception, the check throws a Core\Test\Failure in its place. The message contains the message of the exception. The option message adds your own text in front of it. A failed check inside the function also counts as an error, so this check fails too.

Use it for a test whose whole point is that a call works, for example an import that must skip bad input. A test that checks a result uses Core\Test::assertEquals on the result.

The examples below show a call that works, the message of a failed check, and a test of an import with broken lines.

Parameters

  • $bodycallable(): mixedThe function to run. A value it returns is ignored.

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. A FATAL error or an exit inside $body is not caught, and it is not changed.

Errors

  • Core\Test\Failure$body threw an error. A check that failed inside $body counts too, and it still counts as a failure of its own. The failure message includes the error's message. The failure is recorded before it is thrown.

Examples

Check that a call succeeds

<?nvs
// Shows how to check that a piece of code runs without an error.
class Settings
{
public static function load(string $text): void
{
if (!Core\Str::contains($text, '=')) {
throw new LogicError('a setting needs a name and a value');
}
echo 'loaded: ', $text, "\n";
}
}
// The function is the code to run. It runs once, and it throws no error,
// so the check passes and nothing is thrown.
Core\Test::assertDoesNotThrow(fn(): void => { Settings::load('mode=fast'); });
echo "the check passed\n";
Output
loaded: mode=fast
the check passed

Read a failure

<?nvs
// Shows what happens when the code throws an error, and how to add your own message.
class Settings
{
public static function load(string $text): void
{
if (!Core\Str::contains($text, '=')) {
throw new LogicError('a setting needs a name and a value');
}
}
}
// The code throws a `LogicError`, so the check throws a `Core\Test\Failure`.
// The message contains the message of the `LogicError`.
try {
Core\Test::assertDoesNotThrow(fn(): void => { Settings::load('mode'); });
} 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::assertDoesNotThrow(
fn(): void => { Settings::load('mode'); },
{ message: 'a setting without a value is allowed' },
);
} catch (Core\Test\Failure $failure) {
echo $failure->message, "\n";
}
Output
Core\Test::assertDoesNotThrow failed: `$body` threw: a setting needs a name and a value
a setting without a value is allowed: Core\Test::assertDoesNotThrow failed: `$body` threw: a setting needs a name and a value

Test that bad input is skipped

<?nvs
// Shows a test of an import that must skip bad lines without an error.
class Import
{
// Returns the sum of the numbers in the lines. Lines that are not numbers are skipped.
public static function total(array<string> $lines): int
{
int $total = 0;
foreach ($lines as string $line) {
?int $amount = $line as ?int;
if ($amount != null) {
$total = $total + $amount;
}
}
return $total;
}
}
array<string> $lines = ['120', '', 'abc', '80', '12.5'];
// The import gets empty and broken lines. The check passes when the import
// runs to the end without an error.
Core\Test::assertDoesNotThrow(fn(): void => { Import::total($lines); });
echo 'total: ', Import::total($lines), "\n";
Output
total: 200