Symfony HTTP Client Compatibility Issue - Let's Fix It!#415
Symfony HTTP Client Compatibility Issue - Let's Fix It!#415ahmed-bhs wants to merge 25 commits intophp-vcr:masterfrom
Conversation
|
I'm currently a little bit busy, generally looks fine for me. Can you fix the pipeline please? |
Hey @higidi ! Thanks a lot for your consideration. The pipeline has just been fixed and ready to go! |
|
@ahmed-bhs pipeline still fails. I hope i'll get a slot to review your changes in detail this weekend. |
|
Hey @ahmed-bhs can I please invite you to make these changes on our fork of this? We have maintained it for years now, since the support here is patchy |
|
Hey @specialtactics, I know how it is with open-source—everyone’s busy, myself included 😅. I saw that the pipeline on PR #413 isn’t passing at the moment, but I’ll try to give you a quick hand with the tests. Regarding the Symfony HTTP Client integration I’m working on, it’s turning out to be a bit trickier than I expected. Once my changes get approved here, it would be awesome if you could turn your recent modifications from your fork CoverGenius/php-vcr into PRs on this repository. That way we can review everything in one place and make life a bit easier for everyone. |
Add symfony/http-client ^6.4|^7.0 as dev dependency and update PHPUnit to ^9.6 for better compatibility with PHP 8.x
Replace native PHP assertions with beberlei/assert for better error messages and validation. Ensure required keys exist before accessing them to prevent undefined array key warnings.
Add UrlReconstructor utility to rebuild complete URLs from parsed components. Handles edge cases like missing schemes, invalid URLs, and proper port handling (removes default ports 80/443).
- Add curlClose() method for proper resource cleanup - Clean up handles with NULL callbacks in disable() - Snapshot curl options before execution to prevent TraceableHttpClient issues - Add special handling for CURLINFO_PRIVATE - Improve gzip decompression with proper Accept-Encoding handling
Make wrapper_data property public to ensure stream_get_meta_data() compatibility with NativeHttpClient. Add proper header handling for both string and array formats.
Add namespaced proxy function to intercept stream_get_meta_data() calls in Symfony\Component\HttpClient\Response namespace. This converts StreamWrapperHook objects to arrays for NativeHttpClient compatibility.
Add wrapper class that adapts VCR Response objects to Symfony's ResponseInterface. Handles header normalization, status codes, and content retrieval with proper error handling.
Add main wrapper that intercepts Symfony HttpClient requests and replays VCR responses. Handles: - Automatic gzip decompression - Streaming responses - Request options normalization - Error handling and exceptions
Add specialized wrapper for NativeHttpClient that extends VCRHttpClient with additional compatibility fixes for PHP stream-based implementation.
Add helper class to convert between VCR Request/Response and Symfony format. Handles header normalization, body conversion, and proper HTTP method/URL extraction.
Add AST transformer that automatically wraps 'new CurlHttpClient()' and 'new NativeHttpClient()' instantiations with VCRHttpClient. Uses nikic/php-parser to modify code at runtime.
Add library hook that registers SymfonyHttpClientCodeTransform for automatic client wrapping. Integrates with VCR's code transformation system.
- Register 'symfony_http_client' hook in available hooks - Add cache directories to transformation blacklist - Update VCRFactory to instantiate the new hook
Add SymfonyHttpClientTestTrait with helper methods for testing Symfony HttpClient with VCR. Simplifies test setup and provides utility methods for common testing scenarios.
Add comprehensive integration tests covering: - CurlHttpClient (GET, POST, PUT, DELETE) - NativeHttpClient (GET, POST, PUT, DELETE) - MockHttpClient - TraceableHttpClient Includes JsonPlaceholderClient helper for testing real API calls.
Test individual Symfony HttpClient implementations: - CurlHttpClient with various options - NativeHttpClient with stream_get_meta_data - VCRNativeHttpClient wrapper - ScopingHttpClient with multiple scopes - TraceableHttpClient with request tracing
Test specific features: - Authentication (Basic Auth, Bearer tokens) - Concurrent/async requests - Custom options (headers, timeout, user_data, query params) - Error handling and edge cases
Test hook behavior: - Code transformation effectiveness - CurlHttpClient with curl hooks only (no transformation) - NativeHttpClient with stream_wrapper hooks only
Add 38 cassette files with recorded HTTP responses for: - CRUD operations (GET, POST, PUT, DELETE) - Authentication scenarios - Custom options and headers - Multiple client types - Error cases
Add comprehensive documentation explaining: - When to use high-level vs low-level hooks - VCRHttpClient vs CurlHook trade-offs - Architecture decisions and design rationale - Gzip decompression handling with Symfony
Add complete usage guide with: - Automatic wrapping example (recommended) - Manual wrapping alternative - List of supported clients - How it works explanation - Link to HOOK_STRATEGY.md
Update baseline to reflect new code and fix reported issues in Symfony HttpClient integration files.
- Ignore only dynamic cassettes (cassette* pattern) - Update test files to use correct paths
17ab29e to
dc3124c
Compare
|
This turned out to be more work than I first thought, especially since Symfony HTTP Client has a really complex response compression/decompression gzip logic and doesn’t always work seamlessly. @higidi and @specialtactics , I’d really appreciate it if you could review the code or at least test it out in a real project whenever you have some free time. |
b558c9a to
2978839
Compare
|
@ahmed-bhs unfortunately I am not longer working for my previous employer where those projects are, I will try and find someone who could try it. I will have a review of the code. |
Hey PHP VCR team!
I hope this message finds you well! I’ve stumbled upon a compatibility issue between PHP VCR and the Symfony HTTP Client that I'd love to help resolve. Currently, I’m facing this friendly little roadblock:
This issue has already been raised here: #329, and I believe we can make a positive impact by addressing it!
Why Symfony HTTP Client Rocks
The component is compatible with four distinct HTTP client abstractions: Symfony Contracts, PSR-18, HTTPlug v1/v2, and native PHP streams. This ensures seamless integration with any application that relies on these libraries.
The Symfony HTTP Client is a standout in the PHP community, known for its extensive features and wide adoption. Its interoperability with these HTTP abstractions offers significant advantages
I’ve put together a patch that enhances the extraction of cURL info in the
curlHook. By adding these essential properties to the cURL info list:We can empower PHP VCR to effortlessly retrieve certificate information and private data when using the Symfony HTTP Client.
I believe this contribution will significantly improve compatibility for Symfony projects, and I’m excited to collaborate with you to make it happen. Thank you for considering this fix !