Jérémy Derussé Nicolas Grekas Andreas Braun
Contributed by Jérémy Derussé , Nicolas Grekas and Andreas Braun in #32922 , #32940 and #33078

The PHPUnit Bridge component improves the original PHPUnit library with utilities to report usage of deprecated code and helpers for mocking native functions related to time, DNS and class existence.

When testing some code that has to be compatible with several versions of PHP, it's difficult to pick a PHPUnit version: latest versions like PHPUnit 8 use features incompatible with PHP 5.5 and remove methods available in earlier PHPUnit versions. In addition, PHPUnit 6 switched to namespaced classes, so tests must work with and without namespaces.

That's why in Symfony 4.4, we've improved the PHPUnit Bridge with several polyfills that allow to create tests that are compatible with multiple PHPUnit versions.

Polyfills for the Unavailable Methods

When using the simple-phpunit script provided by PHPUnit Bridge instead of the phpunit script provided by PHPUnit, Symfony injects polyfills for most of the newest methods of TestCase and Assert classes.

In other words, you can start using new methods in test suites that aren't compatible with newest versions of PHPUnit, such as expectException(), assertStringContainsString(), etc.

Removing the Void Return Type

When running the simple-phpunit script with the SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT environment variable set to 1, the PHPUnit bridge will alter the code of PHPUnit to remove the void return type (introduced in PHPUnit 8) from setUp(), tearDown(), setUpBeforeClass() and tearDownAfterClass() methods. This allows you to write a test compatible with both PHP 5 and PHPUnit 8.

Alternatively, you can use the trait SetUpTearDownTrait in your test class to inject the methods without the void return type.

Namespaced PHPUnit Classes

The PHPUnit bridge adds namespaced class aliases for most of the PHPUnit classes declared without namespaces (e.g. PHPUnit_Framework_Assert), allowing you to always use the namespaced class declaration even when the test is executed with PHPUnit 4, where namespaces aren't used.

Published in #Living on the edge