- Can I use this package to mock API responses in Laravel unit tests?
- Yes, this package replaces real Guzzle HTTP calls with pre-defined file-based responses (JSON/XML), making it perfect for testing Laravel services that interact with external APIs. It integrates with Guzzle’s event system, so it works alongside Laravel’s HTTP client.
- Will this work with Laravel’s default Guzzle 6/7 client?
- No, it requires Guzzle v3.7, which is outdated and incompatible with Laravel’s default setup. You’ll need a polyfill, wrapper layer, or compatibility library to use it with modern Laravel. Consider alternatives if this is a blocker.
- How do I set up response mappings for different API endpoints?
- You configure responses by defining routes in a `ArrayConfiguration` object, mapping HTTP methods (GET, POST) to URLs and fixture files. For example, you can return different JSON responses for `grant_type=client_credentials` vs. `grant_type=password` by routing based on query parameters.
- Does this package support testing OAuth or authentication flows?
- Yes, it’s great for testing OAuth flows or authenticated API calls. You can mock token responses, error scenarios (e.g., invalid credentials), and conditional logic like different responses for `access_token` vs. `refresh_token` requests by routing based on request data.
- How do I handle non-200 HTTP responses like 404 or 500 errors?
- The package doesn’t natively support mocking exceptions or non-200 responses. You’ll need to manually configure fixtures with the appropriate status codes (e.g., `404.json`) and route requests to them, but there’s no built-in assertion or exception simulation feature.
- Is this package suitable for integration testing in Laravel?
- Yes, it’s designed for integration testing where you need to stub external API calls. However, it’s not ideal for end-to-end (E2E) testing, as it doesn’t interact with real APIs or browsers. For E2E, use Laravel Dusk or Pest with real API endpoints.
- How do I manage fixture files for mock responses?
- Store fixtures (e.g., `tests/fixtures/oauth/token_success.json`) and load them using `FileResourceLoader`. You can generate boilerplate fixtures with Laravel’s `copy()` or artisan commands, or use a dedicated `tests/fixtures/` directory for organization.
- What are the performance implications of using file-based responses?
- File I/O for loading responses can slow down tests if not optimized. Consider caching responses in memory (e.g., with Laravel’s cache system) or pre-loading fixtures during test setup to reduce overhead, especially in large test suites.
- Are there better alternatives for mocking HTTP requests in Laravel?
- Yes, consider Laravel’s native `Http::fake()` for simple cases, or libraries like `mockery` for dynamic mocking, or `vcr` for recording/replaying real API interactions. This package is niche due to its Guzzle v3.7 dependency and lack of modern features like dynamic responses.
- How do I integrate this into Laravel’s testing setup?
- Register the fake server in your `TestCase` or a dedicated `HttpTestCase` by overriding `getFakeServerClient()`. Inject the configured client into your service classes under test, replacing Laravel’s default HTTP client. Example: Use `HandlerStack` to wrap the fake server in Guzzle’s middleware.