1

I had PHPUnit configured to generate code coverage statistics using XDebug. Seeing that PCOV was faster, I disabled xdebug.so and installed pcov.so.

Now, whenever I run my unit tests, the generated coverage report has no numbers or names or anything: enter image description here

Here's the script that calls phpunit:

#!/bin/bash
script_dir=$(dirname $0);

php $script_dir/phpunit.phar --coverage-html ../../public/coverage

Here is my phpunit.xml, which was unchanged in the process of moving from XDebug to PCOV:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
         bootstrap="bootstrap.php"
         cacheResultFile=".phpunit.cache/test-results"
         executionOrder="depends,defects"
         forceCoversAnnotation="true"
         beStrictAboutCoversAnnotation="true"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutTodoAnnotatedTests="true"
         failOnRisky="true"
         failOnWarning="true"
         verbose="true">
    <testsuites>
        <testsuite name="default">
            <directory>tests</directory>
        </testsuite>
    </testsuites>

    <coverage cacheDirectory=".phpunit.cache/code-coverage"
              processUncoveredFiles="true">
        <include>
            <directory suffix=".php">../../app/</directory>
        </include>
        <exclude>
            <directory suffix=".php">../../app/config/</directory>
            <directory suffix=".php">../../app/libs/</directory>
            <directory suffix=".php">../../app/views/</directory>
        </exclude>
    </coverage>
</phpunit>

Here is the output:

PHPUnit 9.5.6 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.3.11 with PCOV 1.0.9
Configuration: /path/to/phpunit.xml

..............                                                    14 / 14 (100%)

Time: 00:00.751, Memory: 20.00 MB

OK (14 tests, 38 assertions)

Generating code coverage report in HTML format ... done [00:00.074]

PHP version: 7.3.11
PHPUnit version: 9.5.6
PCOV version: 1.09

Is there some additional configuration that needs to be done for PCOV or for PHPUnit for the coverage report to be useful?

Quasipickle
  • 4,383
  • 1
  • 31
  • 53
  • 1
    You are not showing the actual output from PHPUnit. – Sebastian Bergmann Aug 07 '21 at 06:41
  • @SebastianBergmann Added. – Quasipickle Aug 07 '21 at 16:46
  • Is the pcov module enabled? It's often only done during the actual coverage run `php -d pcov.enabled=1 $script_dir/phpunit.phar --coverage-html ../../public/coverage` – Alister Bulman Aug 07 '21 at 17:25
  • @AlisterBulman Yes, it's enabled. `php -m` shows `pcov` in the list. Your command results in the same result. Thanks nonetheless. – Quasipickle Aug 07 '21 at 19:49
  • To check how much the php configuration causes your issue, you can consider to call php with its own configuration specifically for the phpunit test-runner. This can be done for trouble-shooting on the command-line as outlined in an answer to a related question https://stackoverflow.com/a/67912759/367456 . Once having a working configuration on the CLI it is normally straight-forward to spot and handle the issue. – hakre Aug 09 '21 at 07:36
  • @hakre Thanks for the suggestion. The tests run with `php -n`, though no code coverage is generated (as expected). Running it with `php -c .` (targeting a php.ini file that only defined the `extension_dir` and only loaded `pcov.so`) resulted in the same output as previously reported. – Quasipickle Aug 10 '21 at 05:28
  • Well, perhaps my answer there is a bit unclear. What it should aim for is to fully formulate the command line (not using any .ini file) so that by the command-line verbatim it becomes visible what a working configuration is. Phpunit runner should show the intended coverage driver (and PHP only have the bare minimum extensions needed, e.g. explicitly not loading xdebug.so in such a scenario). Seeing that command-line (and the output) then may give more insights. Best for sure is to get the coverage report working, in the answer I suggest using the coverage text-output so it is directly visible. – hakre Aug 10 '21 at 07:55
  • Wondering a bit how the instructions could be made better, too, so that trouble-shooting is easier. – hakre Aug 10 '21 at 07:55
  • Running this command: `php -n -d extension_dir=/path/to/extension/dir -d extension=pcov.so phpunit.phar --coverage-text` gave the same results - no classes, no methods, and no lines were discovered or covered. – Quasipickle Aug 10 '21 at 23:25
  • This might be a configuration issue, but this is only a guess. Sometimes it helps me to put the test-code under coverage as well. It might shed some light in your scenario, too. Perhaps try that? – hakre Aug 13 '21 at 22:25
  • @Quasipickle Did you find a solution to this problem? – JimmyBanks Dec 07 '21 at 19:01
  • @JimmyBanks No, I had to forgo code coverage. – Quasipickle Dec 07 '21 at 22:01

1 Answers1

2

For those who might be still looking for a solution:

You gotta set pcov.directory config to your code base path (e.g. /var/www/html/app), either as a command:

php -d pcov.directory=/var/www/html/app vendor/bin/phpunit --coverage-html=coverage

or in a PHP .ini file (e.g. php.ini or pcov.ini)

Alex
  • 73
  • 6