50 lines
1.5 KiB
PHP
50 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Weerscherm\Tests;
|
|
|
|
use Weerscherm\HttpClient;
|
|
use Weerscherm\HttpResponse;
|
|
|
|
/** Testdubbel: geeft geplande responses in volgorde terug en registreert elk request. */
|
|
final class FakeHttp implements HttpClient
|
|
{
|
|
/** @var list<HttpResponse> */
|
|
private array $queue = [];
|
|
|
|
/** @var list<array{method: string, url: string, params: array, headers: array}> */
|
|
public array $requests = [];
|
|
|
|
public function queue(int $status, array|string $body): self
|
|
{
|
|
$this->queue[] = new HttpResponse($status, is_string($body) ? $body : json_encode($body, JSON_THROW_ON_ERROR));
|
|
return $this;
|
|
}
|
|
|
|
public function get(string $url, array $query = [], array $headers = []): HttpResponse
|
|
{
|
|
return $this->take('GET', $url, $query, $headers);
|
|
}
|
|
|
|
public function postForm(string $url, array $fields): HttpResponse
|
|
{
|
|
return $this->take('POST', $url, $fields, []);
|
|
}
|
|
|
|
/** @return list<array> */
|
|
public function requestsOf(string $method): array
|
|
{
|
|
return array_values(array_filter($this->requests, fn(array $r) => $r['method'] === $method));
|
|
}
|
|
|
|
private function take(string $method, string $url, array $params, array $headers): HttpResponse
|
|
{
|
|
$this->requests[] = ['method' => $method, 'url' => $url, 'params' => $params, 'headers' => $headers];
|
|
if ($this->queue === []) {
|
|
throw new \LogicException("Geen response gepland voor $method $url");
|
|
}
|
|
return array_shift($this->queue);
|
|
}
|
|
}
|