class Factory mixin PendingRequest

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected array $globalMiddleware

The middleware to apply to every request.

protected array|Closure $globalOptions

The options to apply to every request.

protected Collection $stubCallbacks

The stub callables that will handle requests.

protected bool $recording

Indicates if the factory is recording requests and responses.

protected array $recorded

The recorded response array.

protected array $responseSequences

All created response sequences.

protected bool $preventStrayRequests

Indicates that an exception should be thrown if any request is not faked.

protected array $allowedStrayRequestUrls

The URL patterns that are allowed as stray requests.

protected array $connectionConfigs

The configuration for all registered connections.

protected array<string, callable> $connectionHandlers

The resolved low-level transport handlers for registered connections.

Methods

static void
macro(string $name, callable|object $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Check if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Execute a method against a new pending request instance.

__construct(Dispatcher|null $dispatcher = null)

Create a new factory instance.

globalMiddleware(callable $middleware)

Add middleware to apply to every request.

globalRequestMiddleware(callable $middleware)

Add request middleware to apply to every request.

globalResponseMiddleware(callable $middleware)

Add response middleware to apply to every request.

globalOptions(array|Closure $options)

Set the options to apply to every request.

static PromiseInterface
response(mixed $body = null, int $status = 200, array $headers = [])

Create a new response instance for use during stubbing.

static Response
psr7Response(mixed $body = null, int $status = 200, array $headers = [])

Create a new PSR-7 response instance for use during stubbing.

static array
normalizeResponseHeaders(array $headers)

Normalize the given fake response headers.

static string
normalizeScalarString(bool|float|int|string $value)

Normalize a scalar to a string without triggering PHP 8.5 non-finite float warnings.

failedRequest(mixed $body = null, int $status = 200, array $headers = [])

Create a new RequestException instance for use during stubbing.

static Closure
failedConnection(string|null $message = null)

Create a new connection exception for use during stubbing.

sequence(array $responses = [])

Get an invokable object that returns a sequence of responses in order for use during stubbing.

fake(array|callable|null $callback = null)

Register a stub callable that will intercept requests and be able to return stub responses.

fakeSequence(string $url = '*')

Register a response sequence for the given URL pattern.

stubUrl(string $url, array|callable|int|PromiseInterface|Response|string $callback)

Stub the given URL using the given callback.

preventStrayRequests(bool $prevent = true)

Indicate that an exception should be thrown if any request is not faked.

bool
preventingStrayRequests()

Determine if stray requests are being prevented.

allowStrayRequests(array|null $only = null)

Indicate that an exception should not be thrown if any request is not faked.

record()

Begin recording request / response pairs.

void
recordRequestResponsePair(Request $request, Response|null $response)

Record a request response pair.

void
assertSent(callable $callback)

Assert that a request / response pair was recorded matching a given truth test.

void
assertSentInOrder(array $callbacks)

Assert that the given request was sent in the given order.

void
assertNotSent(callable $callback)

Assert that a request / response pair was not recorded matching a given truth test.

void
assertNothingSent()

Assert that no request / response pair was recorded.

void
assertSentCount(int $count)

Assert how many requests have been recorded.

void
assertSequencesAreEmpty()

Assert that every created response sequence is empty.

recorded(callable|null $callback = null)

Get a collection of the request / response pairs matching the given truth test.

createPendingRequest()

Create a new pending request instance for this factory.

newPendingRequest()

Instantiate a new pending request instance for this factory.

Dispatcher|null
getDispatcher()

Get the current event dispatcher implementation.

array
getGlobalMiddleware()

Get the array of global middleware.

registerConnection(string $name, array $config = [])

Register a connection with the given name and configuration.

bool
hasConnection(string $name)

Determine if the given HTTP client connection is registered.

callable
getConnectionHandler(string $name)

Get the shared low-level transport handler for a connection.

callable
createConnectionHandler(array $options)

Create a low-level transport handler for a connection.

ClientInterface
createClient(HandlerStack $handlerStack, CookieJar $cookies)

Create new Guzzle client.

array
getConnectionConfigs()

Get the configuration for all connections.

array
getConnectionConfig(string $name)

Get the configuration for a specific connection.

array
getConnectionOptions(string $name)

Get the request-option preset for a registered connection.

setConnectionConfig(string $name, array $config)

Set the configuration for a specific connection.

void
ensureConnectionIsRegistered(string $name)

Ensure the given HTTP client connection is registered.

void
validateConnectionConfig(array $config)

Validate a registered connection configuration.

static void
flushState()

Flush all static state.

Details

in Macroable at line 28
static void macro(string $name, callable|object $macro)

Register a custom macro.

Boot-only. Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.

Parameters

string $name
callable|object $macro

Return Value

void

in Macroable at line 41
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Boot-only. Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

in Macroable at line 57
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 68
static void flushMacros()

Flush the existing macros.

Boot or tests only. Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.

Return Value

void

in Macroable at line 78
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

at line 670
mixed __call(string $method, array $parameters)

Execute a method against a new pending request instance.

Parameters

string $method
array $parameters

Return Value

mixed

at line 93
__construct(Dispatcher|null $dispatcher = null)

Create a new factory instance.

Parameters

Dispatcher|null $dispatcher

at line 101
Factory globalMiddleware(callable $middleware)

Add middleware to apply to every request.

Parameters

callable $middleware

Return Value

Factory

at line 111
Factory globalRequestMiddleware(callable $middleware)

Add request middleware to apply to every request.

Parameters

callable $middleware

Return Value

Factory

at line 121
Factory globalResponseMiddleware(callable $middleware)

Add response middleware to apply to every request.

Parameters

callable $middleware

Return Value

Factory

at line 134
Factory globalOptions(array|Closure $options)

Set the options to apply to every request.

Boot-only. The options persist on the factory for the worker lifetime and affect every subsequently created pending request.

Parameters

array|Closure $options

Return Value

Factory

at line 144
static PromiseInterface response(mixed $body = null, int $status = 200, array $headers = [])

Create a new response instance for use during stubbing.

Parameters

mixed $body
int $status
array $headers

Return Value

PromiseInterface

at line 159
static Response psr7Response(mixed $body = null, int $status = 200, array $headers = [])

Create a new PSR-7 response instance for use during stubbing.

Parameters

mixed $body
int $status
array $headers

Return Value

Response

Exceptions

InvalidArgumentException

at line 186
static protected array normalizeResponseHeaders(array $headers)

Normalize the given fake response headers.

Parameters

array $headers

Return Value

array

Exceptions

InvalidArgumentException

at line 224
static protected string normalizeScalarString(bool|float|int|string $value)

Normalize a scalar to a string without triggering PHP 8.5 non-finite float warnings.

Parameters

bool|float|int|string $value

Return Value

string

at line 240
static RequestException failedRequest(mixed $body = null, int $status = 200, array $headers = [])

Create a new RequestException instance for use during stubbing.

Parameters

mixed $body
int $status
array $headers

Return Value

RequestException

at line 251
static Closure failedConnection(string|null $message = null)

Create a new connection exception for use during stubbing.

Parameters

string|null $message

Return Value

Closure

at line 266
ResponseSequence sequence(array $responses = [])

Get an invokable object that returns a sequence of responses in order for use during stubbing.

Parameters

array $responses

Return Value

ResponseSequence

at line 274
Factory fake(array|callable|null $callback = null)

Register a stub callable that will intercept requests and be able to return stub responses.

Parameters

array|callable|null $callback

Return Value

Factory

at line 320
ResponseSequence fakeSequence(string $url = '*')

Register a response sequence for the given URL pattern.

Parameters

string $url

Return Value

ResponseSequence

at line 330
Factory stubUrl(string $url, array|callable|int|PromiseInterface|Response|string $callback)

Stub the given URL using the given callback.

Parameters

string $url
array|callable|int|PromiseInterface|Response|string $callback

Return Value

Factory

at line 363
Factory preventStrayRequests(bool $prevent = true)

Indicate that an exception should be thrown if any request is not faked.

Parameters

bool $prevent

Return Value

Factory

at line 373
bool preventingStrayRequests()

Determine if stray requests are being prevented.

Return Value

bool

at line 381
Factory allowStrayRequests(array|null $only = null)

Indicate that an exception should not be thrown if any request is not faked.

Parameters

array|null $only

Return Value

Factory

at line 396
protected Factory record()

Begin recording request / response pairs.

Return Value

Factory

at line 406
void recordRequestResponsePair(Request $request, Response|null $response)

Record a request response pair.

Parameters

Request $request
Response|null $response

Return Value

void

at line 416
void assertSent(callable $callback)

Assert that a request / response pair was recorded matching a given truth test.

Parameters

callable $callback

Return Value

void

at line 427
void assertSentInOrder(array $callbacks)

Assert that the given request was sent in the given order.

Parameters

array $callbacks

Return Value

void

at line 449
void assertNotSent(callable $callback)

Assert that a request / response pair was not recorded matching a given truth test.

Parameters

callable $callback

Return Value

void

at line 460
void assertNothingSent()

Assert that no request / response pair was recorded.

Return Value

void

at line 471
void assertSentCount(int $count)

Assert how many requests have been recorded.

Parameters

int $count

Return Value

void

at line 479
void assertSequencesAreEmpty()

Assert that every created response sequence is empty.

Return Value

void

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

Get a collection of the request / response pairs matching the given truth test.

Parameters

callable|null $callback

Return Value

Collection

at line 509
PendingRequest createPendingRequest()

Create a new pending request instance for this factory.

Return Value

PendingRequest

at line 522
protected PendingRequest newPendingRequest()

Instantiate a new pending request instance for this factory.

Return Value

PendingRequest

at line 536
Dispatcher|null getDispatcher()

Get the current event dispatcher implementation.

Return Value

Dispatcher|null

at line 544
array getGlobalMiddleware()

Get the array of global middleware.

Return Value

array

at line 555
Factory registerConnection(string $name, array $config = [])

Register a connection with the given name and configuration.

Boot-only. The connection preset and shared transport handler registry persist on the factory for the worker lifetime.

Parameters

string $name
array $config

Return Value

Factory

at line 563
bool hasConnection(string $name)

Determine if the given HTTP client connection is registered.

Parameters

string $name

Return Value

bool

at line 571
callable getConnectionHandler(string $name)

Get the shared low-level transport handler for a connection.

Parameters

string $name

Return Value

callable

at line 583
protected callable createConnectionHandler(array $options)

Create a low-level transport handler for a connection.

Parameters

array $options

Return Value

callable

at line 591
ClientInterface createClient(HandlerStack $handlerStack, CookieJar $cookies)

Create new Guzzle client.

Parameters

HandlerStack $handlerStack
CookieJar $cookies

Return Value

ClientInterface

at line 602
array getConnectionConfigs()

Get the configuration for all connections.

Return Value

array

at line 610
array getConnectionConfig(string $name)

Get the configuration for a specific connection.

Parameters

string $name

Return Value

array

at line 618
array getConnectionOptions(string $name)

Get the request-option preset for a registered connection.

Parameters

string $name

Return Value

array

at line 631
Factory setConnectionConfig(string $name, array $config)

Set the configuration for a specific connection.

Boot-only. Reconfiguration replaces the worker-lifetime preset and invalidates its shared transport handler for subsequent requests.

Parameters

string $name
array $config

Return Value

Factory

at line 644
protected void ensureConnectionIsRegistered(string $name)

Ensure the given HTTP client connection is registered.

Parameters

string $name

Return Value

void

at line 654
protected void validateConnectionConfig(array $config)

Validate a registered connection configuration.

Parameters

array $config

Return Value

void

at line 662
static void flushState()

Flush all static state.

Return Value

void