How to use the PHP_CodeCoverage library directly?

433 Views Asked by At

I'm trying to get the PHP_CodeCoverage library working for the simplest possible case to create an HTML code coverage report, and failing. I have PHP and Xdebug installed. I would rather not specify the versions of those that I am using because I'm hoping there is something simple I am overlooking, but I will provide those details upon request. For now, suffice to say, I am using very new versions of them.

To make this as simple as possible, I distilled it down to 2 files and I still can't get it to do what I want. The 2 files are the automatically generated vendor/autoload.php created by composer when installing the coverage library, and the file that is using the library. This file is at project-root/src/CoverageTest.php. The autoload file is at project-root/vendor/autoload.php.

When I run php src/CoverageTest.php from the terminal, it does generate an HTML report. When I view the report, inside the Code Coverage table, there is only a single row with everything set to "n/a" or "0/0". It does not report any coverage as existing or missing. It doesn't say anything about specific files, classes, functions, or lines. To a large extent the code you see in this test file is the same as what appears on their README page. Here is the code:

<?php
require __DIR__.'/../vendor/autoload.php';

$coverage = new \SebastianBergmann\CodeCoverage\CodeCoverage;
$coverage->start('<name of test>');

class MathGenius
{
    public function add($first, $second)
    {
        return $first + $second;
    }
}

$guru = new MathGenius();
$sum = $guru->add(1, 1);

$coverage->stop();

$writer = new \SebastianBergmann\CodeCoverage\Report\Html\Facade;
$writer->process($coverage, __DIR__.'/../code-coverage-report');
1

There are 1 best solutions below

0
On BEST ANSWER

It only reports on whitelisted files and directories. This is how I got the example I posted to work. Before creating the coverage object, I created a filter object, like so:

$filter = new \SebastianBergmann\CodeCoverage\Filter();

Since that example was only attempting to cover itself, I whitelisted that file with the filter like this:

$filter->addFileToWhitelist(__FILE__);

Then when creating the coverage object, you pass in the filter like so:

$coverage = new \SebastianBergmann\CodeCoverage\CodeCoverage(null, $filter);

Here is the full code for the fixed version of the code from the question:

<?php
require __DIR__.'/../vendor/autoload.php';

$filter = new \SebastianBergmann\CodeCoverage\Filter();
$filter->addFileToWhitelist(__FILE__);
$coverage = new \SebastianBergmann\CodeCoverage\CodeCoverage(null, $filter);
$coverage->start('<name of test>');

class MathGenius
{
    public function add($first, $second)
    {
        return $first + $second;
    }
}

$guru = new MathGenius();
$sum = $guru->add(1, 1);

$coverage->stop();

$writer = new \SebastianBergmann\CodeCoverage\Report\Html\Facade;
$writer->process($coverage, __DIR__.'/../code-coverage-report');