class MockClient

Traits

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.

Methods

string
firstClosureParameterType(Closure $closure)

Get the class name of the first parameter of the given Closure.

array
firstClosureParameterTypes(Closure $closure)

Get the class names of the first parameter of the given Closure, including union types.

array
closureParameterTypes(Closure $closure)

Get the class names / types of the parameters of the given Closure.

array
closureReturnTypes(Closure $closure)

Get the class names / types of the return type of the given Closure.

__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.

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
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

in ReflectsClosures at line 26
protected string firstClosureParameterType(Closure $closure)

Get the class name of the first parameter of the given Closure.

Parameters

Closure $closure

Return Value

string

Exceptions

ReflectionException
RuntimeException

in ReflectsClosures at line 49
protected array firstClosureParameterTypes(Closure $closure)

Get the class names of the first parameter of the given Closure, including union types.

Parameters

Closure $closure

Return Value

array

Exceptions

ReflectionException
RuntimeException

in ReflectsClosures at line 75
protected array closureParameterTypes(Closure $closure)

Get the class names / types of the parameters of the given Closure.

Parameters

Closure $closure

Return Value

array

Exceptions

ReflectionException

in ReflectsClosures at line 97
protected array closureReturnTypes(Closure $closure)

Get the class names / types of the return type of the given Closure.

Parameters

Closure $closure

Return Value

array

Exceptions

ReflectionException

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

Create a mock client.

Parameters

array $mockData

at line 88
void addResponses(array $responses)

Add mock responses.

Parameters

array $responses

Return Value

void

at line 100
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 126
MockResponse|Fixture|null match(PendingRequest $pendingRequest)

Resolve the next matching response.

Parameters

PendingRequest $pendingRequest

Return Value

MockResponse|Fixture|null

at line 163
bool isEmpty()

Determine if no mock responses remain.

Return Value

bool

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

Prevent unmatched requests from reaching the network.

Parameters

bool $prevent

Return Value

MockClient

at line 190
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 201
void recordResponse(Response $response)

Record a response.

Parameters

Response $response

Return Value

void

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

Get recorded responses, optionally filtered by a callback.

Parameters

callable|null $callback

Return Value

Collection

at line 225
Request|null lastRequest()

Get the last recorded request.

Return Value

Request|null

at line 233
PendingRequest|null lastPendingRequest()

Get the last recorded pending request.

Return Value

PendingRequest|null

at line 241
Response|null lastResponse()

Get the last recorded response.

Return Value

Response|null

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

Assert that a matching request was sent.

Parameters

string|callable $value

Return Value

void

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

Assert that no matching request was sent.

Parameters

string|callable $value

Return Value

void

at line 267
void assertSentInOrder(array $callbacks)

Assert that requests were sent in order.

Parameters

array $callbacks

Return Value

void

at line 282
void assertNothingSent()

Assert that nothing was sent.

Return Value

void

at line 292
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 312
static MockClient global(array $mockData = [])

Register or retrieve the manager-backed global mock client.

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

Parameters

array $mockData

Return Value

MockClient

at line 322
static MockClient|null getGlobal()

Get the manager-backed global mock client.

Return Value

MockClient|null

at line 330
static void destroyGlobal()

Destroy the manager-backed global mock client.

Return Value

void

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

Determine if any recorded response matches the request condition.

Parameters

string|callable $value

Return Value

bool

at line 352
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 379
protected bool allowsStrayRequest(PendingRequest $pendingRequest)

Determine if the pending request may continue without a fake.

Parameters

PendingRequest $pendingRequest

Return Value

bool

at line 400
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 408
static protected SaloonManager manager()

Resolve the Saloon manager.

Return Value

SaloonManager