Core\Test::assertThrows
Core\Test::assertThrows() checks that a piece of code throws an error of a given class. You pass the code as a function with no parameters, and the name of the class, for example LogicError::class. The check calls the function once.
You must write the class as C::class in the call. C must be Throwable or a class that extends it. A string or a variable does not compile.
The check passes when the function throws that class or a class that extends it. The error is caught, so your test continues after the check.
When the function throws no error, or an error of another class, the check throws a Core\Test\Failure. The message names the class that was thrown and its message. The option message adds your own text in front of it.
The examples below show a check that passes, the messages of failed checks, and a test of a payment that is larger than the balance.
Parameters
$bodycallable(): mixedThe function to run. A value it returns is ignored.$expectedstringThe full class name, asParseError::classgives it. The thrown class matches if it is this class or extends it.
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. A check that fails inside $body still counts as a failure of its own.
Errors
Core\Test\Failure$bodyreturned without throwing, or it threw a class that is not$expected. The message names the class it threw and the message of that error. The failure is recorded before it is thrown.
Examples
Check that bad input throws
<?nvs// Shows how to check that a piece of code throws an error.
class Age{ public static function parse(string $text): int { ?int $age = $text as ?int; if ($age == null) { throw new LogicError('not a number: ' . $text); } if ($age < 0) { throw new LogicError('an age is never negative: ' . $text); } return $age; }}
// The function is the code to run. `LogicError::class` is the class of error you expect.// "-4" is not an age, so the code throws a `LogicError` and the check passes.Core\Test::assertThrows(fn(): void => { Age::parse('-4'); }, LogicError::class);echo "the check passed\n";the check passedRead a failure
<?nvs// Shows what happens when the code throws no error or the wrong error.
class Age{ public static function parse(string $text): int { ?int $age = $text as ?int; if ($age == null) { throw new LogicError('not a number: ' . $text); } return $age; }}
// "-4" is a number, so the code throws no error and the check throws a `Core\Test\Failure`.try { Core\Test::assertThrows(fn(): void => { Age::parse('-4'); }, LogicError::class);} catch (Core\Test\Failure $failure) { echo $failure->message, "\n";}
// The code throws a `LogicError`, not an `IOError`. The message names the error it threw.try { Core\Test::assertThrows(fn(): void => { Age::parse('ten'); }, IOError::class);} 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::assertThrows( fn(): void => { Age::parse('-4'); }, LogicError::class, { message: 'an age is never negative' }, );} catch (Core\Test\Failure $failure) { echo $failure->message, "\n";}Core\Test::assertThrows failed: `$body` returned without throwing LogicErrorCore\Test::assertThrows failed: `$body` threw LogicError, not IOError: not a number: tenan age is never negative: Core\Test::assertThrows failed: `$body` returned without throwing LogicErrorTest a payment that is too large
<?nvs// Shows a test that a payment larger than the balance throws your own error class.
class PaymentError extends RuntimeError{}
class NotEnoughMoney extends PaymentError{}
class Account{ public int $balance = 100;
public function pay(int $amount): void { if ($amount > $this->balance) { throw new NotEnoughMoney('the balance is ' . $this->balance); } $this->balance = $this->balance - $amount; }}
Account $account = new Account();
// A payment of 250 is more than the balance, so `pay` throws `NotEnoughMoney`.Core\Test::assertThrows(fn(): void => { $account->pay(250); }, NotEnoughMoney::class);
// `NotEnoughMoney` extends `PaymentError`, so a check for the parent class passes too.Core\Test::assertThrows(fn(): void => { $account->pay(250); }, PaymentError::class);
// The failed payments did not change the balance.Core\Test::assertSame($account->balance, 100);echo 'balance: ', $account->balance, "\n";balance: 100