84 lines
2.4 KiB
PHP
84 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace HerrleinIT\LogHandler\Tests\Fakes;
|
|
|
|
use BadMethodCallException;
|
|
use GuzzleHttp\ClientInterface;
|
|
use GuzzleHttp\Promise\FulfilledPromise;
|
|
use GuzzleHttp\Promise\PromiseInterface;
|
|
use GuzzleHttp\Psr7\Response;
|
|
use Psr\Http\Message\RequestInterface;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Throwable;
|
|
|
|
class FakeLoghandlerClient implements ClientInterface
|
|
{
|
|
/** @var array<int, array{method:string,uri:string,options:array}> */
|
|
public array $requests = [];
|
|
|
|
/**
|
|
* @param array<int, ResponseInterface|Throwable|callable> $queue
|
|
*/
|
|
public function __construct(private array $queue = []) {}
|
|
|
|
public function request(string $method, $uri, array $options = []): ResponseInterface
|
|
{
|
|
$this->requests[] = [
|
|
'method' => strtoupper($method),
|
|
'uri' => (string) $uri,
|
|
'options' => $options,
|
|
];
|
|
|
|
if ($this->queue !== []) {
|
|
$next = array_shift($this->queue);
|
|
|
|
if ($next instanceof Throwable) {
|
|
throw $next;
|
|
}
|
|
|
|
if (is_callable($next)) {
|
|
$result = $next($this, $method, $uri, $options);
|
|
|
|
if ($result instanceof ResponseInterface) {
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
if ($next instanceof ResponseInterface) {
|
|
return $next;
|
|
}
|
|
}
|
|
|
|
return new Response(200);
|
|
}
|
|
|
|
public function requestAsync($method, $uri, array $options = []): PromiseInterface
|
|
{
|
|
return new FulfilledPromise($this->request($method, $uri, $options));
|
|
}
|
|
|
|
public function send(RequestInterface $request, array $options = []): ResponseInterface
|
|
{
|
|
return $this->request($request->getMethod(), $request->getUri(), $options + ['request' => $request]);
|
|
}
|
|
|
|
public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface
|
|
{
|
|
return $this->requestAsync($request->getMethod(), $request->getUri(), $options + ['request' => $request]);
|
|
}
|
|
|
|
public function getConfig(?string $option = null): mixed
|
|
{
|
|
if ($option !== null) {
|
|
throw new BadMethodCallException('Fake client does not expose configurable options.');
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
public function append(ResponseInterface|Throwable|callable $next): void
|
|
{
|
|
$this->queue[] = $next;
|
|
}
|
|
}
|