2019-02-17 03:19:27 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\MetadataResolver;
|
|
|
|
|
|
|
|
use App\MetadataResolver\Resolver;
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Handler\MockHandler;
|
2019-09-10 22:35:36 +09:00
|
|
|
use GuzzleHttp\HandlerStack;
|
2019-02-17 03:19:27 +09:00
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
use Monolog\Handler\AbstractHandler;
|
2020-08-22 13:04:46 +09:00
|
|
|
use Psr\Http\Message\RequestInterface;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2019-02-17 03:19:27 +09:00
|
|
|
|
|
|
|
trait CreateMockedResolver
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Resolver
|
|
|
|
*/
|
|
|
|
protected $resolver;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var AbstractHandler
|
|
|
|
*/
|
|
|
|
protected $handler;
|
|
|
|
|
2020-08-22 13:04:46 +09:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $snapshotFilename;
|
|
|
|
|
|
|
|
protected function fetchSnapshot(string $filename): string
|
|
|
|
{
|
|
|
|
$this->snapshotFilename = $filename;
|
|
|
|
|
|
|
|
return file_get_contents($filename);
|
|
|
|
}
|
|
|
|
|
2019-02-17 03:19:27 +09:00
|
|
|
/**
|
|
|
|
* @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)
|
|
|
|
{
|
2020-08-22 13:04:46 +09:00
|
|
|
if (!$this->shouldUseMock() && !$this->shouldUpdateSnapshot()) {
|
2019-02-17 03:19:27 +09:00
|
|
|
$this->resolver = app()->make($resolverClass);
|
|
|
|
|
|
|
|
return $this->resolver;
|
|
|
|
}
|
|
|
|
|
2020-08-22 13:04:46 +09:00
|
|
|
if ($this->shouldUseMock()) {
|
|
|
|
$headers += [
|
|
|
|
'content-type' => 'text/html',
|
|
|
|
];
|
|
|
|
|
|
|
|
$mockResponse = new Response($status, $headers, $responseText);
|
|
|
|
$this->handler = new MockHandler([$mockResponse]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$stack = HandlerStack::create($this->handler);
|
|
|
|
$client = new Client(['handler' => $stack]);
|
|
|
|
if ($this->shouldUpdateSnapshot()) {
|
|
|
|
$stack->push($this->makeUpdateSnapshotMiddleware());
|
|
|
|
}
|
2019-02-17 03:19:27 +09:00
|
|
|
|
|
|
|
$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
|
|
|
}
|
2020-08-22 13:04:46 +09:00
|
|
|
|
|
|
|
protected function shouldUpdateSnapshot(): bool
|
|
|
|
{
|
|
|
|
return (bool) env('TEST_UPDATE_SNAPSHOT', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function makeUpdateSnapshotMiddleware(): callable
|
|
|
|
{
|
|
|
|
return function (callable $next) {
|
|
|
|
return function (RequestInterface $request, array $options) use ($next) {
|
|
|
|
return $next($request, $options)->then(function (ResponseInterface $response) {
|
|
|
|
if (empty($this->snapshotFilename)) {
|
|
|
|
throw new \RuntimeException('スナップショットのファイル名が分かりません。file_get_contents()を使っている場合、fetchSnapshot()に置き換えてください。');
|
|
|
|
}
|
|
|
|
|
|
|
|
file_put_contents($this->snapshotFilename, (string) $response->getBody());
|
|
|
|
fwrite(STDERR, "Snapshot Updated: {$this->snapshotFilename}\n");
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
2019-02-17 03:19:27 +09:00
|
|
|
}
|