- How do I integrate php-code-coverage into Laravel’s PHPUnit tests?
- Add it as a dev dependency via `composer require --dev phpunit/php-code-coverage`, then configure PHPUnit’s `clover` or `coverage` flags in `phpunit.xml`. Laravel’s default PHPUnit setup already supports it—no extra Laravel-specific config is needed.
- Does this package work with PestPHP for Laravel testing?
- Yes, it’s fully compatible with PestPHP. Use the `--coverage` flag in Pest commands (e.g., `pest --coverage`) to generate reports. The package’s fixes for closures and case statements also improve Pest’s coverage accuracy in Laravel projects.
- What Laravel versions and PHP versions are supported?
- The package has no Laravel-specific version requirements but works seamlessly with Laravel 9+, 10+, and 11+. It requires PHP 8.0+ (or PHP 7.4+ for older versions). Always pin to `^9.0` or later for stability.
- How do I filter coverage to exclude Laravel vendor files?
- Use the `Filter` class to exclude vendor paths. For example, `$filter->excludeFiles([__DIR__.'/vendor']);`. Laravel’s default test setup often auto-excludes `vendor/`—check your `phpunit.xml` for `<filter>` configurations.
- Will this slow down my Laravel CI/CD pipeline?
- Yes, coverage collection adds ~20–50% overhead. Mitigate this by running coverage only in CI (e.g., `phpunit --coverage --group coverage`) or using parallel testing with PestPHP (`pest --parallel`). Avoid running it in production.
- How do I generate OpenClover reports for SonarQube in Laravel?
- Use the `ReportFacade` to render OpenClover XML. Example: `ReportFacade::fromObject($coverage)->renderOpenClover('/tmp/clover.xml');`. Laravel CI tools like GitHub Actions can then upload this file to SonarQube using standard plugins.
- Does this package fix coverage issues with Laravel’s route closures or middleware?
- Yes, version 14.1.8 improves coverage for closures (e.g., `Route::get(fn() => ...)`) and `case` statements. Re-run coverage after updating if your tests use these patterns—it may reveal previously missed branches in Laravel’s routing logic.
- Can I use PCOV instead of Xdebug for Laravel coverage?
- Yes, the package supports both. Configure PCOV in `php.ini` (`extension=pcov.so`) and select it via `DriverSelector::forLineCoverage()`. PCOV is lighter than Xdebug but may miss some edge cases—test thoroughly in your Laravel environment.
- How do I serialize coverage data for later analysis in Laravel?
- Use the `Serializer` class to save coverage data: `$serializer->serialize($coverage, '/tmp/coverage.php');`. Later, deserialize with `Unserializer` and regenerate reports. This is useful for distributed CI setups or offline analysis.
- What alternatives exist for Laravel coverage if I don’t want to use php-code-coverage?
- PestPHP has built-in coverage support (`--coverage`), and tools like `infection` (mutation testing) or `paratest` (parallel testing) offer alternatives. However, php-code-coverage remains the most flexible for custom reporting (e.g., OpenClover, HTML) and edge-case fixes.