- How do I use this package to mock HTTP requests in Laravel unit tests?
- Install via Composer (`composer require psr-mock/http-client-implementation`), then inject the mock client into your test class. For Laravel’s built-in HTTP client, use dependency injection or replace `Http::client()` with the mock during tests. The package provides a PSR-18-compatible client you can configure to return predefined responses.
- Does this work with Laravel’s built-in HTTP client (since v9) or only third-party clients like Guzzle?
- Yes, it works with Laravel’s built-in HTTP client because it implements PSR-18, which Laravel’s client adheres to. You can swap the real client with this mock in tests without modifying production code. Third-party clients (e.g., Guzzle with PSR-18 adapters) are also supported.
- Can I simulate different responses based on request conditions (e.g., headers, query params)?
- Yes, the package allows you to inspect incoming requests (e.g., headers, body, method) and return dynamic responses. For example, you can mock a 401 response if an `Authorization` header is missing or simulate rate-limiting by delaying responses.
- Will this break if I upgrade Laravel to a newer version (e.g., v10+)?
- No, this package is PSR-18 compliant and has no Laravel-specific dependencies, so it remains compatible with future Laravel versions. However, ensure your tests use the same PSR-18 client interface as your production code to avoid mismatches.
- Is this better than Laravel’s `Http::fake()` for mocking external APIs?
- Both serve different purposes. `Http::fake()` is Laravel-specific and great for quick, declarative mocks, while this package offers programmatic control and PSR-18 compatibility. Use this if you need fine-grained request inspection or work with non-Laravel PSR-18 clients.
- How do I handle streaming responses (e.g., large file downloads) in tests?
- The package supports streaming responses by returning PSR-7 `StreamInterface` objects. You can mock streaming by creating a custom response with a `StreamInterface` (e.g., from a temporary file or in-memory stream) and setting appropriate headers like `Content-Length` or `Transfer-Encoding: chunked`.
- Does this package support middleware simulation (e.g., auth headers, retries)?
- Yes, you can simulate middleware by inspecting requests in your mock and modifying them before returning responses. For example, add an `Authorization` header to every request or throw exceptions to mimic retry logic. This is useful for testing auth flows or error handling.
- What’s the performance impact of using mocks vs. real HTTP calls in tests?
- Mocks are significantly faster since they avoid network latency. Tests become deterministic and run consistently across environments (CI, local). However, over-mocking complex workflows (e.g., OAuth) may reduce test realism—balance mocks with integration tests for critical paths.
- How do I integrate this with Pest or PHPUnit in Laravel?
- For Pest, bind the mock client in your `Pest.php` or test setup. For PHPUnit, use Laravel’s service container to resolve the mock during tests. Example: Replace `Http::client()` with the mock in your test’s `setUp()` method or use Laravel’s `partialMock()` for existing clients.
- Are there any known limitations or edge cases I should watch for?
- Edge cases include async clients (this package is synchronous) and complex streaming scenarios. Laravel-specific features like `Http::macro()` may require custom adapters. Also, avoid over-mocking—use integration tests for workflows where real HTTP behavior matters (e.g., OAuth, WebSockets).