PHPUnit 4.8: Code Coverage Support

PHPUnit 4.8 introduces a couple of small improvements. For instance, the @testWith annotation was added as "syntactic sugar" for defining data providers and the --no-coverage commandline option was added for ignoring any code coverage configuration from the configuration file.

PHP_CodeCoverage, the library used by PHPUnit to collect, process, and report code coverage information, has been updated for PHPUnit 4.8. It can now collect code coverage information on PHP 7 without the need for a third-party extension such as Xdebug. And PHP_CodeCoverage's HHVM driver has been updated to reflect changes to how HHVM provides code coverage information.

PHP 7

The release of PHP 7 draws ever closer. Unfortunately, though, Xdebug, the established standard for collecting code coverage information from the PHP interpreter, has not been updated for PHP 7 yet. PHPDBG, the debugger that is developed as part of PHP and shipped with PHP releases, has recently gained support for collecting code coverage information. PHP_CodeCoverage 2.2, developed as part of the PHPUnit 4.8 release cycle, is the first version to support PHPDBG as a source for code coverage data.

To use PHP 7's built-in code coverage functionality with PHPUnit, simply invoke PHPUnit using the phpdbg (PHPDBG SAPI) binary instead of the php (CLI SAPI) binary as shown below:

$ /usr/local/php-7.0/bin/phpdbg -qrr ../phpunit/phpunit -c build
PHPUnit 4.8.0 by Sebastian Bergmann and contributors.

Runtime:       PHPDBG 7.0.0-dev
Configuration: /usr/local/src/money/build/phpunit.xml

............................................

Time: 1.31 seconds, Memory: 14.00Mb

OK (44 tests, 70 assertions)

Generating code coverage report in Clover XML format ... done

Generating Crap4J report XML file ... done

Generating code coverage report in HTML format ... done

Generating code coverage report in PHPUnit XML format ... done

The -qrr commandline option for the phpdbg that is used above instructs PHPDBG to execute a script and quit after the execution finished without debugging the PHP code that is executed.

HHVM

HHVM used to provide its own API, fb_*_code_coverage(), for collecting code coverage information. This API has been removed, though, and now HHVM exposes its code coverage functionality using Xdebug's xdebug_*_code_coverage() API. PHP_CodeCoverage 2.2 has been updated to reflect these changes.

Please note that the xdebug_*_code_coverage() API is only active (and available to PHPUnit and PHP_CodeCoverage) when xdebug.enable = On is configured in /etc/hhvm/php.ini, for instance.

Looking Ahead: PHPUnit 5

PHPUnit 4.8 is the last release series of PHPUnit to support PHP 5.3, PHP 5.4, and PHP 5.5. These versions of PHP are no longer actively supported by the PHP project. PHPUnit 4.8 will receive bug fixes until August 7, 2016.

PHPUnit 5, scheduled for release in October 2015, will only be supported on PHP 5.6 and PHP 7. As PHPUnit follows Semantic Versioning the major version number must be incremented when the minimum required version of PHP is increased. Functionality that is marked as deprecated in PHPUnit 4 will be removed in PHPUnit 5.

About the author

Sebastian Bergmann
Sebastian Bergmann
Twitter LinkedIn Xing
Share this article
20 Years of PHP Dependencies in Disguise