php-http/mock-client
Mock HTTP client for HTTPlug/PHP-HTTP. Stores outgoing requests and returns queued responses or throws exceptions instead of making real network calls—ideal for unit tests of HTTP-dependent code. Install via Composer as a dev dependency.
composer require --dev php-http/mock-client.MockClient and optionally inject a MessageFactory (e.g., from nyholm/psr7 or guzzlehttp/psr7) for creating responses/requests.addResponse() or addException() in the order they’ll be consumed.MockClient into your service (e.g., where a PSR-18/HTTPlug client is type-hinted).getRequests() or verify response behavior.First use case: Unit-testing a service that sends an API request—verify it formats the request correctly and handles a mocked success response.
MockClient instance per test to avoid cross-test contamination.addResponse() calls to simulate sequences (e.g., retry logic: first request fails with 503 → second succeeds).getRequests() to inspect headers, method, URI, and body—assert on actual HTTP semantics.addException(new \Http\Client\NetworkException(...)) to test error handling paths (e.g., retries, fallbacks).HttplugClientFactory or DI containers to inject mock clients into Symfony/Laravel services only in test environments.$mock->addResponse(...)), then implement client logic—validate compliance via getRequests().getRequests() retains all sent requests until manually cleared (no built-in reset)—use getRequests() before assertions, and avoid sharing instances across unrelated test phases.LogicException. Validate queue length matches expected calls.MessageFactory (e.g., Symfony’s Symfony\Component\HttpClient\Psr18Client won’t work directly—use nyholm/psr7 or php-http/discovery strategies).Http\Client\HttpClient (v2) and not deprecated v1 interfaces.MockClient to add helper methods like expectGet($uri) that queue and assert on expected endpoints, streamlining test readability.(string)$mock->getLastRequest()->getUri() when failures are subtle—this helps compare against test expectations.How can I help you explore Laravel packages today?