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:


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:

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


in the root element.

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