English spoken conference

Symfony 5: The Fast Track

A new book to learn about developing modern Symfony 5 applications.

Support this project

New in Symfony 4.4: PHPUnit Polyfills

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.

Comments

TL;DR If you don't get what this is about, no worries: it's critically required to run the tests of #Symfony on #PHP 7.4 *and* 5.5 (for branch 3.4). Many don't have these requirements. Yet, if you do, enjoy, the tooling is OSS too!
Yummy!
I just spent some time refactoring a couple of test suites in order to make them compatible all the way from phpunit 5.7 to 7.5, and was not enthralled at the idea of having to hammer out of them compatibility with phpunit 8 too. Will the new PhpUnutBridge version be usable in conjunction with Symfony 2.4/php 5.6 ?
Login with SymfonyConnect to post a comment