Description
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
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
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.
Activity
sebastianbergmann commentedon Mar 18, 2025
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 commentedon Mar 18, 2025
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:
In the output shown above,
/var/www/bugged-phpunit-covers-method-with-trait/src/DummyWithTrait.php:10
should beApp\DummyWithTrait::method1
instead.Add tests for #1066
sebastianbergmann commentedon Mar 18, 2025
This is odd: my first attempt to reproduce this through unit tests failed (see dabbb4f).
yurii-stickee-2023 commentedon Mar 18, 2025
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
realpath(..)
that somehow resolves into correct matchPlease ignore me if this makes no sense.
Regards