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.