Skip to content

Instantly share code, notes, and snippets.

@carlosbelisario
Last active August 2, 2018 19:56
Show Gist options
  • Save carlosbelisario/91ac549267568a27a2b346ce1816f79f to your computer and use it in GitHub Desktop.
Save carlosbelisario/91ac549267568a27a2b346ce1816f79f to your computer and use it in GitHub Desktop.
<?php
namespace Tests;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
trait GuzzleMockTrait
{
public function guzzleMock($responseData, $status = 200, $contentType = 'application/json')
{
$response = is_array($responseData)
? new Response($status, ['Content-Type' => $contentType], json_encode($responseData))
: $responseData
;
$mock = new MockHandler([$response]);
$handler = HandlerStack::create($mock);
return new Client(['handler' => $handler]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment