Skip to content

Coverage not working on closures in PHPDBG #945

@Ocramius

Description

@Ocramius
Q A
php-code-coverage version 9.2.17
PHP version 8.1.10
Driver PHPDBG
PCOV version (if used) 1.0.11
Xdebug version (if used)
Installation Method Composer
Usage Method PHPUnit
PHPUnit version (if used) 9.5.25

As investigated in infection/infection#1736, coverage for closures doesn't seem to be picked up correctly, when using PHPDBG.

Specifically, PCOV works as expected:

Selection_1052

A coverage report done with phpdbg -qrr will instead produce partial coverage:

Selection_1051

Reproducible example

<?php

declare(strict_types=1);

final class Foo
{
    /** @return callable(): int */
    function make(): callable
    {
        return static function (): int {
            return 1;
        };
    }
}
<?php

declare(strict_types=1);

require_once __DIR__ . '/../../src/Foo.php';

/** @covers Foo */
final class FooTest extends \PHPUnit\Framework\TestCase
{
    function testClosure(): void
    {
        self::assertSame(1, (new Foo())->make()());
    }
}

PHPDBG output

❯ phpdbg -qrr ./vendor/bin/phpunit --coverage-html=coverage --filter=Foo
PHPUnit 9.5.25 #StandWithUkraine

.                                                                   1 / 1 (100%)

Time: 00:00.140, Memory: 50.00 MB

OK (1 test, 1 assertion)

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

Selection_1053

PCOV output

❯ ./vendor/bin/phpunit --coverage-html=coverage --filter=Foo
PHPUnit 9.5.25 #StandWithUkraine

.                                                                   1 / 1 (100%)

Time: 00:00.053, Memory: 40.00 MB

OK (1 test, 1 assertion)

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

Selection_1054

PHPDBG + PCOV = 💥

Note: running phpdbg with pcov installed will lead to a segfault:

❯ phpdbg -qrr ./vendor/bin/phpunit --coverage-html=coverage --filter=Foo
PHPUnit 9.5.25 #StandWithUkraine

Segmentation fault (core dumped)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions