Testing Warnings in PHPUnit 9+

Testing warnings thrown via trigger_error throws a deprecation warning in PHPUnit 9+:

Expecting E_WARNING and E_USER_WARNING is deprecated and will no longer be possible in PHPUnit 10

Testing E_USER_WARNING and E_WARNINGM will no longer be possible, in favor of thrown exceptions. None of the usual convertWarningsToExceptions configuration tricks work.

Here’s a snippet that will help:

$errored = null;
set_error_handler(function($errno, $errstr, ...$args) use (&$errored) {
    $errored = [$errno, $errstr, $args];
    restore_error_handler();
});
call_error_triggering_function();
$this->assertNotNull($errored, 'did not trigger any warning');                                                                             
[$errno, $errstr, $args] = $errored;
$this->assertEquals(E_USER_WARNING, $errno, 'did not trigger an E_USER_WARNING');

The fact that we’re not even going to be seeing warnings, deprecations in PHPUnit 10 is appalling!

https://phpunit.de/announcements/phpunit-10.html