Download the PHP package daycry/phpunit-extension-vcr without Composer
On this page you can find all versions of the php package daycry/phpunit-extension-vcr. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package phpunit-extension-vcr
PHP-VCR Extension for PHPUnit
A modern library that provides seamless integration between PHP-VCR and PHPUnit, enabling you to record and replay HTTP interactions in your tests using PHP 8+ attributes.
Table of Contents
- Why Use This Extension?
- Requirements
- Installation
- Configuration
- Usage
- Features
- Advanced Usage
- Testing Strategies
- Performance Tips
- Troubleshooting
- Development
- License
- Acknowledgments
- Changelog
Why Use This Extension?
- 🎯 Zero Configuration: Works out of the box with sensible defaults
- 🏷️ Modern PHP 8+ Attributes: Clean, declarative syntax using
#[UseCassette] - 🔄 Automatic State Management: Handles VCR lifecycle automatically
- 🎭 Mixed Test Support: Seamlessly mix tests with and without HTTP recording
- ⚡ Performance: Tests run faster by avoiding real HTTP requests
- 🛡️ Reliability: Consistent test results independent of external services
Requirements
- PHP: 8.2 or higher
- PHPUnit: 10.0 or higher
- php-vcr/php-vcr: ^1.7
Installation
Install via Composer:
Configuration
Add the extension to your phpunit.xml or phpunit.xml.dist file:
Configuration Parameters
All parameters are optional and will use sensible defaults:
| Parameter | Default | Description |
|---|---|---|
cassettesPath |
tests/fixtures |
Directory to store cassette files (relative to project root) |
storage |
yaml |
Storage format (yaml for human-readable, json for smaller files) |
mode |
new_episodes |
Recording mode (see PHP-VCR docs) |
libraryHooks |
stream_wrapper |
Hooks to enable (see PHP-VCR docs) |
requestMatchers |
method, url |
Request matching strategy (see PHP-VCR docs) |
whitelistedPaths |
(empty) | Paths to allow real HTTP requests (comma-separated) |
blacklistedPaths |
(empty) | Paths to block from recording (comma-separated) |
Recording Modes
none: Only playback existing cassettes, never record new onesonce: Record new episodes only if the cassette file doesn't existnew_episodes: Record new requests, replay existing ones (recommended for development)all: Always re-record all requests, overwriting the entire cassette
Library Hooks
Multiple hooks can be enabled by separating them with commas:
stream_wrapper: Interceptsfile_get_contents(),fopen(), etc.curl: Intercepts cURL functionssoap: Intercepts SOAP client requests
Usage
The extension provides the #[UseCassette] attribute that can be applied to test classes or individual test methods.
Basic Usage
Recording HTTP requests for all tests in a class:
Recording HTTP requests for specific test methods:
Method-level cassettes override class-level cassettes:
Working with Different HTTP Clients
The extension works with any HTTP client that uses PHP's HTTP stream context:
Features
- Automatic State Management: The library automatically handles VCR state cleanup between tests, ensuring that tests without cassettes don't interfere with tests that use cassettes.
- Flexible Configuration: All VCR configuration options are supported through PHPUnit extension parameters.
- PHP 8+ Attributes: Uses modern PHP attributes for clean and readable test declarations.
- Mixed Test Support: Seamlessly handles test suites that mix tests with and without cassettes.
Advanced Usage
Working with Data Providers
The extension works seamlessly with PHPUnit data providers:
Understanding VCR Modes
The mode parameter controls how VCR handles HTTP requests:
none: Playback only. Throws exception if no matching cassette is found.once: Record new episodes only if cassette doesn't exist.new_episodes(recommended): Record new requests, replay existing ones.all: Always re-record all requests (overwrites cassette).
Cassette File Structure
Cassettes are stored as YAML (default) or JSON files containing HTTP request/response pairs:
Custom Request Matching
You can customize how requests are matched to cassette entries:
Testing Strategies
1. API Integration Tests
Use VCR for testing external API integrations:
2. Service Layer Tests
Record interactions with external services:
3. Webhook Testing
Test webhook handlers by recording webhook payloads:
Performance Tips
- Use specific request matchers: Only match what you need to avoid false matches
- Group related tests: Use class-level cassettes for related API calls
- Clean up cassettes: Remove outdated cassettes regularly
- Use
nonemode in CI: Ensure tests only use existing cassettes in production
Troubleshooting
Common Issues
Cassette Not Found
Solution: Check that the cassette file exists and the request matches exactly. Consider using fewer request matchers.
Permission Errors
Solution: Ensure the cassettes directory is writable:
Tests Failing in CI
Solution: Set VCR mode to none in CI to ensure only existing cassettes are used:
Outdated Cassettes
Solution: Delete cassette files and re-run tests with new_episodes mode:
Debug Mode
Enable VCR debug output by setting the VCR_DEBUG environment variable:
Development
Running Tests
Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-feature - Make your changes and add tests
- Run the test suite:
composer test - Submit a pull request
Please ensure:
- All tests pass
- Code follows PSR-12 standards
- New features include tests and documentation
Code Quality Tools
This project uses several quality tools:
- PHPStan: Static analysis (Level 8)
- PHP-CS-Fixer: Code style enforcement
- Infection: Mutation testing
- PHPUnit: Unit and integration testing
License
This project is licensed under the MIT License. See the LICENSE file for details.
Acknowledgments
- PHP-VCR - The underlying VCR library
- PHPUnit - The testing framework
- All contributors who have helped improve this library
Changelog
v2.0.0
- Added PHP 8+ attribute support
- Improved state management
- Added comprehensive test coverage
- Updated documentation
v1.0.0
- Initial release
- Basic VCR integration with PHPUnit
For detailed changes, see RELEASES.