PHPCoverage - an open-source code coverage measurement tool for PHP Applications


PHPCoverage is an open-source tool for measuring and reporting code coverage provided by the test suite of a PHP application. PHPCoverage can instrument and record the line coverage information for any PHP script at runtime.

PHPCoverage also provides an extensible reporting mechanism with a standard HTML report implemented out of the box. The default report displays the summary information about the code coverage for an application and also shows the detailed information about a file including which lines were actually executed and with what frequency. It is possible to specify the directories and files that should be included and/or excluded from a coverage measurement. Screenshots of the summary and detailed line coverage reports are available here.

PHPCoverage works on PHP 5.0 and newer and uses Xdebug Extension for gathering the coverage data.


Frequently Asked Questions

  1. How do I install PHPCoverage?

    Just expand the tarball

    tar -zxvf spikephpcoverage-0.6.tar.gz

    This will create a directory called spikephpcoverage-0.6 and expand all files in it. You should export an enviroment variable PHPCOVERAGE_HOME or define a constant in your test script that refers to the location of src directory and use it when including the PHPCoverage files:

    $ export PHPCOVERAGE_HOME=/path/to/phpcoverage/src


    define("PHPCOVERAGE_HOME", "/path/to/phpcoverage/src"); 
    require_once PHPCOVERAGE_HOME . "/CoverageRecorder.php";
  2. How do I use PHPCoverage for recording code coverage locally?

    This topic explains how PHPCoverage can be used to record local code coverage information. Any PHP script that runs in the same context as that of the invoker is said to run locally. In practice, if you are running tests that do not exercise PHP code through a web server, you can use the normal CoverageRecorder class. On the other hand, if your test suite contains web tests that access PHP code running in a web server (on same or different) machine, you will need to follow a different procedure for code coverage measurement.

  3. How do I use PHPCoverage for recording remote code coverage?

    If your tests are trying to exercise PHP code that is running within a web server on same or different machine, you should use RemoteCoverageRecorder to measure code coverage. You should also instrument the application code before running your tests. The actual process of instrumenting a file is straightforward. PHPCoverage files need to be included at the top and bottom of each of the application files. PHPCoverage comes with a command line utility to do this for you.

    Deploy your web application into your web server. This typically involves copying the application files into a web directory managed by the webserver or creating a symlink from such directory to your application.

    Execute the command line utility to instrument your application.

    $ cd /path/to/application/base/dir
    $ export PHPCOVERAGE_HOME=/path/to/phpcoverage/location
    $ php /cli/instrument.php -b . file1.php file2.php ... 

    You need to specify your application base directory with -b option, followed by a list of files or directories or both. The application base directory is where your index.php is stored. PHPCoverage files will be copied to this directory and need to be accessed over HTTP to initialize code coverage measurements. instrument.php also supports following options: