Skip to content

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

Closed
@titantwentyone

Description

@titantwentyone
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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions