php - PHPUnit 9 Support for using expectException() with PHPUnitFrameworkErrorError is deprecated
Solution:
https://thephp.cc/news/2020/02/migrating-to-phpunit-9
From what I found expectException()
is supposed to catch all different types of errors i.e. deprecated, notice, warning and error. So the maintainers decided to have separate methods for each type i.e. expectDeprecation()
, expectNotice()
, expectWarning()
and expectError()
It seems to me the maintainers do not intend expectException()
to specify the type of error in the first place, knowing whether it is deprecated or notice or warning or error is all they want to know and I partly agree. Isn't able to catch error enough for most cases unless your code can throw different types of errors and you want to make sure that it throws the right type of error.
For your code expectError()
should be enough.
public function testFooErrorsOnBlankBar()
{
$this->expectError();
My\Class::foo('', '123');
}
Answer
Solution:
shouldn't the code instead be:
class FooClass extends \PHPUnit\Framework\TestCase
{
public function testFooErrorsOnBlankBar()
{
$this->expectException(\InvalidArgumentException::class);
My\Class::foo('', '123');
}
}
Source