php-code-coverage icon indicating copy to clipboard operation
php-code-coverage copied to clipboard

Lines with 0 executions should be marked as "Non covered"

Open gufoe opened this issue 1 year ago • 5 comments

Q A
php-code-coverage version 11.0.8
PHP version 8.3.11
Driver Xdebug
Xdebug version (if used) v3.2.0
Installation Method Composer
Usage Method other

I am new to this library which seems fantastic, but I have found a weird behaviour. Files that have some lines tested are shown as 100% tested, even if they contain some functions that have never been executed.

I have made a very minimal and self contained example here, you can just run it with php -d xdebug.mode=coverage index.php and you'll find the report in report/index.html.

The report will show the file as 100% coverage (3/3 lines). But the file does not have 3 lines: it has 41 lines, so i was expecting 3/41 tested (~7% coverage).

Am I getting something wrong or is there a bug in the library?

image image

Thank you very much for the effort you put into this library, it really helps.

gufoe avatar Dec 17 '24 15:12 gufoe

You need to separate the code that uses this library from the code you want code coverage for.

sebastianbergmann avatar Dec 18 '24 06:12 sebastianbergmann

Ok, I have updated the repo, still the same behaviour. image

gufoe avatar Dec 18 '24 09:12 gufoe

Please do not use Xdebug's API directly, but only use CodeCoverage::start() and CodeCoverage::stop().

sebastianbergmann avatar Dec 18 '24 11:12 sebastianbergmann

Awesome, that was the issue, thank you! I updated the code in the repo, now the report properly shows 50% of the methods/code is tested.

gufoe avatar Dec 19 '24 08:12 gufoe

Hello its me again! I found out the issue: it happens when i try to run coverage twice on the same PHP process. The first time i run the coverage report, it works properly (logs both covered (green) and uncovered (red) lines in the report). The second time i run the report, it will only log green lines, therefore reporting 100% coverage.

You can find the minimal repository to see the issue here: https://github.com/gufoe/php-debug

This happens in 3 cases:

  • calling the generate report twice from the same php-cli command
  • calling the generate report once per request, on two different requests, using php -S 127.0.0.1:8080 (the first time the report will be correct, the second time it will be broken)
  • same as preivous, but on php fpm. the report will only be correct on the first request, so i would have to restart php-fpm every time i have to generate a report.

Here is the screenshots of the report results, after the first run, and after the second run:

Image

Image

gufoe avatar Feb 10 '25 12:02 gufoe

Hello, would you be able to provide feedback about this? It makes the library not usable in many cases. Thank you!

gufoe avatar May 14 '25 12:05 gufoe

This library is not designed to be used in the way I understand you want to use it: collection of code coverage can be started and stopped multiple times (on the same CodeCoverage instance) in the same PHP process, but a report can be generated only once.

sebastianbergmann avatar May 20 '25 11:05 sebastianbergmann