php-vcr/phpunit-testlistener-vcr
PHPUnit TestListener integrating PHP-VCR via @vcr annotations. Automatically turns VCR on/off per test and records/replays HTTP interactions using named cassettes. Install with Composer and register the listener in phpunit.xml.
Use Case Alignment (Unchanged, but clarified with PHP 8.1+ focus)
Laravel Synergy (Updated for PHP 8.1+)
Http client and HttpTests remains seamless.#[\VCR\Test]).Anti-Patterns (Reinforced)
Dependencies (Updated for PHP 8.1+)
php-vcr/php-vcr is pinned to ^3.3.composer require php-vcr/phpunit-testlistener-vcr:^3.3 php-vcr/php-vcr:^3.3
Testing Framework (Clarified)
| Risk Area | Severity | Mitigation Strategy | Update for 3.3.0 |
|---|---|---|---|
| PHP 8.0+ Only | High | Drop support for Laravel 8.x (PHP 7.4/8.0). | Action Required: Audit CI/CD for PHP 8.0. |
| Cassette Management | High | Use vcr_mode: once + CI gating. |
No change. |
| Middleware Conflicts | Medium | Exclude via ignoreMiddleware. |
No change. |
| Parallel Test Failures | Low | Unique cassette paths per test class. | No change. |
| Laravel-Specific Quirks | Low | Test with Http::fake() fallback. |
No change. |
assertJson() improvements)?Laravel Compatibility:
#[VCR('my_test')]
public function test_api_call(): void { ... }
VCR\VCR interface to Laravel’s HttpClient for DI:
$this->app->bind(VCR::class, function () {
return VCR::initStorage('tests/_vcr_cassettes');
});
Tooling Synergy (Updated)
VCR::configure([
'filter' => ['hosts' => ['api.example.com' | 'staging.api.example.com']],
]);
strategy:
matrix:
php: ['8.1', '8.2', '8.3'] # Remove 8.0
Phase 0: PHP 8.1+ Upgrade (New)
composer.json:
"require": {
"php": "^8.1",
"php-vcr/phpunit-testlistener-vcr": "^3.3",
"php-vcr/php-vcr": "^3.3"
}
composer update and test with Laravel’s PHP 8.1 baseline.Phase 1: Pilot Phase (Unchanged)
Configuration (Updated for Attributes)
<listeners>
<listener class="VCR\TestListener\VCRListener"/>
</listeners>
use VCR\Test;
#[Test\VCR('users/create')]
public function test_user_creation(): void { ... }
CI/CD Integration (Updated)
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1' # Remove 8.0
Laravel Versions:
PHPUnit Versions:
HTTP Clients:
Http client).Client (if used).@vcr to #[\VCR\Test].Cassette Updates:
#[VCR('dynamic', mode: 'once')] // Records once, then replays
public function test_dynamic_api(): void { ... }
- name: Update Cassettes
run: phpunit --vcr-mode=once --filter="DynamicTest"
if: github.ref == 'refs/heads/main'
Storage:
phpunit --vcr-cleanup or a custom Artisan command:
// app/Console/Commands/CleanVCR.php
public function handle() {
$cassettes = File::allFiles(storage_path('tests/_vcr_cassettes'));
// Delete cassettes for deleted tests...
}
Configuration Drift:
EnsureFrontendRequestsAreStateful).
How can I help you explore Laravel packages today?