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.81k stars 373 forks source link

XDebug environment variable takes precedence over ini option #988

Open HorvathPeterHub opened 1 year ago

HorvathPeterHub commented 1 year ago

Issue: XDebug code coverage not working with "php -dxdebug.mode=coverage" option

Description:

I'm trying to run PHPUnit tests with code coverage in PhpStorm. I use the following command:

php -dxdebug.mode=coverage /app/vendor/phpunit/phpunit/phpunit --coverage-clover /opt/phpstorm-coverage/Packages@ArrayTest.xml --configuration /app/lib/MyProject/phpunit.xml --filter Horvathpeter\\MyProject\\Test\\MyTest --test-suffix MyTest.php /app/vendor/horvathpeter/myproject/test --teamcity

However, even though the -dxdebug.mode=coverage option is passed, I still get a warning that XDEBUG_MODE=coverage or xdebug.mode=coverage has to be set. And the code coverage dialog does not appear.

Upon checking the PHPUnit code, I found that it checks for the XDEBUG_MODE environment variable first, before checking the xdebug.mode ini option. And even though the ini option is passed, it still checks for the environment variable, which is set in my case to something else.

Here's the relevant code from PHPUnit:

$mode = getenv('XDEBUG_MODE');

if ($mode === false || $mode === '') {
    $mode = ini_get('xdebug.mode');
}

if ($mode === false ||
    !in_array('coverage', explode(',', $mode), true)) {
    throw new Xdebug3NotEnabledException;
}

I checked the environment variables using var_dump(getenv('XDEBUG_MODE')); and found that it is set to debug,develop.

It seems that the getenv() function takes precedence over the -dxdebug.mode option. Can this behavior be changed so that the passed ini option takes precedence over the environment variable?

Expected result:

The -dxdebug.mode=coverage option should enable code coverage without the need for setting the XDEBUG_MODE environment variable.

Actual result:

The -dxdebug.mode=coverage option is not recognized as enabling code coverage, and the XDEBUG_MODE environment variable needs to be set.

Environment:

PHP version: 8.2.1
XDebug version: 3.2.0
PHPUnit version: 9.6.5
sebastianbergmann commented 1 year ago

Here's the relevant code from PHPUnit:

That code is not in PHPUnit but in php-code-coverage.

HorvathPeterHub commented 1 year ago

Yes, you're right. But is the ticket good here? Or should I submit it somewhere else? Update: now I see, you've transferred it.

sebastianbergmann commented 1 year ago

I already moved it.