summaryrefslogtreecommitdiffstats
path: root/tests/RecordingTransport.php
blob: da93fb3eabccc49c8d961d1ce27e82d9c2896db8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php

declare(strict_types=1);

namespace Cepis\Tests;

use Cepis\NetworkException;
use Cepis\Response;
use Cepis\Transport;

/**
 * Records what the client asked for and replays a canned list of responses.
 *
 * Used only to test the seam itself — what the client passes an implementer,
 * and how it reacts to what one returns. Every test of the client's own
 * behaviour goes over a real socket to {@see TestServer}.
 */
final class RecordingTransport implements Transport
{
    /** @var list<array{url: string, headers: array<string, string>, timeout: float}> */
    public array $calls = [];

    /** @param list<Response> $responses */
    public function __construct(private array $responses = [])
    {
    }

    public function get(string $url, array $headers, float $timeout): Response
    {
        $this->calls[] = ['url' => $url, 'headers' => $headers, 'timeout' => $timeout];

        $response = array_shift($this->responses);
        if ($response === null) {
            throw new NetworkException('no response left for ' . $url);
        }

        return $response;
    }
}