API / Minn\Http / Fake

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.

Used by 1

Constants and properties

privatearray $sent
privatereadonly 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.

$answersarray<string, mixed>
methodhttp(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.

$withinlist<string>
methodanswer(Minn\Http\Outbound $request): Minn\Http\Exchange#

The answer for one request, which is recorded as sent.

methodsent(string $pattern = '*'): array#

The requests sent so far, oldest first; with a pattern, only those whose URL matches it.

@return list<Outbound>
staticreply(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.

$headersarray<string, string|list<string>>name => value; Set-Cookie values become cookies
staticmatches(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.

privateexchange(mixed $answer, string $url): Minn\Http\Exchange#