This php script will read the clover xml report from phpunit and calculates the coverage score. Based on the given threshold the script will exit ok of the coverage is higher then the threshold or exit with code 1 if the coverage is lower then the threshold. This script can be used in your continuous deployment environment or for example added to a pre-commit hook.
The script can be installed using composer. Add this repository as a dependency to the composer.json file.
composer require --dev robiningelbrecht/phpunit-coverage-check dev-master
Generate the coverage file by using phpunit and run the coverage check script:
vendor/bin/phpunit --coverage-clover clover.xml
vendor/bin/coverage-checker clover.xml 80
Command coverage-checker, version 1.0
Check the code coverage using the clover or text report of phpunit
Usage: coverage-checker [OPTIONS...] [ARGUMENTS...]
Arguments:
<file> The location of the file that's generated by phpunit
<percentage> The coverage threshold that is acceptable. Min = 1, Max = 100
Options:
[-f|--formatter] Formatter to use ("message" by default)
[-p|--processor] Processor to use ("clover-coverage" by default)
[-m|--enabled-metrics] Metrics to use when calculating coverage. Defaults to all
[-elc|--exit-on-low-coverage] Exit the script when low coverage is detected. Defaults to "false"
[-h|--help] Show help
[-v|--verbosity] Verbosity level
[-V|--version] Show version
Legend: <required> [optional] variadic...