handcraftedinthealps/code-coverage-checker
CLI tool to enforce PHPUnit code coverage thresholds. Generate a coverage.php report, then check line/class/method coverage against a minimum percentage, optionally limited to specific directories—ideal for CI to fail builds when coverage drops.
Pros:
symfony/phpunit-bridge for kernel testing).app/, src/) enables targeted quality gates for high-risk or business-critical code.Cons:
vendor/bin/code-coverage-checker), which may not align with Laravel’s service container or task scheduling (e.g., Artisan commands).laravel/framework), this package adds minimal overhead.--coverage-php flag, which is standard in Laravel’s phpunit.xml configurations.# GitHub Actions example
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: phpunit --coverage-php=coverage.clover
- run: vendor/bin/code-coverage-checker coverage.clover line 90
php-coveralls, sonar-scanner)?--coverage-php flag.tests/Feature/).Phase 1: Pilot in CI
composer.json as a dev dependency:
"require-dev": {
"handcraftedinthealps/code-coverage-checker": "^0.2.9"
}
phpunit.xml to generate coverage:
<phpunit>
<coverage>
<include>
<directory>./app</directory>
</include>
<exclude>
<directory>./vendor</directory>
</exclude>
<report>php</report>
</coverage>
</phpunit>
- name: Check coverage
run: vendor/bin/code-coverage-checker coverage.clover line 90
Phase 2: Local Enforcement
php artisan coverage:check) for local development:
// app/Console/Commands/CheckCoverage.php
use Symfony\Component\Process\Process;
public function handle()
{
$process = new Process(['phpunit', '--coverage-php=coverage.clover']);
$process->run();
$this->call('vendor:publish', ['--provider' => 'HandcraftedInTheAlps\CodeCoverageChecker\CodeCoverageCheckerServiceProvider']);
$this->info('Running coverage check...');
$exitCode = shell_exec('vendor/bin/code-coverage-checker coverage.clover line 90');
if ($exitCode !== 0) exit(1);
}
app/Console/Kernel.php:
protected $commands = [
Commands\CheckCoverage::class,
];
Phase 3: Granular Thresholds
app/ and lower for tests/:
vendor/bin/code-coverage-checker coverage.clover line 95 app/ tests/Unit/
tests/Browser/ from checks.phpunit.xml).vendor/bin/ in PATH.composer.json to avoid surprises:
"handcraftedinthealps/code-coverage-checker": "0.2.9"
.env or config/coverage.php to avoid hardcoded CLI values:
// config/coverage.php
return [
'thresholds' => [
'line' => env('COVERAGE_THRESHOLD_LINE', 90),
'directories' => ['app/', 'src/'],
],
];
composer.json scripts for local testing:
"scripts": {
"test:coverage": "phpunit --coverage-php=coverage.clover && vendor/bin/code-coverage-checker coverage.clover line $(config:coverage.thresholds.line
How can I help you explore Laravel packages today?