esi/phpunit-coverage-check
Reads PHPUnit clover/openclover XML coverage reports and computes overall coverage percentage. Fails CI with exit code 1 when coverage drops below a configurable threshold; exits successfully when the threshold is met. Useful for enforcing minimum test coverage.
PHPUnit Coverage Check - Check the code coverage using the clover report of PHPUnit.
This library will read the clover (or openclover in PHPUnit 12.2+) XML report from PHPUnit and calculate the coverage score. Based on the given threshold, the application will exit OK if the coverage is higher than the threshold or exit with code 1 if the coverage is lower than the threshold.
This library can be used in multiple ways:
composer.json and running it with a composer script.See GitHub Action, Installation and Usage for more information.
[!IMPORTANT] This project is not in any way an official
PHPUnitproject. Meaning it is not associated with, or endorsed by, thePHPUnitproject or its authorSebastian Bergmann.
A GitHub action is available and can be found in the PHPUnit Coverage Check Action repository, or on the GitHub Marketplace.
PHPUnit Coverage Check can be installed using Composer. Add this repository as a dev-dependency to the composer.json file.
$ composer require --dev esi/phpunit-coverage-check:^3.1
To use PHPUnit Coverage Check on an older version of PHP:
$ composer require --dev esi/phpunit-coverage-check:^1.0
$ composer require --dev esi/phpunit-coverage-check:^2.0
Download the phpunit-coverage-check.phar from an available release. It is recommended to check the signature when downloading the Phar from a GitHub Release (with phpunit-coverage-check.phar.asc).
# Adjust the URL based on the latest release
wget -O phpunit-coverage-check.phar "https://github.com/ericsizemore/phpunit-coverage-check/releases/download/3.1.0/phpunit-coverage-check.phar"
wget -O phpunit-coverage-check.phar.asc "https://github.com/ericsizemore/phpunit-coverage-check/releases/download/3.1.0/phpunit-coverage-check.phar.asc"
# Check that the signature matches
gpg --verify phpunit-coverage-check.phar.asc phpunit-coverage-check.phar
# Check the issuer (the ID can also be found from the previous command)
gpg --keyserver hkps://keys.openpgp.org --recv-keys F8367C6E9D7A7028292144AAC9D8B66FF3C06696
rm phpunit-coverage-check.phar.asc
chmod +x phpunit-coverage-check.phar
The Phar files of PHPUnit Coverage Check are signed with a public key associated to admin@secondversion.com..
The key(s) associated with this E-Mail address can be queried at keys.openpgp.org.
You can also install the PHPUnit Coverage Check Phar with Phive.
If not already using Phive, you can read more about it here, also Phive installation and usage.
There are two parameters that must be passed to return the code coverage.
Generate the clover.xml file by using PHPUnit:
$ php vendor/bin/phpunit --coverage-clover clover.xml
You can also add the coverage report generation to your PHPUnit configuration file (phpunit.xml.dist for example). You would need to add the following lines inside the <coverage> tag:
<report>
<clover outputFile="clover.xml"/>
</report>
For the experimental OpenClover report in PHPUnit 12.2+
$ php vendor/bin/phpunit --coverage-openclover clover.xml
<report>
<openclover outputFile="clover.xml"/>
</report>
$ php vendor/bin/coverage-check /path/to/clover.xml 100
$ php vendor/bin/coverage-check /path/to/clover.xml 100 --only-percentage
# -O for only-percentage works as well
$ php vendor/bin/coverage-check /path/to/clover.xml 100 -O
# -F or show-files will display coverage per file, formatted in a table
$ php vendor/bin/coverage-check /path/to/clover.xml 100 -F
You can use the Api directly if you wish. The Esi\CoverageCheck\CoverageCheck::nonConsoleCall() method will process and return the data, like how the console application displays it.
/**
* Processes the coverage data with the given clover file and threshold, and returns the information
* like how the Console application will.
*
* This is mainly useful for those that may wish to use this library outside the CLI/Console or PHAR.
*/
public function nonConsoleCall(string $cloverFile, int $threshold = 100, bool $onlyPercentage = false): string
An example usage:
use Esi\CoverageCheck\CoverageCheck;
$check = new CoverageCheck();
$results = $check->nonConsoleCall(__DIR__ . '/tests/fixtures/clover.xml', 90);
echo $results; // Total code coverage is 90.32%
$ php phpunit-coverage-check.phar /path/to/clover.xml 100
$ php phpunit-coverage-check.phar /path/to/clover.xml 100 --only-percentage
# -O for only-percentage works as well
$ php phpunit-coverage-check.phar /path/to/clover.xml 100 -O
# -F or show-files will display coverage per file, formatted in a table
$ php phpunit-coverage-check.phar /path/to/clover.xml 100 -F
With --only-percentage (or -O) enabled, the CLI command will only return the resulting coverage percentage.
With --show-files (or -F), --only-percentage will be ignored. This option parses the clover file for coverage information for each file in the project/package, determine coverage, and display the information in a table.
For example:
$ php coverage-check build/logs/clover.xml 90 -F
------------------------------------------------------------------- -------------------------- ----------
File Elements (Covered/Total) Coverage
------------------------------------------------------------------- -------------------------- ----------
[...]\phpunit-coverage-check\src\Application.php 12/12 100.00%
[...]\phpunit-coverage-check\src\Command\CoverageCheckCommand.php 94/94 100.00%
[...]\phpunit-coverage-check\src\CoverageCheck.php 80/80 100.00%
[...]\phpunit-coverage-check\src\Style\CoverageCheckStyle.php 12/12 100.00%
[...]\phpunit-coverage-check\src\Utils.php 39/39 100.00%
------------------------------------------------------------------- -------------------------- ----------
Overall Totals 237/237 100.00%
------------------------------------------------------------------- -------------------------- ----------
$ php coverage-check tests/fixtures/clover.xml 90 -F
----------------------------- -------------------------- ----------
File Elements (Covered/Total) Coverage
----------------------------- -------------------------- ----------
/tmp/Example/String.php 36/38 94.74%
/tmp/Example/StringList.php 20/24 83.33%
----------------------------- -------------------------- ----------
Overall Totals 56/62 89.04%
----------------------------- -------------------------- ----------
The --table-width (or -W) option will only have an affect if used with the --show-files option. The only requirement for this particular option is that you must pass an integer
value. For example:
$ php coverage-check tests/fixtures/clover.xml 90 -F -W 120
# ...or
$ php coverage-check tests/fixtures/clover.xml 90 --show-files --table-width 120
This library is a fork of/based upon rregeer/phpunit-coverage-check by Richard Regeer.
Most of this library has been rewritten from the ground up, to replace and improve the majority of the original library. The overall idea, and key pieces of the calculation, are thanks to the original library. Many thanks and much appreciation to Richard Regeer for his wonderful work.
phpunit-coverage-check library this library is forked from.coverage:check command.See CONTRIBUTING.
Bugs and feature requests are tracked on GitHub.
See backward-compatibility.md for more information on Backwards Compatibility.
See the CHANGELOG for more information on what has changed recently.
See the LICENSE for more information on the license that applies to this project.
See SECURITY for more information on the security disclosure process.
How can I help you explore Laravel packages today?