Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support XDEBUG_MODE environment variable #834

Closed
derickr opened this issue Nov 26, 2020 · 8 comments
Closed

Support XDEBUG_MODE environment variable #834

derickr opened this issue Nov 26, 2020 · 8 comments
Assignees
Labels

Comments

@derickr
Copy link
Contributor

derickr commented Nov 26, 2020

Q A
PHPUnit version 9.4.3
PHP version 7.4.11
Installation Method Composer

Summary

PHPUnit only understands Xdebug modes set in php.ini when checking for coverage availability.

When running:

$ XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-text

It is not recognised that the mode is set.

Current behavior

PHPUnit shows a warning:

Warning:       xdebug.mode=coverage has to be set in php.ini

How to reproduce

  • Any repository with phpunit available in vendor/bin/phpunit.
  • Run: XDEBUG_MODE=coverage php -n -dzend_extension=xdebug vendor/bin/phpunit --coverage-text

Expected behavior

Code coverage should have been run, as it would have been with:

php -n -dzend_extension=xdebug -dxdebug.mode=coverage ./vendor/bin/phpunit --coverage-text
@sebastianbergmann
Copy link
Owner

sebastianbergmann commented Nov 27, 2020

What is PHPUnit supposed to do when this XDEBUG_MODE environment variable is set?

@sebastianbergmann sebastianbergmann transferred this issue from sebastianbergmann/phpunit Nov 27, 2020
@sebastianbergmann
Copy link
Owner

sebastianbergmann commented Nov 27, 2020

From reading

Besides setting the mode with xdebug.mode, you can also set the mode with the XDEBUG_MODE environment variable. If this environment variable is active, it overrides the mode as set through xdebug.mode.

I understand that Xdebug looks at the XDEBUG_MODE environment variable and changes its mode based on that. As far as I can see, though, Xdebug does not provide an API for querying its mode apart from using ini_get('xdebug.mode') (or using xdebug_info(), capturing its output through output buffering, and then parsing the generated HTML).

For a short term solution, this library can fall back to looking for/at the XDEBUG_MODE environment variable. If it is set, its string contents will be treated like the string contents of ini_get('xdebug.mode') is right now:

if (!ini_get('xdebug.mode') || !in_array('coverage', explode(',', ini_get('xdebug.mode')), true)) {
    throw new Xdebug3NotEnabledException;
}

The message of Xdebug3NotEnabledException, of course, needs to be adapted.

In the long run, though, I would really like for Xdebug to provide a function that returns information about which mode(s) is/are currently active.

@sebastianbergmann sebastianbergmann changed the title PHPUnit does not understand that modes can also be read from XDEBUG_MODE env var XDEBUG_MODE environment variable is not considered Nov 27, 2020
@sebastianbergmann sebastianbergmann self-assigned this Nov 27, 2020
@sebastianbergmann sebastianbergmann changed the title XDEBUG_MODE environment variable is not considered Support XDEBUG_MODE environment variable Nov 27, 2020
sebastianbergmann added a commit that referenced this issue Nov 27, 2020
@sebastianbergmann
Copy link
Owner

Created https://bugs.xdebug.org/view.php?id=1898 for tracking my feature request for Xdebug.

@AyrtonRicardo
Copy link

AyrtonRicardo commented Dec 1, 2020

Is this supposed to work when configuring the xdebug variable via phpunit.xml.dist file?

Because if so, then the file below is not working: it's not generating reporting and showing me the warning mentioned in the issue.

<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="tests/bootstrap.php"
>
    <php>
        <ini name="error_reporting" value="-1" />
        <ini name="xdebug.mode" value="coverage" />
        <env name="XDEBUG_MODE" value="coverage" />
    </php>

    <testsuites>
        <testsuite name="Main Test Suite">
            <directory>tests</directory>
        </testsuite>
    </testsuites>

    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">src</directory>
        </include>
        <report>
            <clover outputFile="./reports/coverage_clover.xml"/>
        </report>
    </coverage>

    <logging>
        <junit outputFile="reports/junit.xml"/>
    </logging>
