digitalrevolution/phpunit-file-coverage-inspection
Define per-file code coverage thresholds from Clover coverage.xml, with directory/file overrides and optional uncovered-method rules. Fails builds with checkstyle or GitLab formatted reports so a single file can’t hide behind high overall coverage.
Install the package in your Laravel project:
composer require --dev digitalrevolution/phpunit-file-coverage-inspection
Generate a phpfci.xml config file in your project root using:
php vendor/bin/phpfci baseline coverage.xml --config ./phpfci.xml --baseDir ./src
(Replace coverage.xml with your actual PHPUnit coverage report path.)
Run inspection against your coverage report:
php vendor/bin/phpfci inspect coverage.xml
(Outputs violations to stdout by default.)
phpfci.xml to set file/directory-specific thresholds (e.g., src/App/Commands at 95%, src/App/Jobs at 100%).php vendor/bin/phpfci inspect coverage.xml --exit-code-on-failure
test:
script:
- php vendor/bin/phpunit --coverage-clover coverage.xml
- php vendor/bin/phpfci inspect coverage.xml --reportGitlab=gl-coverage-errors.json
artifacts:
when: always
reports:
codequality: gl-coverage-errors.json
- name: Run coverage inspection
run: |
php vendor/bin/phpfci inspect coverage.xml --reportCheckstyle=checkstyle.xml
if [ $? -ne 0 ]; exit 1;
baseline to auto-generate rules from existing coverage:
php vendor/bin/phpfci baseline coverage.xml --config phpfci.xml --baseDir ./src
(Adjust thresholds manually afterward in phpfci.xml.)php vendor/bin/phpfci inspect unit-coverage.xml feature-coverage.xml
composer.json):
"scripts": {
"test:coverage": "phpunit --coverage-clover=coverage.xml",
"test:inspect": "php vendor/bin/phpfci inspect coverage.xml --exit-code-on-failure"
}
Run with:
composer test:coverage && composer test:inspect
php vendor/bin/phpfci inspect coverage.xml --reportCheckstyle=reports/checkstyle.xml
php vendor/bin/phpfci inspect coverage.xml --reportGitlab=gl-errors.json
Path Resolution Issues:
--baseDir matches the project root (not src/). Paths in coverage.xml must resolve correctly.phpfci.xml.Overlapping Rules:
src/App/ rule at 90% won’t apply to src/App/Exceptions/ if it has a 100% rule.Method Coverage Strictness:
allow-uncovered-methods="false" (default) fails if any method has 0% coverage, even if file coverage is met.<ignore-uncovered-methods> for specific files.Baseline Mismatches:
baseline with inconsistent coverage files (e.g., unit vs. feature tests) may generate misleading rules.PHPUnit Version Compatibility:
^2.2.0).--reportText to preview violations before failing CI:
php vendor/bin/phpfci inspect coverage.xml --reportText > violations.txt
phpfci.xml is valid against the schema.phpunit --coverage-text to verify file paths match your project structure.Custom Validators:
CoverageValidator interface to add logic (e.g., exclude vendor files).Post-Inspection Hooks:
--reportGitlab output to trigger Slack notifications or annotate PRs:
$errors = json_decode(file_get_contents('gl-errors.json'), true);
// Custom logic (e.g., Slack API call)
Symfony Console Integration:
PhpFciCommand class in Laravel Artisan commands for programmatic access:
use DigitalRevolution\PhpFci\Command\PhpFciCommand;
$command = new PhpFciCommand();
$command->run(new ArrayInput(['inspect', 'coverage.xml']), new NullOutput());
unit/, feature/).--baseDir to limit path resolution overhead.How can I help you explore Laravel packages today?