Skip to content

turkeryildirim/phpunit-json-coverage-report

Repository files navigation

PHPUnit JSON Coverage Report

Comprehensive JSON coverage reporter for PHPUnit. Reports all 5 coverage metrics: Line, Branch, Path, Function, and Class.

Why this package exists

PHPUnit provides code coverage data via php-code-coverage, but it does not offer a native JSON output format.

Existing formats such as HTML, Clover XML, or Cobertura XML are designed for human reading or specific CI tools. They are not convenient for programmatic consumption.

This package fills that gap by exposing coverage data as structured JSON.


Background

There has been prior discussion about adding JSON output support directly to php-code-coverage:

sebastianbergmann/php-code-coverage#829

As of now, JSON output is not available natively. This package provides an external solution.

How it works

This library is implemented as a PHPUnit extension.
It accesses the raw coverage data during runtime and serializes it into JSON.

  • No XML parsing
  • No post-processing required
  • Direct access to in-memory coverage data

Use Cases

  • CI pipelines (custom coverage checks)
  • Programmatic analysis (scripts, tooling)
  • Frontend dashboards
  • Integration with other systems

Supported Versions

PHPUnit php-code-coverage Status
10.x 10.x ✅ Tested
11.x 11.x ✅ Tested
12.x 12.x ✅ Tested
13.x 13.x ✅ Tested

See Version Compatibility for detailed test results.

Installation

composer require --dev turkeryildirim/phpunit-json-coverage-report

Configuration

Add the reporter to your phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
    <!-- ... other config ... -->

    <extensions>
        <bootstrap class="Turker\PHPUnitCoverageReporter\JsonReporterExtension">
            <!-- Optional: Custom output path (default: coverage.json) -->
            <parameter name="outputFile" value="coverage.json"/>
            <!-- Optional: Filter metrics (default: all) -->
            <!-- Available: lines, branches, paths, functions, classes -->
            <parameter name="metrics" value="lines,branches,functions,classes"/>
        </bootstrap>
    </extensions>
</phpunit>

Run tests with coverage:

vendor/bin/phpunit

A coverage.json file will be generated in your project root.

Documentation

License

MIT License - see LICENSE for details.

About

Comprehensive JSON coverage reporter for PHPUnit.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages