phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

Add events for code coverage

Open robiningelbrecht opened this issue 2 years ago • 4 comments

The new event system allows us to write plugins and react to certain events. Even though we already have a lot of useful events, I think there are still a few missing. To be specific, there are currently no events available for code coverage.

It would be nice to have for example

namespace PHPUnit\Event\CodeCoverage\Started;
namespace PHPUnit\Event\CodeCoverage\Stopped;
namespace PHPUnit\Event\CodeCoverage\ReportProcessed;
...

I can think of a lot of useful plugins that could use the code coverage percentage...

robiningelbrecht avatar Apr 04 '23 06:04 robiningelbrecht

PHPUnit itself does not know anything about code coverage data and the data is currently not held/exposed by php-code-coverage (the library that collects and processes code coverage data) in a way that would support what you propose.

sebastianbergmann avatar Apr 04 '23 06:04 sebastianbergmann

Too bad, would be very nice. Nevertheless, thanks for putting a lot of your time into this!

robiningelbrecht avatar Apr 04 '23 06:04 robiningelbrecht

@sebastianbergmann Checked the code, and correct me if I'm wrong, but PHPUnit does know when code coverage started stopped and when the reports have been processed?

Am I missing something here?

EDIT: Although you are right that the events could not contain any data about the coverage result itself...

namespace PHPUnit\TextUI;

final class Application
{
     public function run(array $argv): int
    {
        ...
        CodeCoverage::instance()->generateReports($printer, $configuration);
    }
}

And

namespace PHPUnit\Framework;

final class TestRunner
{
    public function run(TestCase $test): void
    {
        ...
        if ($collectCodeCoverage) {
            CodeCoverage::instance()->start($test);
        }

        ...
        CodeCoverage::instance()->stop(
           $append,
           $linesToBeCovered,
           $linesToBeUsed
        );
    }
}

robiningelbrecht avatar Apr 06 '23 06:04 robiningelbrecht

PHPUnit knows the "when", but not the result(s). I do not see any point in adding events for "code coverage report generation started", "code coverage report generation finished", etc.

sebastianbergmann avatar Apr 06 '23 06:04 sebastianbergmann