2019-02-17 03:19:27 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\MetadataResolver;
|
|
|
|
|
|
|
|
use App\MetadataResolver\Resolver;
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Handler\MockHandler;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
use Monolog\Handler\AbstractHandler;
|
|
|
|
|
|
|
|
trait CreateMockedResolver
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Resolver
|
|
|
|
*/
|
|
|
|
protected $resolver;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var AbstractHandler
|
|
|
|
*/
|
|
|
|
protected $handler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $resolverClass
|
|
|
|
* @param string $responseText
|
|
|
|
* @param array $headers
|
|
|
|
* @param int $status
|
|
|
|
* @return Resolver
|
|
|
|
*/
|
|
|
|
protected function createResolver(string $resolverClass, string $responseText, array $headers = [], int $status = 200)
|
|
|
|
{
|
|
|
|
if (!$this->shouldUseMock()) {
|
|
|
|
$this->resolver = app()->make($resolverClass);
|
|
|
|
|
|
|
|
return $this->resolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
$headers += [
|
|
|
|
'content-type' => 'text/html',
|
|
|
|
];
|
|
|
|
|
|
|
|
$mockResponse = new Response($status, $headers, $responseText);
|
|
|
|
$this->handler = new MockHandler([$mockResponse]);
|
|
|
|
$client = new Client(['handler' => $this->handler]);
|
|
|
|
$this->resolver = app()->make($resolverClass, ['client' => $client]);
|
2019-03-03 00:29:43 +09:00
|
|
|
|
2019-02-17 03:19:27 +09:00
|
|
|
return $this->resolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function shouldUseMock(): bool
|
|
|
|
{
|
2019-07-02 12:44:25 +09:00
|
|
|
return (bool) env('TEST_USE_HTTP_MOCK', true);
|
2019-02-17 03:19:27 +09:00
|
|
|
}
|
|
|
|
}
|