class Factory mixin PendingRequest (View source)

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected Dispatcher|null $dispatcher

The event dispatcher implementation.

protected array $globalMiddleware

The middleware to apply to every request.

protected Closure|array $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 Response|null}> $recorded

The recorded response array.

protected ResponseSequence> $responseSequences

All created response sequences.

protected bool $preventStrayRequests

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

protected array<int, string> $allowedStrayRequestUrls

A list of URL patterns that are allowed to bypass the stray request guard.

Methods

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

Register a custom macro.

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

Mix another object into the class.

static bool
hasMacro(string $name)

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

$this
globalMiddleware(callable $middleware)

Add middleware to apply to every request.

$this
globalRequestMiddleware(callable $middleware)

Add request middleware to apply to every request.

$this
globalResponseMiddleware(callable $middleware)

Add response middleware to apply to every request.

$this
globalOptions(Closure|array $options)

Set the options to apply to every request.

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

Create a new response instance for use during stubbing.

static Response
psr7Response(array|string|null $body = null, int $status = 200, array<string, mixed> $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(scalar $value)

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

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

Create a new RequestException instance for use during stubbing.

static PromiseInterface
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.

$this
fake(callable|array<string, mixed>|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.

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

Stub the given URL using the given callback.

$this
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.

$this
allowStrayRequests(array|null $only = null)

Allow stray, unfaked requests entirely, or optionally allow only specific URLs.

$this
record()

Begin recording request / response pairs.

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

Record a request response pair.

void
assertSent(callable|Response|null): bool $callback)

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

void
assertSentInOrder(list<string|Response|null): bool|callable> $callbacks)

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

void
assertNotSent(callable|Response|null): bool $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.

Response|null}>
recorded(Response|null): bool|callable $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.

Details

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

Register a custom macro.

Parameters

string $name
object|callable $macro

Return Value

void

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

Mix another object into the class.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

static bool hasMacro(string $name)

Checks if macro is registered.

Parameters

string $name

Return Value

bool

static void flushMacros()

Flush the existing macros.

Return Value

void

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

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

mixed __call(string $method, array $parameters)

Execute a method against a new pending request instance.

Parameters

string $method
array $parameters

Return Value

mixed

__construct(Dispatcher|null $dispatcher = null)

Create a new factory instance.

Parameters

Dispatcher|null $dispatcher

$this globalMiddleware(callable $middleware)

Add middleware to apply to every request.

Parameters

callable $middleware

Return Value

$this

$this globalRequestMiddleware(callable $middleware)

Add request middleware to apply to every request.

Parameters

callable $middleware

Return Value

$this

$this globalResponseMiddleware(callable $middleware)

Add response middleware to apply to every request.

Parameters

callable $middleware

Return Value

$this

$this globalOptions(Closure|array $options)

Set the options to apply to every request.

Parameters

Closure|array $options

Return Value

$this

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

Create a new response instance for use during stubbing.

Parameters

array|string|null $body
int $status
array $headers

Return Value

PromiseInterface

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

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

Parameters

array|string|null $body
int $status
array<string, mixed> $headers

Return Value

Response

Exceptions

InvalidArgumentException

static protected array normalizeResponseHeaders(array $headers)

Normalize the given fake response headers.

Parameters

array $headers

Return Value

array

Exceptions

InvalidArgumentException

static protected string normalizeScalarString(scalar $value)

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

Parameters

scalar $value

Return Value

string

static RequestException failedRequest(array|string|null $body = null, int $status = 200, array<string, mixed> $headers = [])

Create a new RequestException instance for use during stubbing.

Parameters

array|string|null $body
int $status
array<string, mixed> $headers

Return Value

RequestException

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

Create a new connection exception for use during stubbing.

Parameters

string|null $message

Return Value

PromiseInterface

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

$this fake(callable|array<string, mixed>|null $callback = null)

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

Parameters

callable|array<string, mixed>|null $callback

Return Value

$this

ResponseSequence fakeSequence(string $url = '*')

Register a response sequence for the given URL pattern.

Parameters

string $url

Return Value

ResponseSequence

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

Stub the given URL using the given callback.

Parameters

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

Return Value

$this

Exceptions

InvalidArgumentException

$this preventStrayRequests(bool $prevent = true)

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

Parameters

bool $prevent

Return Value

$this

bool preventingStrayRequests()

Determine if stray requests are being prevented.

Return Value

bool

$this allowStrayRequests(array|null $only = null)

Allow stray, unfaked requests entirely, or optionally allow only specific URLs.

Parameters

array|null $only

Return Value

$this

$this record()

Begin recording request / response pairs.

Return Value

$this

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

Record a request response pair.

Parameters

Request $request
Response|null $response

Return Value

void

void assertSent(callable|Response|null): bool $callback)

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

Parameters

callable|Response|null): bool $callback

Return Value

void

void assertSentInOrder(list<string|Response|null): bool|callable> $callbacks)

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

Parameters

list<string|Response|null): bool|callable> $callbacks

Return Value

void

void assertNotSent(callable|Response|null): bool $callback)

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

Parameters

callable|Response|null): bool $callback

Return Value

void

void assertNothingSent()

Assert that no request / response pair was recorded.

Return Value

void

void assertSentCount(int $count)

Assert how many requests have been recorded.

Parameters

int $count

Return Value

void

void assertSequencesAreEmpty()

Assert that every created response sequence is empty.

Return Value

void

Response|null}> recorded(Response|null): bool|callable $callback = null)

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

Parameters

Response|null): bool|callable $callback

Return Value

Response|null}>

PendingRequest createPendingRequest()

Create a new pending request instance for this factory.

Return Value

PendingRequest

protected PendingRequest newPendingRequest()

Instantiate a new pending request instance for this factory.

Return Value

PendingRequest

Dispatcher|null getDispatcher()

Get the current event dispatcher implementation.

Return Value

Dispatcher|null

array getGlobalMiddleware()

Get the array of global middleware.

Return Value

array

Laravel Cloud is the best place to deploy Laravel, Nuxt, Express, Hono, Node.js, Bun, Go, Flask, Python, and more, with dedicated infrastructure available on AWS through Laravel Private Cloud.