Install PCOV in a PHP Docker image

Published at 30 Sep 2024

If you're maintaining a PHP project, you probably want to obtain code coverage reports of your tests.

There are multiple options to generate a code coverage report, today I'm going to explain to you how to install PCOV a lightweight package for generating accurate reports.

This tutorial assumes you're using a PHP Docker image, if not, some of the commands below may differ for your image.

Installing & enabling PCOV

To install PCOV in your system, you should execute the following command.

pecl install pcov

Next, you should enable the PHP extension.

docker-php-ext-enable pcov

Keep in mind you cannot have PCOV and XDebug enabled at the same time.

If you want to learn more about PCOV, you should check the GitHub repository of the project. (https://github.com/krakjoe/pcov)

Enabling coverage reports at PHPUnit

In order to be able to retrieve a code coverage report, you should add the following lines inside the phpunit.xml file before the </phpunit> closing tag.

<coverage processUncoveredFiles="true">
    <include>
        <directory suffix=".php">./app</directory>
    </include>
    <report>
        <html outputDirectory="reports/coverage"/>
    </report>
</coverage>

After adding those lines, you can execute PHPUnit using the following command.

phpunit

Or the following command can be used if your project is using Laravel 9 or above.

php artisan test --coverage