sebastianbergmann / php-code-coverage

Library that provides collection, processing, and rendering functionality for PHP code coverage information.
BSD 3-Clause "New" or "Revised" License
8.76k stars 370 forks source link

Branch Coverage and Path Coverage are not correctly reported for traits #1023

Closed titantwentyone closed 7 months ago

titantwentyone commented 7 months ago
Q A
php-code-coverage version 10.1.9
PHP version 8.2.13
Driver Xdebug
Xdebug version (if used) 3.3.0
Installation Method Composer
Usage Method PHPUnit
PHPUnit version (if used) 10.5.2

When testing the methods of a trait, the code coverage stats for branches and paths are labelled as n/a when looking at the summary however the code summary below is correctly highlighted showing the code that cover the paths and branches.

Example repo at https://github.com/titantwentyone/code-coverage-for-traits whch includes the code below:

#[\PHPUnit\Framework\Attributes\CoversClass(\Titantwentyone\CodeCoverageForTraits\Concerns\ExampleTrait::class)]
class ExampleTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @test
     */
    public function it_calls_the_trait()
    {
        $amount = (new \Titantwentyone\CodeCoverageForTraits\ExampleClass)->getAmount();
        $this->assertEquals(100, $amount);
    }
}
namespace Titantwentyone\CodeCoverageForTraits;

use Titantwentyone\CodeCoverageForTraits\Concerns\ExampleTrait;

class ExampleClass
{
    use ExampleTrait;

    public function getAmount(): int
    {
        return $this->getValue();
    }
}
namespace Titantwentyone\CodeCoverageForTraits\Concerns;

trait ExampleTrait
{
    public function getValue(): int
    {
        return 100;
    }
}

If useful, phpunit.xml is defined as:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         cacheDirectory=".phpunit.cache"
         executionOrder="depends,defects"
         requireCoverageMetadata="false"
         beStrictAboutCoverageMetadata="false"
         beStrictAboutOutputDuringTests="true"
         failOnRisky="true"
         failOnWarning="true">
    <testsuites>
        <testsuite name="default">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
    <coverage includeUncoveredFiles="true" pathCoverage="true">
    </coverage>
    <source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
        <include>
            <directory>src</directory>
        </include>
    </source>
</phpunit>

And for completeness, the command I am running is:

XDEBUG_MODE=coverage php ./vendor/bin/phpunit --coverage-html ./coverage -c ./phpunit.xml --path-coverage

What I am seeing: image For both branches and paths: image

dvdoug commented 7 months ago

This is specific to traits, because for the past few versions Xdebug has been outputting branch/path data for traits under a key that doesn't allow us easily to map it back to where it came from.

That's now fixed (https://bugs.xdebug.org/view.php?id=2027), so I think all we have to do here is strip off the extra annotation