Fake final class
Minn\Http\Fake · public/minn/src/Minn/Http/Fake.php · 94 lines
Answers requests in place of the network, and keeps what was sent. A test makes one and hands its client to the code it tests:
$fake = new Fake(['api.example.com/*' => ['ok' => true]]); $result = (new StatusCheck($fake->http()))->run(); $fake->sent('api.example.com/status'); // the requests that went there
Only that client is answered, so there is nothing to put back and nothing else in the process sees the fake.
Constants and properties
| private | array $sent | |
| private | readonly array $answers | Promoted from the constructor. |
Public
| new | __construct(array $answers = array ( ))# | Keys are URL patterns (* matches anything; the scheme and query may be left off); values are an array (sent as JSON), a string (the body), a status code, an Exchange from reply(), or a closure taking the Outbound and returning one of those. A request no pattern matches fails as if nothing were listening.
| |||
| method | http(array $within = array ( )): Minn\Http# | A client this fake answers, bound to $within as an extension's is by its needs.http. The destination rules still run, but names are not resolved.
| |||
| method | answer(Minn\Http\Outbound $request): Minn\Http\Exchange# | The answer for one request, which is recorded as sent. | |||
| method | sent(string $pattern = '*'): array# | The requests sent so far, oldest first; with a pattern, only those whose URL matches it. @return list<Outbound> | |||
| static | reply(array|string $body = '', int $status = 200, array $headers = array ( )): Minn\Http\Exchange# | A response made by hand, for a fake to answer with. An array body is sent as JSON.
| |||
| static | matches(string $pattern, string $url): bool# | Whether a URL matches a pattern in which * stands for anything; the scheme and the query string may be left off. |
Internals 1
Private and protected methods: how the class does its work. Not a contract; they change whenever the code is tidied.
| private | exchange(mixed $answer, string $url): Minn\Http\Exchange# |
Nothing matches.