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

xdebug mode detection ignores CLI option (-dxdebug.mode=coverage) #1002

Open pionl opened 1 year ago

pionl commented 1 year ago
Q A
PHPUnit version 9.6.9
PHP version 8.1.20
Installation Method Composer

Summary

Lately we have changed our docker setup that sets XDEBUG_MODE=debug (based on .env file) and it broke our execution of tests with code coverage in PHPStorm.

Currently we are able to bypass it by using different PHPStorm run configuration that forces XDEBUG_MODE=coverage.

Current behavior

PHPUnit outputs a warning:

Warning:       XDEBUG_MODE=coverage or xdebug.mode=coverage has to be set

PHPStorm runs the tests with php -dxdebug.mode=coverage /app/vendor/phpunit/phpunit/phpunit which ensures that xdebug should use coverage value but PHPUnit uses XDEBUG_MODE environment variable with higher priority.

I've checked this by checking xdebug_info.

debug.php

<?php

xdebug_info();
XDEBUG_MODE=debug php debug.php | grep xdebug.mode
...
xdebug.mode (through XDEBUG_MODE) => debug
XDEBUG_MODE=debug php -dxdebug.mode=coverage debug.php | grep xdebug.mode
...
xdebug.mode (through XDEBUG_MODE) => debug => coverage

I was able to test this in PHPUnit 9 (don't have a project on PHP10), but the code seems same?

Is this expected behavior?

How to reproduce

XDEBUG_MODE=debug php -dxdebug.mode=coverage ./vendor/phpunit/phpunit/phpunit --coverage-text

Expected behavior

Code coverage should be collected.

Thank you for your time!

sebastianbergmann commented 1 year ago

The code for PHPUnit 9.6 that determines whether Xdebug 3 is loaded and configured for code coverage is here.

The code for PHPUnit 10 is different in that it uses xdebug_info('mode') when available.

Can you please investigate whether backporting the xdebug_info('mode') query from the php-code-coverage version used by PHPUnit 10 to the one used by PHPUnit 9 would solve your problem? Thanks!

pionl commented 1 year ago

Hi @sebastianbergmann thank you for the answer, will look into it this week!