Download the PHP package faisalmirza/diff-coverage without Composer
On this page you can find all versions of the php package faisalmirza/diff-coverage. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download faisalmirza/diff-coverage
More information about faisalmirza/diff-coverage
Files in faisalmirza/diff-coverage
Package diff-coverage
Short Description Calculate code coverage for git diff (changed lines only)
License MIT
Homepage https://github.com/faisalmirza/diff-coverage
Informations about the package diff-coverage
diff-coverage
Ensure new code is tested. Check coverage for only the lines you changed.
diff-coverage runs your tests with coverage and validates that changed lines meet your coverage threshold. Stop debating overall coverage percentages—focus on what matters: new code should be tested.
Why diff-coverage?
| Traditional Coverage | Diff Coverage |
|---|---|
| "We need 80% overall coverage" | "New code must be tested" |
| Legacy code drags down metrics | Only measures what you changed |
| Hard to improve incrementally | Every PR can pass |
| Debates about thresholds | Clear, actionable feedback |
Requirements
- PHP 8.1+
- Git
- Xdebug or PCOV (for coverage)
Installation
Note: If not yet on Packagist, install from GitHub:
Quick Start
For Laravel, Pest, or PHPUnit projects, just run:
The tool auto-detects your test framework and runs tests with coverage automatically.
Configuration
Configuration is loaded in layers (each layer overrides the previous):
Option 1: Zero Configuration (Recommended)
diff-coverage auto-detects your project type and enables parallel testing if paratest is installed:
| Framework | Detection | Source Path | Coverage Path |
|---|---|---|---|
| Laravel | artisan file |
app, src |
tests/coverage/clover.xml |
| Symfony | bin/console + composer.json |
src |
var/coverage/clover.xml |
| CakePHP | bin/cake file |
src |
tmp/coverage/clover.xml |
| CodeIgniter | spark file |
app |
build/coverage/clover.xml |
| Yii | yii file |
src |
tests/coverage/clover.xml |
| Laminas | config/application.config.php |
module, src |
data/coverage/clover.xml |
| Pest | vendor/bin/pest |
src, app |
coverage/clover.xml |
| PHPUnit | phpunit.xml or vendor/bin/phpunit |
src, app |
coverage/clover.xml |
Parallel Testing: Automatically enabled when brianium/paratest is installed.
Option 2: Configuration File
Create .diff-coverage.json in your project root:
All fields are optional. Only specify what you want to override.
| Field | Type | Default | Description |
|---|---|---|---|
branch |
string | origin/main |
Branch to compare against |
threshold |
integer | 100 |
Minimum coverage percentage for changed lines |
coverage_file |
string | (auto-detected) | Path to Clover XML coverage file |
test_cmd |
string | (auto-detected) | Command to run tests with coverage |
source_paths |
array | ["app", "src"] |
Directories to check for freshness |
Option 3: CLI Arguments
Override any setting via command line:
| Option | Description | Example |
|---|---|---|
-b, --branch |
Branch to compare | -b origin/develop |
-t, --threshold |
Coverage threshold | -t 80 |
-c, --coverage |
Coverage file path | -c build/coverage.xml |
-T, --test-cmd |
Test command | -T "phpunit --coverage-clover=cov.xml" |
-f, --force |
Force re-run tests | -f |
-s, --skip-tests |
Use existing coverage | -s |
-h, --help |
Show help | -h |
Usage Examples
Performance
Freshness Detection
diff-coverage automatically skips running tests if your coverage file is newer than your source files. This speeds up repeated runs during development.
To always run tests: ./vendor/bin/diff-coverage -f
To never run tests: ./vendor/bin/diff-coverage -s
CI Integration
GitHub Actions
GitLab CI
Bitbucket Pipelines
Exit Codes
| Code | Meaning |
|---|---|
0 |
Coverage meets threshold (or no changes detected) |
1 |
Coverage below threshold, tests failed, or configuration error |
How It Works
- Detect your test framework (Laravel/Pest/PHPUnit)
- Run tests with coverage (unless skipped or fresh)
- Generate git diff against the target branch
- Filter coverage report to only changed lines
- Calculate coverage percentage for those lines
- Exit with success/failure based on threshold
Under the hood, diff-coverage uses exussum12/coverage-checker for the diff filtering logic.
Troubleshooting
"No changes detected"
Your branch has no diff against the target branch. This is normal for the main branch.
"Coverage file not found"
Tests may have failed, or the coverage file path is incorrect. Check:
- Tests pass when run directly
- The coverage file path in your config matches where your tests write it
Coverage seems wrong
Ensure you're comparing against the correct branch:
Tests run every time
The freshness check compares timestamps. If your CI always clones fresh, tests will always run. This is expected behavior in CI.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License. See LICENSE for details.