1

I'm working on a Laravel 10 (with PHPUnit 10 and Xdebug 3) project in which business logic has been split into modules: at the app/ directory level is the modules/ directory where resides modules code (i.e. providers, migrations, models, etc).

.
├── app          <-
├── bin
├── bootstrap
├── config
├── database
├── docker
├── environments
├── lang
├── modules      <-
├── node_modules
├── public
├── resources
├── routes
├── storage
├── tests
└── vendor

In phpunit.xml I added the new directory source as follow:

<source>
    <include>
        <directory>app</directory>
        <directory>modules</directory>
    </include>
</source>

So now the HTML code coverage output correctly list it:

enter image description here

Problem is: its coverage is always at 0% when running tests for modules business logic.

I never used PHPUnit code coverage attributes since I never needed them, every other project I worked on generated correct code coverage without them.

Do I need to add those attributes for source files out of app/ directory?

Update #1

Adding CoversClass(TestedClass::class)] attribute to a module test class did not solve the problem, modules/ code coverage percentage is still 0.

Update #2: Xdebug not being set to coverage

I'm checking if Xdebug mode is correctly set to coverage during tests, and using a simple dd(getenv()) inside of a test show that XDEBUG_MODE=off.

The application is running on docker and the container mount a custom 99-php.ini with the entry xdebug.mode=coverage, so I assume that the '99' makes it the least important between the .ini files being loaded, thus its config do not overwrite an existing xdebug.mode=off in another file.

I added the entry <env name="XDEBUG_MODE" value="coverage" /> to phpunix.xml php section, but still the test output still show off as xdebug mode.

Finally I tried to manually set env variable in container with XDEBUG_MODE=coverage and it looks working (test output correctly display an entry XDEBUG_MODE=coverage from getenv()), problem is modules directory still get a 0% code coverage.

8
  • have u try like ./app and ./modules? ref docs Commented Oct 22, 2024 at 14:28
  • @francisco I don't get what you mean, testing configuration already include app and modules paths, as I said html output correctly list every subfolder and file in both, moreover for app is reported the percentage for the little amount of code covered Commented Oct 22, 2024 at 14:51
  • 2
    Sorry, try this php -d pcov.enabled=1 -d pcov.directory=. -dpcov.exclude="~vendor~" \ vendor/bin/pest --coverage-html=build/coverage I've seen here ref they have a case with 0% cov Commented Oct 22, 2024 at 15:00
  • and one more thing, do you have extension=pcov enable in PHP? it's better that xdebug Commented Oct 22, 2024 at 15:05
  • 1
    The numbers in these files (99) are the order. So a 99 file will override a 20 file. Commented Oct 23, 2024 at 23:58

1 Answer 1

0

I encountered the same issue, and the root cause is related to PCOV. It cannot handle a situation where you have more than two directories at the root level.

Your phpunit.xml configuration is correct, so there's no need to change it. The issue can be resolved by simply disabling PCOV during the code coverage run. You can do this by using the following command:

sail php -d pcov.enabled=0 ./vendor/bin/phpunit --coverage-html coverage-report

This ensures that PCOV does not interfere with your code coverage and allows your configuration to work as expected.

For more details, you can check Why Does Code Coverage for Custom Modules Fail in Laravel?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.