class MockClient

Properties

protected Fixture|MockResponse|Fixture|MockResponse> $sequenceResponses

Responses consumed in registration order.

protected Fixture|MockResponse|Fixture|MockResponse> $connectorResponses

Responses keyed by connector class.

protected Fixture|MockResponse|Fixture|MockResponse> $requestResponses

Responses keyed by request class.

protected Fixture|MockResponse|Fixture|MockResponse> $urlResponses

Responses keyed by URL pattern.

protected Response> $recordedResponses

Recorded Saloon responses.

protected bool $preventStrayRequests

Whether unmatched requests are prevented.

protected null|list<string> $allowedStrayRequestUrls

URL patterns allowed to continue without a fake.

protected bool $withoutResponseCache

Whether requests using this mock client bypass the response cache.

Methods

__construct(array $mockData = [])

Create a mock client.

void
addResponses(array $responses)

Add mock responses.

void
addResponse(MockResponse|Fixture|callable $response, string|null $captureMethod = null)

Add a mock response.

match(PendingRequest $pendingRequest)

Resolve the next matching response.

bool
isEmpty()

Determine if no mock responses remain.

preventStrayRequests(bool $prevent = true)

Prevent unmatched requests from reaching the network.

allowStrayRequests(array|null $only = null)

Allow unmatched requests to continue through the normal send lifecycle.

void
recordResponse(Response $response)

Record a response.

recorded(callable|null $callback = null)

Get recorded responses, optionally filtered by a callback.

Request|null
lastRequest()

Get the last recorded request.

lastPendingRequest()

Get the last recorded pending request.

Response|null
lastResponse()

Get the last recorded response.

void
assertSent(string|callable $value)

Assert that a matching request was sent.

void
assertNotSent(string|callable $value)

Assert that no matching request was sent.

void
assertSentInOrder(array $callbacks)

Assert that requests were sent in order.

void
assertNothingSent()

Assert that nothing was sent.

void
assertSentCount(int $count, string|null $requestClass = null)

Assert the number of recorded requests.

withoutCache()

Bypass the response cache for requests using this mock client.

bool
shouldBypassResponseCache()

Determine if requests using this mock client bypass the response cache.

static MockClient
global(array $mockData = [])

Register or retrieve the manager-backed global mock client.

static MockClient|null
getGlobal()

Get the manager-backed global mock client.

static void
destroyGlobal()

Destroy the manager-backed global mock client.

bool
requestWasSent(string|callable $value)

Determine if any recorded response matches the request condition.

bool
responseMatches(Response $response, string|callable $value)

Determine if the recorded response matches the request condition.

bool
requestMatchesType(Request $request, ReflectionType|null $type)

Determine if the request satisfies a closure parameter type.

bool
urlMatches(string $pattern, PendingRequest $pendingRequest)

Determine if the pending request's URL matches the pattern.

bool
allowsStrayRequest(PendingRequest $pendingRequest)

Determine if the pending request may continue without a fake.

resolveValue(MockResponse|Fixture|callable $value, PendingRequest $pendingRequest)

Resolve a configured response value.

static SaloonManager
manager()

Resolve the Saloon manager.

Details

at line 85
__construct(array $mockData = [])

Create a mock client.

Parameters

array $mockData

at line 95
void addResponses(array $responses)

Add mock responses.

Parameters

array $responses

Return Value

void

at line 107
void addResponse(MockResponse|Fixture|callable $response, string|null $captureMethod = null)

Add a mock response.

Parameters

MockResponse|Fixture|callable $response
string|null $captureMethod

Return Value

void

at line 133
MockResponse|Fixture|null match(PendingRequest $pendingRequest)

Resolve the next matching response.

Parameters

PendingRequest $pendingRequest

Return Value

MockResponse|Fixture|null

at line 170
bool isEmpty()

Determine if no mock responses remain.

Return Value

bool

at line 183
MockClient preventStrayRequests(bool $prevent = true)

Prevent unmatched requests from reaching the network.

Parameters

bool $prevent

Return Value

MockClient

at line 197
MockClient allowStrayRequests(array|null $only = null)

Allow unmatched requests to continue through the normal send lifecycle.

Parameters

array|null $only

Return Value

MockClient

at line 208
void recordResponse(Response $response)

Record a response.

Parameters

Response $response

Return Value

void

at line 218
Collection recorded(callable|null $callback = null)

Get recorded responses, optionally filtered by a callback.

Parameters

callable|null $callback

Return Value

Collection

at line 232
Request|null lastRequest()

Get the last recorded request.

Return Value

Request|null

at line 240
PendingRequest|null lastPendingRequest()

Get the last recorded pending request.

Return Value

PendingRequest|null

at line 248
Response|null lastResponse()

Get the last recorded response.

Return Value

Response|null

at line 256
void assertSent(string|callable $value)

Assert that a matching request was sent.

Parameters

string|callable $value

Return Value

void

at line 264
void assertNotSent(string|callable $value)

Assert that no matching request was sent.

Parameters

string|callable $value

Return Value

void

at line 274
void assertSentInOrder(array $callbacks)

Assert that requests were sent in order.

Parameters

array $callbacks

Return Value

void

at line 289
void assertNothingSent()

Assert that nothing was sent.

Return Value

void

at line 299
void assertSentCount(int $count, string|null $requestClass = null)

Assert the number of recorded requests.

Parameters

int $count
string|null $requestClass

Return Value

void

at line 316
MockClient withoutCache()

Bypass the response cache for requests using this mock client.

Return Value

MockClient

at line 326
bool shouldBypassResponseCache()

Determine if requests using this mock client bypass the response cache.

Return Value

bool

at line 342
static MockClient global(array $mockData = [])

Register or retrieve the manager-backed global mock client.

The client persists until the test application is destroyed or destroyGlobal() is called.

Parameters

array $mockData

Return Value

MockClient

at line 353
static MockClient|null getGlobal()

Get the manager-backed global mock client.

Return Value

MockClient|null

at line 363
static void destroyGlobal()

Destroy the manager-backed global mock client.

This removes the recorder for every subsequent Saloon request in the worker.

Return Value

void

at line 372
protected bool requestWasSent(string|callable $value)

Determine if any recorded response matches the request condition.

Parameters

string|callable $value

Return Value

bool

at line 386
protected bool responseMatches(Response $response, string|callable $value)

Determine if the recorded response matches the request condition.

Parameters

Response $response
string|callable $value

Return Value

bool

at line 409
protected bool requestMatchesType(Request $request, ReflectionType|null $type)

Determine if the request satisfies a closure parameter type.

Parameters

Request $request
ReflectionType|null $type

Return Value

bool

at line 443
protected bool urlMatches(string $pattern, PendingRequest $pendingRequest)

Determine if the pending request's URL matches the pattern.

A pattern may omit the scheme and host, and matches the URL with or without its query string.

Parameters

string $pattern
PendingRequest $pendingRequest

Return Value

bool

at line 454
protected bool allowsStrayRequest(PendingRequest $pendingRequest)

Determine if the pending request may continue without a fake.

Parameters

PendingRequest $pendingRequest

Return Value

bool

at line 475
protected MockResponse|Fixture resolveValue(MockResponse|Fixture|callable $value, PendingRequest $pendingRequest)

Resolve a configured response value.

Parameters

MockResponse|Fixture|callable $value
PendingRequest $pendingRequest

Return Value

MockResponse|Fixture

at line 483
static protected SaloonManager manager()

Resolve the Saloon manager.

Return Value

SaloonManager