- How do I integrate php-code-coverage with Laravel’s default PHPUnit tests?
- Add it as a dev dependency via `composer require --dev phpunit/php-code-coverage`, then configure it in `phpunit.xml` using the `<coverage>` section. Replace PHPUnit’s built-in `--coverage` flag with this package for advanced reporting like OpenClover or HTML. Laravel’s default PHPUnit setup already supports it natively.
- Which Laravel versions and PHP versions does php-code-coverage support?
- The package works with Laravel 8.0+ (PHP 8.2+) and Laravel 10+ (PHP 8.2+). For legacy Laravel 7.x (PHP 7.4+), use version `^12.5`. Always align your PHP version with Laravel’s requirements to avoid compatibility issues. Check the [compatibility matrix](https://github.com/sebastianbergmann/php-code-coverage#compatibility-matrix) for specifics.
- Can I use this package with Pest instead of PHPUnit in Laravel?
- Yes, Pest 2.0+ supports this package via its `--coverage` flag. Install it as a dev dependency, then run `pest --coverage` or configure it in `pest.php` to generate reports. The package works identically to PHPUnit for coverage collection and reporting.
- How do I configure php-code-coverage to exclude vendor/ or tests/ directories?
- Use the `Filter` class to exclude paths. In your test bootstrap or PHPUnit config, define a filter like `$filter->excludeFiles([__DIR__.'/../vendor', __DIR__.'/../tests'])` before starting coverage. This ensures only your app code (e.g., `app/`) is measured, reducing noise in reports.
- What’s the best way to generate OpenClover reports for Codecov in Laravel CI?
- Configure PHPUnit to output OpenClover XML: add `<coverage><driver>xdebug</driver><format>xml</format></coverage>` to `phpunit.xml`. In GitHub Actions, upload the generated `build/logs/clover.xml` to Codecov using their upload tool. For Pest, use `--coverage=clover` and specify the output path.
- How do I handle false positives in coverage reports (e.g., excluded files still showing up)?
- False positives often stem from misconfigured filters. Double-check your `includeFiles()` and `excludeFiles()` paths in the `Filter` class. For edge cases, manually validate reports by comparing them to your codebase. Use `ReportFacade::fromObject()` to debug the raw coverage data before rendering.
- Is there a performance impact when using php-code-coverage in CI, and how can I mitigate it?
- XML report generation (e.g., OpenClover) can slow down CI for large codebases. Cache reports between test runs by serializing coverage data with `Serializer` and reusing it. For speed, prefer lightweight formats like `--coverage-clover` over HTML. Run reports in parallel if your CI supports it.
- How do I migrate from PHPUnit’s built-in coverage to php-code-coverage in Laravel?
- Replace `--coverage` CLI flags with this package’s config in `phpunit.xml`. Start by adding `<coverage><driver>xdebug</driver><include>app/*</include></coverage>`. Test incrementally by running `php artisan test --coverage` and comparing reports. Use `ReportFacade` for custom formats if needed.
- What are the alternatives to php-code-coverage for Laravel code coverage?
- Laravel’s default PHPUnit includes basic coverage tools, but for advanced needs, consider `phpunit/php-code-coverage` (most feature-rich), `infection/infection` (mutation testing), or `roave/security-advisories` (for security-focused coverage). This package is the de facto standard for PHPUnit/Pest integration due to its modularity and format support.
- How do I enforce minimum coverage thresholds in Laravel CI using this package?
- Use PHPUnit’s `--min-coverage` flag (e.g., `--min-coverage=80`) or integrate with tools like Codecov/GitHub Actions to block PRs. For custom logic, parse the coverage data with `ReportFacade` and fail builds programmatically. Example: `if ($coverage->getCoveragePercentage() < 90) exit(1);` in a post-test script.