PHP 7.4 and PHPUnit 9
Using the PHPUnit homepage example (https://phpunit.de/getting-started/phpunit-9.html):
private function ensureIsValidEmail(string $email): void
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException(
sprintf(
'"%s" is not a valid email address',
$email
)
);
}
}
The homepage also shows us how to test the exception is thrown using the expectException() method:
public function testCannotBeCreatedFromInvalidEmailAddress(): void
{
$this->expectException(InvalidArgumentException::class);
Email::fromString('invalid');
}
That's great. But what if I want to test the exception is not thrown given valid input ?
Looking at the docs (https://phpunit.readthedocs.io/en/9.3/writing-tests-for-phpunit.html#testing-exceptions) there seems to be no mention of an inverse method to expectException() ?
How should I approach this ?
EDIT TO ADD:
Just to make it perfectly clear, I'm looking to test an Email::fromString('[email protected]'); scenario, i.e. that the exception is not thrown.
testCanBeCreatedFromValidEmailAddressmethod tests the happy path. It won't pass if an exception was thrown. Given that, you don't have to explicitly check for the exception.@doesNotPerformAssertions