Migrating to PHPUnit 6

The first version of PHPUnit 6 was released today. Today also marks the End of Life of PHPUnit 4. What does this mean for you? The answer to this question depends on which versions of PHP and PHPUnit you currently use.

You currently use PHPUnit 4

Until today, PHPUnit 4.8 was the old stable release of PHPUnit. It was supported on PHP 5.3, PHP 5.4, PHP 5.5, and PHP 5.6. Active support for PHP 5 ended on December 31, 2016 and PHP 5.3, PHP 5.4, PHP 5.5 have already reached their End of Life.

You use a version of PHPUnit that is no longer supported, no bugs will be fixed, and no new releases will be made. If you still use PHP 5.3, PHP 5.4, or PHP 5.5 then PHPUnit 4.8 is newest version of PHPUnit you can use. If you use PHP 7.0 or PHP 7.1 then you should upgrade to PHPUnit 6 (see below).

If you use Composer to manage PHPUnit as a dependency of your project and want to ensure that you stay on PHPUnit 4.8 then you should depend on "phpunit/phpunit": "^4.8" in your composer.json file.

If you use PHPUnit from a PHP archive (PHAR) and want to ensure that you stay on PHPUnit 4.8 then you have to download PHPUnit from https://phar.phpunit.de/phpunit-4.8.phar.

You currently use PHPUnit 5

Until today, PHPUnit 5.7 was the stable release of PHPUnit. Now it has become the old stable release of PHPUnit. It is supported on PHP 5.6, PHP 7.0, and PHP 7.1. PHPUnit 5.7 will receive bugfixes until it reaches its End of Life on February 2, 2018.

You use a version of PHPUnit that is still supported but will not get new features anymore. If you still use PHP 5.6 then PHPUnit 5.7 is newest version of PHPUnit you can use. If you use PHP 7.0 or PHP 7.1 then you should upgrade to PHPUnit 6 (see below).

If you use Composer to manage PHPUnit as a dependency of your project and want to ensure that you stay on PHPUnit 5.7 then you should depend on "phpunit/phpunit": "^5.7" in your composer.json file.

If you use PHPUnit from a PHP archive (PHAR) and want to ensure that you stay on PHPUnit 5.7 then you have to download PHPUnit from https://phar.phpunit.de/phpunit-5.7.phar.

You want to use PHPUnit 6

PHPUnit 6 requires PHP 7.0 or PHP 7.1. It introduces significant changes that require a migration of your existing test suite to PHPUnit 6.

If you use Composer to manage PHPUnit as a dependency of your project and want to use PHPUnit 6 then you should depend on "phpunit/phpunit": "^6.0" in your composer.json file.

If you use PHPUnit from a PHP archive (PHAR) and want to use PHPUnit 6 then you can download PHPUnit from https://phar.phpunit.de/phpunit.phar.

Below you find a list of things to do and keep in mind while migrating your existing test suite to PHPUnit 6:

PHPUnit_Framework_TestCase is now PHPUnit\Framework\TestCase

PHPUnit's units of code are now namespaced. Among other things, this means that your test case classes now need to extend PHPUnit\Framework\TestCase instead of PHPUnit_Framework_TestCase.

While PHPUnit 5.7 has a forward compatibility layer, meaning you can already use PHPUnit\Framework\TestCase with that version, there is no backward compatibility layer in PHPUnit 6 that allows you to use PHPUnit_Framework_TestCase.

Useless Tests

For a long time already, PHPUnit can perform perform optional checks for so-called risky tests while executing a test suite. Starting with PHPUnit 6, the risky test check for useless tests is active by default.

A useless test is one that does not perform an assertion (such as assertEquals(), for instance), does not expect an exception (using expectException(), for instance), and does not configure an expectation on a mock object.

Simply use the --dont-report-useless-tests commandline option or set beStrictAboutTestsThatDoNotTestAnything="false" in your XML configuration file to disable the risky test check for useless tests.

Global State

Starting with PHPUnit 6, the global and super-global variables are no longer backed up before and restored after each test by default. In general, this functionality is not required for clean, modern, object-oriented PHP code. This functionality slows down the execution of the test suite and increases memory usage.

Simply use the --globals-backup commandline option or set backupGlobals="true" in your XML configuration file to enable the backup of global and super-global variables before each test and their restoring after each test.

Deprecated APIs

Methods such as getMock(), getMockWithoutInvokingTheOriginalConstructor(), setExpectedException(), setExpectedExceptionRegExp(), or hasPerformedExpectationsOnOutput() that were deprecated before have now been removed from PHPUnit 6.

createMock(), createConfiguredMock(), createPartialMock(), createTestProxy(), and getMockBuilder() should be used instead of getMock() and getMockWithoutInvokingTheOriginalConstructor(). And expectException() should be used instead of setExpectedException() (or the @expectException annotation).

JUnit Logger

The logfile format generated using the --log-junit commandline option and the <log type="junit" target="..."/> configuration directive has been updated to match the current format used by JUnit.

Due to this change you may need to update how your continuous integration server processes test result logfiles generated by PHPUnit.

Über den Autor

Sebastian Bergmann
Sebastian Bergmann
Twitter LinkedIn Xing
Artikel teilen
Refactoring to PHP 7 The Death Star Version Constraint