tissue/tests/Unit/MetadataResolver/CreateMockedResolver.php

56 lines
1.3 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Unit\MetadataResolver;
use App\MetadataResolver\Resolver;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
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' => HandlerStack::create($this->handler)]);
$this->resolver = app()->make($resolverClass, ['client' => $client]);
2019-03-03 00:29:43 +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);
}
}