rregeer/phpunit-coverage-check
Install the Package
Add to your composer.json:
composer require --dev rregeer/phpunit-coverage-check
Generate a Clover Report Run PHPUnit with coverage and clover output:
vendor/bin/phpunit --coverage-clover clover.xml
Run the Coverage Check Execute the script with the clover file and a threshold (e.g., 80%):
vendor/bin/coverage-check clover.xml 80
CI/CD Integration
Add to your CI script (e.g., .github/workflows/ci.yml):
- name: Run PHPUnit
run: vendor/bin/phpunit --coverage-clover clover.xml
- name: Check Coverage
run: vendor/bin/coverage-check clover.xml 80
Exit code 1 will fail the pipeline.
Pre-Commit Hook
Use with tools like husky or pre-commit:
# .huskyrc.js
{
"hooks": {
"pre-commit": "vendor/bin/phpunit --coverage-clover clover.xml && vendor/bin/coverage-check clover.xml 80"
}
}
Laravel-Specific Workflow
vendor/ or tests/ from coverage).- name: Laravel Coverage Check
run: |
php artisan test --coverage-clover=clover.xml
vendor/bin/coverage-check clover.xml 85
COVERAGE_THRESHOLD).
vendor/bin/coverage-check clover.xml $COVERAGE_THRESHOLD
--parallel) and aggregate clover reports before checking.--filter=Feature) and apply suite-specific thresholds.Clover File Path
clover.xml) may break across environments. Use relative paths or config files..env:
COVERAGE_CLOVER_PATH=storage/logs/clover.xml
Then reference it dynamically in scripts.Threshold Precision
80 checks for ≥ 80.00%.80.0 for explicit decimal handling (though the package may ignore decimals).Exit Codes
1 = failure (coverage below threshold).0 = success (coverage meets threshold).PHPUnit Configuration
<coverage> is properly configured in phpunit.xml:
<coverage>
<include>src/*</include>
<exclude>src/Exceptions/*</exclude>
</coverage>
Dry Runs
Use --only-percentage to verify coverage before enforcing thresholds:
vendor/bin/coverage-check clover.xml 80 --only-percentage
Outputs only the percentage (e.g., 78.50%), helping debug without failing.
Log Clover Details Inspect the clover.xml file directly for discrepancies:
xmllint --format clover.xml # Pretty-print XML
Look for unexpected files or line counts.
CI Debugging Cache the clover file as an artifact in CI to inspect after failures:
- name: Upload Clover Artifact
uses: actions/upload-artifact@v3
if: failure()
with:
name: clover-report
path: clover.xml
Custom Validation Logic
Extend the script by wrapping it in a PHP class (e.g., app/Console/Commands/CoverageCheck.php):
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class CoverageCheck extends Command {
protected function handle() {
$process = new Process(['vendor/bin/coverage-check', 'clover.xml', '80']);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
}
}
Add pre/post-check logic (e.g., Slack notifications).
Composite Reports Merge multiple clover files (e.g., from parallel tests) before checking:
vendor/bin/phpunit --coverage-clover=clover1.xml --testdox-html
vendor/bin/phpunit --coverage-clover=clover2.xml --testdox-html
php merge-clover.php clover1.xml clover2.xml merged.xml
vendor/bin/coverage-check merged.xml 80
Use tools like php-coveralls for merging.
Laravel Service Provider
Register a global coverage check listener in AppServiceProvider:
public function boot() {
if (app()->environment('testing')) {
$this->registerCoverageCheck();
}
}
protected function registerCoverageCheck() {
Artisan::addCommand(new \App\Console\Commands\CoverageCheck);
}
Trigger checks via php artisan coverage:check.
How can I help you explore Laravel packages today?