</phpunit>

Version code coverage: phpunit/php-code-coverage (9.2.5)
Version Phpunit: phpunit/phpunit (9.4.4)
Installation via: composer
PHP Version: PHP 7.3.21

calling in the way described in the issue @php -n -dzend_extension=xdebug -dxdebug.mode=coverage ./vendor/bin/phpunit --coverage-text works, but it's not that nice in my env at the moment to call it in that way.

@sebastianbergmann
Copy link
Owner

No, this cannot work when XDEBUG_MODE is set using PHPUnit's XML configuration file. It has to be set at PHP startup, "long" before the code that parses the XML configuration file is parsed.

@AyrtonRicardo
Copy link

@sebastianbergmann thanks for clarify it.
I will stick to this command then XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-text .

koriym added a commit to koriym/Koriym.PhpSkeleton that referenced this issue Dec 6, 2020
morawskim added a commit to stasiu20/ssorder that referenced this issue Dec 30, 2020
Support for XDEBUG_MODE environment variable was introduced in php-code-coverage at version 9.2.4.
sebastianbergmann/php-code-coverage#834

PHPUnit 9.4 requires php-code-coverage^9.2
trmccormick added a commit to wvulibraries/ccdt that referenced this issue Jan 6, 2021
XDEBUG_MODE needs to be set to coverage on the command to run phpunit. This either can be passed in the command to run or placed in the php.ini file. Solution discussed on issue sebastianbergmann/php-code-coverage#834
CQD added a commit to CQD/qlurk that referenced this issue Jan 10, 2021
CQD added a commit to CQD/qlurk that referenced this issue Jan 10, 2021
CQD added a commit to CQD/qlurk that referenced this issue Jan 10, 2021
hemberger added a commit to smrealms/smr that referenced this issue Jan 23, 2021
After switching to xdebug v3 in 325974c, PHPUnit stopped creating
coverage reports and began emitting the following warning:

> XDEBUG_MODE=coverage or xdebug.mode=coverage has to be set

We set this as an environment variable in the `smr-integration-test`
service to re-enable coverage reports.

See sebastianbergmann/php-code-coverage#834.
@nelson6e65
Copy link

nelson6e65 commented Jan 25, 2021

As a Composer script, you can use "@putenv XDEBUG_MODE=coverage":

"test:coverage": [
  "@putenv XDEBUG_MODE=coverage",
  "phpunit --color=always --coverage-clover=\"output/code-coverage/clover.xml\""
],
"test:coverage-html": [
  "@putenv XDEBUG_MODE=coverage",
  "phpunit --color=always --coverage-html=\"output/code-coverage\""
],

https://getcomposer.org/doc/articles/scripts.md#setting-environment-variables

benblub added a commit to benblub/activity-recorder that referenced this issue Jan 31, 2021
jfritschi added a commit to apereo/phpCAS that referenced this issue Feb 9, 2021
wouldsmina pushed a commit to libertempo/web that referenced this issue Apr 7, 2021
* Put libertempo in lowercase to solve a warning from composer

* Add a description

* Global version update

* Update php version to 7.4

* Update php version to 7.4

* Rollback to php 7.2

* Rollback to php 7.1

* Update to php 7.3 for travis

* Correct the channel property for irc

* Update to php-code-coverage 9.2.6 because of sebastianbergmann/php-code-coverage#834

* Only notify IRC on the main repo, not on clones

* Move to php 7.4 to enable test coverage

* Upgrade atoum to 3.4.1 for compatibility on travis-ci

* Set the exact version

* Check if the fetch_array returns something before checking the value
kienstra added a commit to kienstra/block-scaffolding-wp-1 that referenced this issue May 10, 2021
patrik-csak added a commit to patrik-csak/fa-wp-admin-menu-icons that referenced this issue Oct 28, 2021
@SukhrobNuraliev
Copy link

xdebug.mode=coverage has to be set in php.ini

bjuppa added a commit to kontenta/kontour that referenced this issue Apr 27, 2023
atehvg added a commit to superbrave/auth0-http-client that referenced this issue Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants