tomb1n0/guzzle-mock-handler
Lightweight mock handler for Guzzle to fake HTTP responses in tests. Queue responses, simulate status codes, headers, and body content, and inspect requests without making real network calls—useful for unit/integration testing of API clients.
Start by installing the package via Composer: composer require --dev tomb1n0/guzzle-mock-handler. It provides a custom Guzzle handler stack compatible with PHPUnit tests—ideal for mocking HTTP responses without relying on external services. The first use case is typically mocking API client calls in unit tests: inject the mock handler into your Guzzle client, define expected requests/responses via GuzzleHttp\MockHandler::append(), and assert interactions using MockHandler::getInfo() or custom assertions.
MockHandler, prepending it to your client’s handler stack. Use append() to queue responses matching expected requests.Request and Response instances (e.g., new Response(200, [], json_encode($data))) to simulate real endpoints precisely.MockHandler::getLastRequest() (or track in tests) to inspect headers, body, or URI of outgoing requests.HandlerStack::create($mockHandler)), otherwise other handlers may interfere.assertNotSame or断言 to verify no HTTP client leaks to production).MockHandler isn’t state-resetting automatically; initialize it fresh per test case to avoid stale responses in parallel test runs.mockery or phpspec, it doesn’t provide rich mocking DSL—combine with PHPUnit assertions for clarity.MockHandler with Middleware::mock() or create custom handler wrappers to inject request validation logic.How can I help you explore Laravel packages today?