Last active
August 2, 2018 19:56
-
-
Save carlosbelisario/91ac549267568a27a2b346ce1816f79f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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