- Can I use httptest to mock HTTP requests in Laravel 9+ or PHP 8.0+?
- No, httptest was last updated in 2018 and may not support PHP 8.0+ or Laravel 9+. You’ll need polyfills like symfony/http-foundation v5.x or a custom wrapper to bridge compatibility gaps. Test thoroughly for breaking changes.
- How does httptest compare to Laravel’s built-in `Http::fake()` for testing?
- httptest offers more granular control over request/response mocking (e.g., templated responses, contract testing) but lacks Laravel-specific features like `actingAs()` or middleware simulation. `Http::fake()` is simpler and natively supported, so weigh your needs.
- Will httptest work with PestPHP or only PHPUnit?
- httptest is designed for PHPUnit, but it can integrate with PestPHP with minor adjustments (e.g., custom matchers). PestPHP’s `Http::fake()` might be a lower-effort alternative if you’re already using it.
- Can I use httptest to test Laravel’s HttpClient or Guzzle HTTP calls?
- Not directly—httptest targets PSR-7/HTTP-Foundation interfaces. You’ll need to wrap Laravel’s `HttpClient` or Guzzle instances in a compatible adapter or use a facade to bridge the gap.
- Does httptest support testing API contracts (e.g., OpenAPI/Swagger validation)?
- Yes, httptest can mock and assert HTTP responses to validate API contracts, but you’ll need to manually define expected payloads, headers, and status codes. No built-in OpenAPI/Swagger parsing exists.
- How do I handle middleware (e.g., Auth, CORS) in httptest?
- httptest doesn’t simulate Laravel middleware. Use workarounds like `Http::actingAs()` for auth or manually mock middleware responses in your test assertions.
- What’s the best way to migrate from httptest to Laravel’s native testing tools?
- Start by replacing simple mocks with `Http::fake()`, then gradually adopt PestPHP or PHPUnit’s native assertions. Audit tests for Laravel-specific quirks (e.g., redirects, sessions) that httptest doesn’t handle.
- Are there performance concerns with httptest in large test suites?
- httptest introduces minimal overhead for mocking, but complex response templating or assertions could slow tests. Benchmark against `Http::fake()`—Laravel’s native tool is optimized for performance.
- Can httptest test database or queue interactions triggered by HTTP calls?
- No, httptest focuses solely on HTTP layer mocking. For DB/queue interactions, combine it with Laravel’s testing tools (e.g., `refreshDatabase()`, `queueFake()`) or use transactional tests.
- What should I do if httptest breaks in a new Laravel/PHP version?
- Fork the package and update dependencies (e.g., PSR-7, Symfony components) or switch to alternatives like `mockery` for manual mocking or `vcr` for HTTP recording. Document the migration path in your test suite.