class Factory mixin PendingRequest

Traits

Constants

protected GLOBAL_CONFIGURATION_DISABLED_CONTEXT_KEY_PREFIX

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.

mixed
withoutGlobalConfiguration(Closure $callback)

Execute a callback while requests are created without global middleware or global options.

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.

forgetConnectionHandlers()

Forget all resolved connection handlers.

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 771
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 98
__construct(Dispatcher|null $dispatcher = null)

Create a new factory instance.

Parameters

Dispatcher|null $dispatcher

at line 109
Factory globalMiddleware(callable $middleware)

Add middleware to apply to every request.

Boot-only. The middleware persists on the factory for the worker lifetime and affects every subsequently created pending request.

Parameters

callable $middleware

Return Value

Factory

at line 122
Factory globalRequestMiddleware(callable $middleware)

Add request middleware to apply to every request.

Boot-only. The middleware persists on the factory for the worker lifetime and affects every subsequently created pending request.

Parameters

callable $middleware

Return Value

Factory

at line 135
Factory globalResponseMiddleware(callable $middleware)

Add response middleware to apply to every request.

Boot-only. The middleware persists on the factory for the worker lifetime and affects every subsequently created pending request.

Parameters

callable $middleware

Return Value

Factory

at line 148
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 162
mixed withoutGlobalConfiguration(Closure $callback)

Execute a callback while requests are created without global middleware or global options.

Parameters

Closure $callback

Return Value

mixed

at line 183
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 200
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 227
static protected array normalizeResponseHeaders(array $headers)

Normalize the given fake response headers.

Parameters

array $headers

Return Value

array

Exceptions

InvalidArgumentException

at line 265
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 283
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 294
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 311
ResponseSequence sequence(array $responses = [])

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

Tests only. The sequence is retained by the factory for the worker lifetime.

Parameters

array $responses

Return Value

ResponseSequence

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

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

Tests only. Stubs persist on the factory for the worker lifetime and affect every subsequently created pending request.

Parameters

array|callable|null $callback

Return Value

Factory

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

Register a response sequence for the given URL pattern.

Tests only. The sequence and stub persist on the factory for the worker lifetime.

Parameters

string $url

Return Value

ResponseSequence

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

Stub the given URL using the given callback.

Tests only. The stub persists on the factory for the worker lifetime and affects every subsequently created pending request.

Parameters

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

Return Value

Factory

Exceptions

InvalidArgumentException

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

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

Boot or tests only. The policy persists on the factory for the worker lifetime and affects every subsequently created pending request.

Parameters

bool $prevent

Return Value

Factory

at line 433
bool preventingStrayRequests()

Determine if stray requests are being prevented.

Return Value

bool

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

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

Boot or tests only. The policy persists on the factory for the worker lifetime and affects every subsequently created pending request.

Parameters

array|null $only

Return Value

Factory

at line 462
Factory record()

Begin recording request / response pairs.

Tests only. Recording persists on the factory for the worker lifetime and retains every subsequently completed request attempt.

Return Value

Factory

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

Record a request response pair.

Parameters

Request $request
Response|null $response

Return Value

void

at line 482
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 493
void assertSentInOrder(array $callbacks)

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

Parameters

array $callbacks

Return Value

void

at line 515
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 526
void assertNothingSent()

Assert that no request / response pair was recorded.

Return Value

void

at line 537
void assertSentCount(int $count)

Assert how many requests have been recorded.

Parameters

int $count

Return Value

void

at line 545
void assertSequencesAreEmpty()

Assert that every created response sequence is empty.

Return Value

void

at line 558
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 578
PendingRequest createPendingRequest()

Create a new pending request instance for this factory.

Return Value

PendingRequest

at line 593
protected PendingRequest newPendingRequest()

Instantiate a new pending request instance for this factory.

Return Value

PendingRequest

at line 611
Dispatcher|null getDispatcher()

Get the current event dispatcher implementation.

Return Value

Dispatcher|null

at line 619
array getGlobalMiddleware()

Get the array of global middleware.

Return Value

array

at line 632
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 640
bool hasConnection(string $name)

Determine if the given HTTP client connection is registered.

Parameters

string $name

Return Value

bool

at line 648
callable getConnectionHandler(string $name)

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

Parameters

string $name

Return Value

callable

at line 671
protected callable createConnectionHandler(array $options)

Create a low-level transport handler for a connection.

Parameters

array $options

Return Value

callable

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

Create new Guzzle client.

Parameters

HandlerStack $handlerStack
CookieJar $cookies

Return Value

ClientInterface

at line 690
array getConnectionConfigs()

Get the configuration for all connections.

Return Value

array

at line 698
array getConnectionConfig(string $name)

Get the configuration for a specific connection.

Parameters

string $name

Return Value

array

at line 706
array getConnectionOptions(string $name)

Get the request-option preset for a registered connection.

Parameters

string $name

Return Value

array

at line 719
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 735
Factory forgetConnectionHandlers()

Forget all resolved connection handlers.

Boot or tests only. Request-time use forces subsequent requests to rebuild warmed keep-alive, DNS, and TLS session state.

Return Value

Factory

at line 745
protected void ensureConnectionIsRegistered(string $name)

Ensure the given HTTP client connection is registered.

Parameters

string $name

Return Value

void

at line 755
protected void validateConnectionConfig(array $config)

Validate a registered connection configuration.

Parameters

array $config

Return Value

void

at line 763
static void flushState()

Flush all static state.

Return Value

void