- How do I integrate php-code-coverage with Laravel’s built-in PHPUnit testing?
- Laravel’s `php artisan test` already uses PHPUnit, which depends on php-code-coverage. Configure coverage in your `phpunit.xml` by adding `<coverage>` tags with `<include>` (e.g., `./app`) and `<exclude>` (e.g., `./vendor`) directives. Run tests with `--coverage-clover` or `--coverage-html` flags for reports.
- Can I use this package with Pest instead of PHPUnit for Laravel testing?
- Yes, Pest supports coverage via the `--coverage` flag, which internally uses php-code-coverage. Configure Pest’s `phpunit.xml` or run `pest --coverage` to generate reports. Ensure your Pest version aligns with PHPUnit’s coverage drivers for compatibility.
- What’s the best way to enforce minimum coverage thresholds in CI for Laravel projects?
- Use PHPUnit’s `--min-coverage` flag (e.g., `phpunit --coverage-clover=coverage.clover --min-coverage=80`) in your CI script. For Laravel, add this to your `phpunit.xml` under `<phpunit>` or trigger it via GitHub Actions/GitLab CI. Fail builds if coverage drops below your threshold.
- How do I exclude Laravel’s vendor or config files from coverage reports?
- Exclude directories in `phpunit.xml` using `<exclude><directory>./vendor</directory></exclude>` or `<exclude><directory>./config</directory></exclude>`. Alternatively, use the `Filter` class programmatically to dynamically exclude paths during coverage collection.
- Is php-code-coverage compatible with Laravel 10 and PHP 8.4+?
- Yes, php-code-coverage supports PHP 8.4+. Laravel 10 (PHP 8.2+) may require minor adjustments if using older versions (pre-v13.0.0). Test with `composer require phpunit/php-code-coverage:^14.0` for the latest features, including SHA-256 hashing for faster cache keys.
- How can I generate OpenClover reports for SonarQube in Laravel?
- Run PHPUnit with `--coverage-clover=coverage.clover`, then use the `ReportFacade` to convert the Clover file to OpenClover: `ReportFacade::fromFile('coverage.clover')->renderOpenClover('sonarqube.xml')`. Upload the XML to SonarQube via CI/CD or use GitHub Actions with `sonarsource/sonarqube-scan-action`.
- What are the performance implications of php-code-coverage for large Laravel codebases?
- XML report generation can add latency for large projects (>10k LOC). Use `--coverage-clover` instead of `--coverage-html` for faster CI runs, or cache coverage data with `CachingSourceAnalyser`. Parallel test execution (e.g., Pest’s `--parallel`) may introduce race conditions; validate with v13.0.2+ fixes.
- How do I migrate from deprecated PHP serialization to the new Serializer/Unserializer?
- Replace `unserialize(file_get_contents('coverage.php'))` with `Unserializer::unserialize('coverage.php')`. Update custom scripts to use the new `Serializer` class for writing coverage data. This change was introduced in v14.0.0 to improve security and maintainability.
- Can I customize coverage reports (e.g., themes, excluded metrics) for Laravel?
- Yes, use the `ReportFacade` to customize HTML reports (e.g., dark mode via v14.0.0 palettes). Exclude metrics like methods or branches with `Filter` or PHPUnit’s `<coverage><report><exclude><method/></exclude></report></coverage>`. For OpenClover, validate schema compliance if using experimental features.
- What alternatives exist for code coverage in Laravel if php-code-coverage doesn’t fit?
- Consider `infection/infection` for mutation testing or `paratestphp/paratest` for parallel test execution with coverage. For simpler setups, `jacoco/jacoco` (Java) or `istanbul.js` (JavaScript) may integrate via CI, but php-code-coverage remains the PHP standard due to its PHPUnit integration and extensibility.