dvdoug/behat-code-coverage
Behat extension that generates code coverage reports for PHP applications. Wraps PHPUnit’s php-code-coverage library to produce familiar, interoperable reports and integrates coverage generation into your Behat test runs.
Pros:
phpunit/php-code-coverage (v9–v14), ensuring compatibility with existing Laravel/PHPUnit coverage workflows. Reports (HTML, Cobertura, Clover, Serialized) align with industry standards.Cons:
phpunit --coverage). Requires dual coverage pipelines unless merged.behat.yml setup (e.g., reports, filter, cache), adding complexity to CI/CD pipelines.Laravel Compatibility:
phpunit/php-code-coverage under the hood). No Laravel-specific dependencies.codecov.io uploads).Key Dependencies:
| Risk Area | Mitigation Strategy |
|---|---|
| Coverage Gaps | Use filter.exclude in behat.yml to align with Laravel’s testable codebase (e.g., exclude vendor/, tests/Unit/). |
| CI Performance | Cache coverage data (cache: sys_get_temp_dir()) and parallelize Behat scenarios. |
| Configuration Errors | Validate behat.yml schema early (e.g., via behat --dry-run). |
| Xdebug Conflicts | Use PCOV in CI (faster) and Xdebug locally (debugging). |
| Report Format Issues | Test Cobertura/HTML reports in CI before merging (e.g., behat --coverage --format=cobertura). |
php-code-coverage evolves (e.g., v15+ compatibility)?Primary Use Case: BDD-driven Laravel projects where Behat tests validate user journeys, API contracts, or domain logic (e.g., e-commerce workflows).
Complementary Tools:
phpunit --coverage).Anti-Patterns:
| Phase | Action Items |
|---|---|
| Assessment | Audit existing Behat tests to identify coverage gaps (e.g., missing edge cases in feature tests). |
| Pilot | Add the extension to a non-critical feature branch and test coverage reports in CI. |
| Configuration | Update behat.yml with minimal viable config (e.g., HTML + Cobertura reports). Example: |
extensions:
LeanPHP\Behat\CodeCoverage\CodeCoverageExtension:
reports:
html:
directory: tests/_output/coverage
cobertura: tests/_output/coverage.cobertura.xml
filter:
exclude:
- vendor/
- tests/Unit/
cache: tests/_output/coverage-cache
``` |
| **CI Integration** | Add steps to:
1. Run Behat with coverage: `behat --coverage`.
2. Upload Cobertura to Codecov: `bash <(curl -s https://codecov.io/bash)`.
3. (Optional) Fail builds below 80% coverage for critical paths. |
| **Toolchain Sync** | Ensure PHPUnit coverage and Behat coverage are **not double-counting** (e.g., exclude Laravel’s `tests/Feature/` from Behat if PHPUnit already covers them). |
| **Local Setup** | Document how to **enable/disable coverage** locally (e.g., `behat --coverage` vs. `behat`). |
### **Compatibility**
- **Laravel-Specific**:
- Works with **Laravel’s default Behat setup** (no Laravel-specific modifications needed).
- **Artisan Commands**: Can wrap Behat coverage in a custom Artisan command (e.g., `php artisan behat:coverage`) for consistency.
- **Service Providers**: No impact; extension runs as a Behat extension.
- **Dependency Conflicts**:
- **Xdebug/PCOV**: Ensure `php.ini` has `xdebug.coverage_enable=1` (or use PCOV for CI).
- **Behat Version**: Tested with Behat 3.7+; Laravel’s default Behat bundle (if used) should be updated.
- **PHPUnit**: No direct conflict, but ensure `phpunit/php-code-coverage` versions align (e.g., PHPUnit 10+ uses v10+ of the coverage library).
### **Sequencing**
1. **Phase 1: Basic Coverage**
- Enable HTML reports for **manual review** of Behat test coverage.
- Focus on **critical user flows** (e.g., checkout, authentication).
2. **Phase 2: CI Integration**
- Add Cobertura reports to **Codecov/SonarQube**.
- Set **coverage thresholds** for high-risk features.
3. **Phase 3: Advanced Metrics**
- Enable **branch coverage** (Xdebug) for complex logic (e.g., discount calculations).
- Integrate with **mutual coverage tools** (e.g., Infection) for mutation testing.
4. **Phase 4: Optimization**
- Cache coverage data to **reduce CI runtime**.
- Parallelize Behat scenarios to **offset Xdebug overhead**.
---
## Operational Impact
### **Maintenance**
- **Pros**:
- **Low Maintenance**: Extension is **batteries-included** with `php-code-coverage`; updates require minimal effort (e.g., bumping `phpunit/php-code-coverage` version).
- **Community Backed**: 62 stars, active changelog, and alignment with PHPUnit’s ecosystem.
- **Configuration Stability**: Schema validation in `behat.yml` reduces misconfigurations.
- **Cons**:
- **Dependency Churn**: Must track `php
How can I help you explore Laravel packages today?