MemoryHttpClient
in package
implements
IHttpClient
In-memory HTTP client implementation for testing.
Provides programmable responses without making actual HTTP requests. Perfect for testing HTTP-dependent code without network calls.
Table of Contents
Interfaces
- IHttpClient
- Interface for HTTP client operations abstraction.
Properties
- $defaultHeaders : array<string|int, mixed>
- $requests : array<string|int, mixed>
- $responses : array<string|int, mixed>
- $timeout : int
Methods
- addErrorResponse() : void
- Add an error response
- addResponse() : void
- Add a canned response for a specific URL pattern
- addSuccessResponse() : void
- Add a simple successful response
- clearRequests() : void
- Clear all recorded requests
- delete() : IHttpResponse
- Perform DELETE request
- get() : IHttpResponse
- Perform GET request
- getLastRequest() : array<string|int, mixed>|null
- Get last recorded request
- getRequests() : array<string|int, mixed>
- Get all recorded requests
- post() : IHttpResponse
- Perform POST request with form data
- postJson() : IHttpResponse
- Perform POST request with JSON body
- put() : IHttpResponse
- Perform PUT request
- setDefaultHeaders() : void
- Set default headers for all requests
- setTimeout() : void
- Set timeout for requests
- findResponse() : IHttpResponse|null
- Find canned response for method and URL
- recordAndRespond() : IHttpResponse
- Record request and return canned response
Properties
$defaultHeaders
private
array<string|int, mixed>
$defaultHeaders
= []
$requests
private
array<string|int, mixed>
$requests
= []
$responses
private
array<string|int, mixed>
$responses
= []
$timeout
private
int
$timeout
= 30
Methods
addErrorResponse()
Add an error response
public
addErrorResponse(string $urlPattern, string $errorMessage[, int $errorCode = 1 ][, string $method = '*' ]) : void
Parameters
- $urlPattern : string
-
URL pattern
- $errorMessage : string
-
Error message
- $errorCode : int = 1
-
Error code
- $method : string = '*'
-
HTTP method
addResponse()
Add a canned response for a specific URL pattern
public
addResponse(string $urlPattern, IHttpResponse $response[, string $method = '*' ]) : void
Parameters
- $urlPattern : string
-
URL or URL pattern (supports * wildcard)
- $response : IHttpResponse
-
Response to return
- $method : string = '*'
-
HTTP method (GET, POST, etc.) or '*' for any
addSuccessResponse()
Add a simple successful response
public
addSuccessResponse(string $urlPattern[, string $body = '' ][, int $statusCode = 200 ][, string $method = '*' ]) : void
Parameters
- $urlPattern : string
-
URL pattern
- $body : string = ''
-
Response body
- $statusCode : int = 200
-
Status code (default 200)
- $method : string = '*'
-
HTTP method
clearRequests()
Clear all recorded requests
public
clearRequests() : void
delete()
Perform DELETE request
public
delete(string $url[, array<string|int, mixed> $headers = [] ]) : IHttpResponse
Parameters
- $url : string
-
Request URL
- $headers : array<string|int, mixed> = []
-
Additional headers
Tags
Return values
IHttpResponse —Response object
get()
Perform GET request
public
get(string $url[, array<string|int, mixed> $params = [] ][, array<string|int, mixed> $headers = [] ]) : IHttpResponse
Parameters
- $url : string
-
Request URL
- $params : array<string|int, mixed> = []
-
Query parameters
- $headers : array<string|int, mixed> = []
-
Additional headers
Tags
Return values
IHttpResponse —Response object
getLastRequest()
Get last recorded request
public
getLastRequest() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null —Request info or null
getRequests()
Get all recorded requests
public
getRequests() : array<string|int, mixed>
Return values
array<string|int, mixed> —Array of request info
post()
Perform POST request with form data
public
post(string $url[, array<string|int, mixed> $data = [] ][, array<string|int, mixed> $headers = [] ]) : IHttpResponse
Parameters
- $url : string
-
Request URL
- $data : array<string|int, mixed> = []
-
POST data
- $headers : array<string|int, mixed> = []
-
Additional headers
Tags
Return values
IHttpResponse —Response object
postJson()
Perform POST request with JSON body
public
postJson(string $url, string $json[, array<string|int, mixed> $headers = [] ]) : IHttpResponse
Parameters
- $url : string
-
Request URL
- $json : string
-
JSON string
- $headers : array<string|int, mixed> = []
-
Additional headers
Tags
Return values
IHttpResponse —Response object
put()
Perform PUT request
public
put(string $url[, array<string|int, mixed> $data = [] ][, array<string|int, mixed> $headers = [] ]) : IHttpResponse
Parameters
- $url : string
-
Request URL
- $data : array<string|int, mixed> = []
-
PUT data
- $headers : array<string|int, mixed> = []
-
Additional headers
Tags
Return values
IHttpResponse —Response object
setDefaultHeaders()
Set default headers for all requests
public
setDefaultHeaders(array<string|int, mixed> $headers) : void
Parameters
- $headers : array<string|int, mixed>
-
Headers to set
Tags
setTimeout()
Set timeout for requests
public
setTimeout(int $seconds) : void
Parameters
- $seconds : int
-
Timeout in seconds
Tags
findResponse()
Find canned response for method and URL
private
findResponse(string $method, string $url) : IHttpResponse|null
Parameters
- $method : string
-
HTTP method
- $url : string
-
URL
Return values
IHttpResponse|null —Response or null if not found
recordAndRespond()
Record request and return canned response
private
recordAndRespond(string $method, string $url, array<string|int, mixed> $data, array<string|int, mixed> $headers) : IHttpResponse
Parameters
- $method : string
-
HTTP method
- $url : string
-
URL
- $data : array<string|int, mixed>
-
Request data
- $headers : array<string|int, mixed>
-
Request headers
Return values
IHttpResponse —Response object