MockHttpClient
in package
implements
HttpClientInterface, ResetInterface
uses
HttpClientTrait
A test-friendly HttpClient that doesn't make actual HTTP requests.
Tags
Table of Contents
Interfaces
- HttpClientInterface
- Provides flexible methods for requesting HTTP resources synchronously or asynchronously.
- ResetInterface
- Provides a way to reset an object to its initial state.
Properties
- $CHUNK_SIZE : int
- $defaultOptions : array<string|int, mixed>
- $requestsCount : int
- $responseFactory : ResponseInterface|Closure|iterable<string|int, mixed>|null
Methods
- __construct() : mixed
- getRequestsCount() : int
- request() : ResponseInterface
- Requests an HTTP resource.
- reset() : void
- setResponseFactory() : void
- stream() : ResponseStreamInterface
- Yields responses chunk by chunk as they complete.
- withOptions() : static
- Returns a new instance of the client with new default options.
- dechunk() : string
- getProxy() : array<string|int, mixed>|null
- Loads proxy configuration from the same environment variables as curl when no proxy is explicitly set.
- getProxyUrl() : string|null
- jsonEncode() : string
- mergeDefaultOptions() : array<string|int, mixed>
- mergeQueryString() : string|null
- Merges and encodes a query array with a query string.
- normalizeBody() : string|resource|Closure
- normalizeHeaders() : array<string|int, array<string|int, string>>
- normalizePeerFingerprint() : array<string|int, mixed>
- parseUrl() : array<string|int, mixed>
- Parses a URL and fixes its encoding if needed.
- prepareRequest() : array<string|int, mixed>
- Validates and normalizes method, URL and options, and merges them with defaults.
- removeDotSegments() : string
- Removes dot-segments from a path.
- resolveUrl() : array<string|int, mixed>
- Resolves a URL against a base URI.
- shouldBuffer() : bool
Properties
$CHUNK_SIZE
private
static int
$CHUNK_SIZE
= 16372
$defaultOptions
private
array<string|int, mixed>
$defaultOptions
= []
$requestsCount
private
int
$requestsCount
= 0
$responseFactory
private
ResponseInterface|Closure|iterable<string|int, mixed>|null
$responseFactory
Methods
__construct()
public
__construct([callable|array<string|int, callable>|ResponseInterface|array<string|int, ResponseInterface>|iterable<string|int, mixed>|null $responseFactory = null ][, string|null $baseUri = 'https://example.com' ]) : mixed
Parameters
- $responseFactory : callable|array<string|int, callable>|ResponseInterface|array<string|int, ResponseInterface>|iterable<string|int, mixed>|null = null
- $baseUri : string|null = 'https://example.com'
getRequestsCount()
public
getRequestsCount() : int
Return values
intrequest()
Requests an HTTP resource.
public
request(string $method, string $url[, array<string|int, mixed> $options = [] ]) : ResponseInterface
Responses MUST be lazy, but their status code MUST be checked even if none of their public methods are called.
Implementations are not required to support all options described above; they can also support more custom options; but in any case, they MUST throw a TransportExceptionInterface when an unsupported option is passed.
Parameters
- $method : string
- $url : string
- $options : array<string|int, mixed> = []
Return values
ResponseInterfacereset()
public
reset() : void
setResponseFactory()
public
setResponseFactory(callable|array<string|int, callable>|ResponseInterface|array<string|int, ResponseInterface>|iterable<string|int, mixed>|null $responseFactory) : void
Parameters
- $responseFactory : callable|array<string|int, callable>|ResponseInterface|array<string|int, ResponseInterface>|iterable<string|int, mixed>|null
stream()
Yields responses chunk by chunk as they complete.
public
stream(ResponseInterface|iterable<string|int, mixed> $responses[, float|null $timeout = null ]) : ResponseStreamInterface
Parameters
- $responses : ResponseInterface|iterable<string|int, mixed>
-
One or more responses created by the current HTTP client
- $timeout : float|null = null
-
The idle timeout before yielding timeout chunks
Return values
ResponseStreamInterfacewithOptions()
Returns a new instance of the client with new default options.
public
withOptions(array<string|int, mixed> $options) : static
Parameters
- $options : array<string|int, mixed>
Return values
staticdechunk()
private
static dechunk(string $body) : string
Parameters
- $body : string
Return values
stringgetProxy()
Loads proxy configuration from the same environment variables as curl when no proxy is explicitly set.
private
static getProxy(string|null $proxy, array<string|int, mixed> $url, string|null $noProxy) : array<string|int, mixed>|null
Parameters
- $proxy : string|null
- $url : array<string|int, mixed>
- $noProxy : string|null
Return values
array<string|int, mixed>|nullgetProxyUrl()
private
static getProxyUrl(string|null $proxy, array<string|int, mixed> $url) : string|null
Parameters
- $proxy : string|null
- $url : array<string|int, mixed>
Return values
string|nulljsonEncode()
private
static jsonEncode(mixed $value[, int|null $flags = null ][, int $maxDepth = 512 ]) : string
Parameters
- $value : mixed
- $flags : int|null = null
- $maxDepth : int = 512
Tags
Return values
stringmergeDefaultOptions()
private
static mergeDefaultOptions(array<string|int, mixed> $options, array<string|int, mixed> $defaultOptions[, bool $allowExtraOptions = false ]) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
- $defaultOptions : array<string|int, mixed>
- $allowExtraOptions : bool = false
Tags
Return values
array<string|int, mixed>mergeQueryString()
Merges and encodes a query array with a query string.
private
static mergeQueryString(string|null $queryString, array<string|int, mixed> $queryArray, bool $replace) : string|null
Parameters
- $queryString : string|null
- $queryArray : array<string|int, mixed>
- $replace : bool
Tags
Return values
string|nullnormalizeBody()
private
static normalizeBody(array<string|int, mixed>|string|resource|Traversable|Closure $body[, array<string|int, mixed> &$normalizedHeaders = [] ]) : string|resource|Closure
Parameters
- $body : array<string|int, mixed>|string|resource|Traversable|Closure
- $normalizedHeaders : array<string|int, mixed> = []
Tags
Return values
string|resource|ClosurenormalizeHeaders()
private
static normalizeHeaders(array<string|int, mixed> $headers) : array<string|int, array<string|int, string>>
Parameters
- $headers : array<string|int, mixed>
Tags
Return values
array<string|int, array<string|int, string>>normalizePeerFingerprint()
private
static normalizePeerFingerprint(mixed $fingerprint) : array<string|int, mixed>
Parameters
- $fingerprint : mixed
Tags
Return values
array<string|int, mixed>parseUrl()
Parses a URL and fixes its encoding if needed.
private
static parseUrl(string $url[, array<string|int, mixed> $query = [] ][, array<string|int, mixed> $allowedSchemes = ['http' => 80, 'https' => 443] ]) : array<string|int, mixed>
Parameters
- $url : string
- $query : array<string|int, mixed> = []
- $allowedSchemes : array<string|int, mixed> = ['http' => 80, 'https' => 443]
Tags
Return values
array<string|int, mixed>prepareRequest()
Validates and normalizes method, URL and options, and merges them with defaults.
private
static prepareRequest(string|null $method, string|null $url, array<string|int, mixed> $options[, array<string|int, mixed> $defaultOptions = [] ][, bool $allowExtraOptions = false ]) : array<string|int, mixed>
Parameters
- $method : string|null
- $url : string|null
- $options : array<string|int, mixed>
- $defaultOptions : array<string|int, mixed> = []
- $allowExtraOptions : bool = false
Tags
Return values
array<string|int, mixed>removeDotSegments()
Removes dot-segments from a path.
private
static removeDotSegments(string $path) : string
Parameters
- $path : string
Tags
Return values
stringresolveUrl()
Resolves a URL against a base URI.
private
static resolveUrl(array<string|int, mixed> $url, array<string|int, mixed>|null $base[, array<string|int, mixed> $queryDefaults = [] ]) : array<string|int, mixed>
Parameters
- $url : array<string|int, mixed>
- $base : array<string|int, mixed>|null
- $queryDefaults : array<string|int, mixed> = []
Tags
Return values
array<string|int, mixed>shouldBuffer()
private
static shouldBuffer(array<string|int, mixed> $headers) : bool
Parameters
- $headers : array<string|int, mixed>