covergenius/phpunit-testlistener-vcr
PHPUnit test listener that records and replays HTTP interactions using a VCR-style approach. Capture real API responses into cassettes during tests, then replay them for fast, deterministic runs without hitting external services.
Http, Guzzle, Illuminate\Http) makes this package a natural fit for testing routes, external APIs, or third-party integrations.actingAs(), json()) by adding cassette-based replayability.vcr/vcr (~3.0), which is stable but may need configuration for Laravel’s service container.Tests\TestCase booting) to auto-record/replay cassettes.VCR instance in Laravel’s container.$this->refreshDatabase()) or queued job testing separately.| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Cassette corruption | Medium | Use Git LFS for large cassettes; validate schema. |
| Test flakiness | High | Enforce cassette updates in CI (e.g., fail if responses differ). |
| Performance bloat | Low | Exclude non-critical tests; use vcr/skip annotations. |
| Laravel-specific quirks | Medium | Test with Http::fake() interactions first. |
| Dependency conflicts | Low | Pin vcr/vcr and phpunit/phpunit versions. |
phpunit.xml with custom listeners.Http facade, Guzzle, or Symfony’s Client.vcr/vcr (~3.0) and a cassette storage (default: ./tests/vcr_cassettes).phpunit.xml:
<listeners>
<listener class="CoverGenius\PHPUnit\VCRListener" file="./vendor/covergenius/phpunit-testlistener-vcr/src/VCRListener.php" />
</listeners>
VCR instance in a service provider:
$this->app->singleton(\VCR\VCR::class, function () {
return \VCR\VCR::init()->configure()->setCassettePath(__DIR__.'/../tests/vcr_cassettes');
});
Tests\TestCase to auto-record/replay:
protected function setUp(): void {
parent::setUp();
$this->vcr = app(\VCR\VCR::class);
$this->vcr->insertCassette($this->getCassetteName());
}
@vcr/skip annotations for tests not using cassettes.Http::fake() with VCR for real HTTP interactions.api.middleware.key)..gitattributes for LFS).vcr/vcr and phpunit/phpunit for breaking changes.UserController_createUser.yml).Http::fake() → refactor tests to use one or the other..yml/.json files) works for small teams.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Stale cassette | Tests pass locally but fail in CI | Enforce CI validation; auto-update cassettes. |
| Cassette corruption | Tests fail unpredictably | Use checksums; back up cassettes. |
| API contract changes | All tests using the endpoint fail | Monitor API changes; update cassettes proactively. |
| Storage full | CI pipeline crashes | Set size limits; archive old cassettes. |
| Parallel test conflicts | Race conditions in recordings | Disable sharing or use unique paths. |
How can I help you explore Laravel packages today?