guzzleMockHandler = new MockHandler($responses); $handler = HandlerStack::create($this->guzzleMockHandler); $client = new Client(['handler' => $handler]); // Inject to Laravel service container $this->app->instance(Client::class, $client); } /** * Add responses to Guzzle client's mock queue. * Pass a Response or RequestException instance, or an array of them. * * @param array|Response|RequestException|int $response * @param array $headers * @param string $body * @param string $version * @param string|null $reason */ public function appendToGuzzleQueue($response = 200, $headers = [], $body = '', $version = '1.1', $reason = null) { if (! $this->guzzleMockHandler) { $this->setupGuzzleClientMock(); } if (is_array($response)) { foreach ($response as $single) { $this->appendToGuzzleQueue($single); } return; } if ($response instanceof Response || $response instanceof RequestException) { return $this->guzzleMockHandler->append($response); } return $this->guzzleMockHandler->append(new Response($response, $headers, $body, $version, $reason)); } }