mirror of
https://github.com/bs-community/blessing-skin-server.git
synced 2024-12-21 06:19:38 +08:00
3cf19d8656
This pull request applies code style fixes from an analysis carried out by [StyleCI](https://github.styleci.io). --- For more information, click [here](https://github.styleci.io/analyses/8wKwbZ).
69 lines
1.9 KiB
PHP
69 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Concerns;
|
|
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\HandlerStack;
|
|
use GuzzleHttp\Psr7\Response;
|
|
use GuzzleHttp\Handler\MockHandler;
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
/**
|
|
* @see http://docs.guzzlephp.org/en/stable/testing.html
|
|
* @see https://christrombley.me/blog/testing-guzzle-6-responses-with-laravel
|
|
*/
|
|
trait MocksGuzzleClient
|
|
{
|
|
/**
|
|
* @var MockHandler
|
|
*/
|
|
public $guzzleMockHandler;
|
|
|
|
/**
|
|
* Set up for mocking Guzzle HTTP client.
|
|
*
|
|
* @param Response|RequestException $responses
|
|
* @return void
|
|
*/
|
|
public function setupGuzzleClientMock($responses = null)
|
|
{
|
|
$this->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));
|
|
}
|
|
}
|