- How do I integrate this package with Laravel’s HTTP client (Http::) for testing?
- The package works seamlessly with Laravel’s Http client. Simply use `Http::get()`, `Http::post()`, etc., in your tests as usual. The VCR listener will automatically capture and replay those requests. No additional configuration is needed unless you want to filter specific hosts or middleware.
- Can I use this with Laravel 9 or 10? What PHP versions are supported?
- Yes, this package supports Laravel 9+ and 10+ due to its PHP 8.1+ requirement. It leverages Laravel’s built-in HTTP client and testing utilities without version-specific hacks, making it compatible with modern Laravel releases.
- Will this work with PestPHP instead of PHPUnit?
- While the package is designed for PHPUnit, you can use it with PestPHP by running Pest tests through PHPUnit’s command-line interface. Pest’s test runner can delegate to PHPUnit, allowing the VCR listener to intercept HTTP calls. However, some Pest-specific features may not integrate as smoothly.
- How do I handle dynamic API responses (e.g., timestamps, user-specific data) in cassettes?
- Use PHP-VCR’s built-in filters to sanitize dynamic data. For example, configure the listener to ignore fields like `created_at` or `user_id` by defining a filter in your VCR config. This ensures cassettes remain deterministic while still capturing the core API behavior.
- What’s the best way to manage cassette storage in a team environment?
- Store cassettes in your repository’s `tests/_vcr_cassettes` directory and commit them like test code. Use `.vcrignore` to exclude non-deterministic endpoints (e.g., webhooks, real-time updates). For large teams, implement a CI/CD gate to review cassette changes before merging.
- Can I selectively apply VCR to specific tests instead of all tests?
- Yes, use the `@vcr` annotation in your test classes or methods to scope VCR recording/replaying. This is useful for mixing deterministic tests with dynamic ones. Alternatively, configure the listener in `phpunit.xml` to exclude certain test suites.
- How do I handle missing cassettes in CI/CD (e.g., when APIs change)?
- Configure the listener to run in `vcr_mode: once` for new tests, forcing a fresh recording. For CI, add a pre-test step to validate cassettes or fail gracefully with a fallback (e.g., `Http::fake()`). Use GitHub Actions or similar to automate cassette updates.
- Are there performance concerns with replaying cassettes in tests?
- Cassette replay adds minimal overhead since it’s just file I/O. The bigger impact comes from recording new cassettes, which requires real HTTP calls. Optimize by using `@vcr` selectively and avoiding recording for fast, deterministic tests.
- Does this package conflict with Laravel Dusk or other testing tools?
- No direct conflicts, but Dusk (for browser testing) won’t benefit from VCR since it tests UI interactions. For API-heavy Dusk tests, you can still use VCR for backend HTTP calls. Ensure middleware like `auth:api` is excluded from recording if it’s not deterministic.
- How do I update cassettes when an API changes in production?
- Use the `vcr_mode: once` setting to force a re-record. For CI/CD, implement a workflow that updates cassettes only when API responses change (e.g., via diff tools). Document the process for your team to avoid manual updates. Consider using a `vcr:update` Artisan command for bulk updates.