Testing Deprecations with PHPUnit

When testing deprecations with PHPUnit greater than 9.5.10 you might encounter this error:

Failed asserting that exception of type "PHPUnit\Framework\Error\Deprecated" is thrown.

The code in the test is as follows:

$this->expectDeprecation();

and the code that should be tested is like this:

\trigger_error('foo', \E_USER_DEPRECATED);

However PHPUnit does not convert the deprecation to a testable error.
By default PHPUnit converts only errors, warnings and notices but not deprecations as we can read here:
https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices

So we have to tell PHPUnit to convert also deprecations to exceptions and we do this in the phpunit.xml.dist by setting the attribute

convertDeprecationsToExceptions="true"

in the root element.

Now the test should work and deprecations are handled as exceptions.