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

CoversMethod looks bugged when trait is used #1066

Open
yurii-stickee-2023 opened this issue Mar 17, 2025 · 4 comments
Open

CoversMethod looks bugged when trait is used #1066

yurii-stickee-2023 opened this issue Mar 17, 2025 · 4 comments

Comments

@yurii-stickee-2023
Copy link

yurii-stickee-2023 commented Mar 17, 2025

Q A
PHPUnit version 12.0.7
PHP version 8.3.19
Installation Method Composer

Summary

It looks like CoversMethod is bugged when trait is used by class.
Test coverage that is reported is not complete and is very strange.
I have created repository that recreates the issue https://github.com/yurii-stickee-2023/bugged-phpunit-covers-method-with-trait

p.s.
When tests are run while trait is used, PHPUnit outputs

1) Tests\DummyWithTraitTest::test1
This test executed code that is not listed as code to be covered or used:
- /var/www/bugged-phpunit-covers-method-with-trait/src/DummyWithTrait.php:10

/var/www/bugged-phpunit-covers-method-with-trait/tests/DummyWithTraitTest.php:12

OK, but there were issues!
Tests: 3, Assertions: 3, Risky: 1.

Maybe this can be useful

Current behavior

It has very weird behavior. It makes Dummy class with test coverage while other 2 classes are left without test coverage for some unknown reason
Image

How to reproduce

Here https://github.com/yurii-stickee-2023/bugged-phpunit-covers-method-with-trait/blob/73a82437acf414ea08422982dd68afd564c69032/src/DummyWithTrait.php#L6 in dummy class DummyWithTrait uncomment trait so it becomes

<?php
namespace App;

class DummyWithTrait extends BaseDummy
{
    use SomeTrait; // <--- THIS

    public function method1()
    {
        return __FUNCTION__;
    }

    public function method2()
    {
        return __FUNCTION__;
    }
}

Run tests (I did it with xDebug)

XDEBUG_MODE=coverage php -dpcov.enabled=0 vendor/bin/phpunit --coverage-html=.phpunit.cache/coverage-xdebug

Expected behavior

It should report 50% test coverage for all 3 classes - Dummy, Dummy2 and DummyWithTrait

Image

If you downgrade to PHPUnit 11 like composer require --dev phpunit/phpunit:^11.0 -W you will see that it works as expected in both cases now.

@sebastianbergmann
Copy link
Owner

I can confirm that PHPUnit 11.5 (with php-code-coverage 11.0.9) and PHPUnit 12.0 (with phpunit/php-code-coverage 12.1.0) behave differently and will investigate this further.

@sebastianbergmann sebastianbergmann transferred this issue from sebastianbergmann/phpunit Mar 18, 2025
@sebastianbergmann
Copy link
Owner

This might be related or a separate bug, but how PHPUnit 12 reports the "code that is not listed as code to be covered or used" is wrong:

This test executed code that is not listed as code to be covered or used:
- /var/www/bugged-phpunit-covers-method-with-trait/src/DummyWithTrait.php:10

In the output shown above, /var/www/bugged-phpunit-covers-method-with-trait/src/DummyWithTrait.php:10 should be App\DummyWithTrait::method1 instead.

@sebastianbergmann
Copy link
Owner

This is odd: my first attempt to reproduce this through unit tests failed (see dabbb4f).

@yurii-stickee-2023
Copy link
Author

Good day, @sebastianbergmann

Thank you for looking into this. Unfortunately I do not know internals, but if you think I can help with some manual testing, please let me know.

Just a wild guess about why it may pass during unit testing based on your code dabbb4f
I see that it has predefined list of files to load, so it can be

  1. File order that is used in unit tests
  2. Usage of realpath(..) that somehow resolves into correct match

Please ignore me if this makes no sense.

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants