Skip to content

Core\Test::expectFailure

Core\Test::expectFailure(callable(): mixed $body): voidCore\Test::expectFailure(
    callable(): mixed $body,
): void

Core\Test::expectFailure() runs a function and checks that a check inside it failed. You pass the code as a function with no parameters. When a check inside it fails, expectFailure passes. The failed checks then do not count against your test.

Inside a test, a failed check counts even when your code catches the Core\Test\Failure. So try and catch cannot hide a failure. expectFailure is the one way to say that a failure is expected.

When no check inside the function fails, expectFailure throws a Core\Test\Failure. When the function throws an error of another class, that error passes through unchanged.

Use it to test a check you wrote yourself. It should pass for good values and fail for bad ones.

The examples below show a failure that is expected, what happens when nothing fails, and a test of your own check for prices.

Parameters

  • $bodycallable(): mixedThe function to run. A failed check counts even when $body catches the Core\Test\Failure itself.

Return value

Returns void. Nothing. The failed checks inside $body are removed from the test's result, and the Core\Test\Failure it threw is caught. The checks that passed still count.

Errors

  • Core\Test\FailureNo check inside $body failed. This failure is recorded before it is thrown, so a catch does not remove it from the test's result.

Examples

Expect a check to fail

<?nvs
// Shows how to say that a check is expected to fail.
// 2 + 2 is not 5, so `assertSame` fails inside the function.
// `expectFailure` catches the failure, and the program continues.
Core\Test::expectFailure(fn(): void => Core\Test::assertSame(2 + 2, 5));
echo "the check failed, as expected\n";
// A failed check counts even when the code catches it.
// So `expectFailure` passes here too.
Core\Test::expectFailure(fn(): void => {
try {
Core\Test::assertTrue(false);
} catch (Core\Test\Failure $failure) {
echo 'caught: ', $failure->message, "\n";
}
});
echo "the caught failure still counted\n";
Output
the check failed, as expected
caught: Core\Test::assertTrue failed: `$actual` is false
the caught failure still counted

When nothing fails

<?nvs
// Shows what happens when no check fails, and when the code throws another error.
// Every check inside the function passes.
// So `expectFailure` throws a `Core\Test\Failure`.
try {
Core\Test::expectFailure(fn(): void => Core\Test::assertSame('a', 'a'));
} catch (Core\Test\Failure $failure) {
echo $failure->message, "\n";
}
// The function throws a `RuntimeError`.
// The `RuntimeError` passes through `expectFailure` unchanged.
try {
Core\Test::expectFailure(fn(): void => {
throw new RuntimeError('the file is missing');
});
} catch (RuntimeError $error) {
echo 'RuntimeError: ', $error->message, "\n";
}
Output
Core\Test::expectFailure failed: the callable ran without a failed assertion
RuntimeError: the file is missing

Test your own check

<?nvs
// Shows a test of a check you wrote yourself: it must pass for a good price and fail for a bad one.
final class PriceCheck
{
// A valid price is more than 0 cents and at most 1,000,000 cents.
public static function assertValid(int $cents): void
{
Core\Test::assertTrue($cents > 0, { message: 'price ' . $cents . ' is not positive' });
Core\Test::assertTrue($cents <= 1000000, { message: 'price ' . $cents . ' is too high' });
}
}
// A good price: both checks pass.
PriceCheck::assertValid(1999);
echo "1999 is valid\n";
// Bad prices: one check fails each time, as expected.
Core\Test::expectFailure(fn(): void => PriceCheck::assertValid(0));
Core\Test::expectFailure(fn(): void => PriceCheck::assertValid(-5));
Core\Test::expectFailure(fn(): void => PriceCheck::assertValid(2000000));
echo "0, -5 and 2000000 are not valid\n";
Output
1999 is valid
0, -5 and 2000000 are not valid