donatj/mock-webserver
Lightweight PHP mock web server for tests and local development. Spin up an HTTP server on a random port, enqueue canned responses, inspect received requests, and simulate endpoints reliably. Handy for integration tests without external services.
<?php
namespace donatj\MockWebServer;
class MockWebServer {
public const VND = 'VND.DonatStudios.MockWebServer';
public const LAST_REQUEST_FILE = 'last.request';
public const REQUEST_COUNT_FILE = 'count.request';
public const TMP_ENV = 'MOCK_WEB_SERVER_TMP';
}
function __construct([ int $port = 0 [, string $host = '127.0.0.1']])
TestWebServer constructor.
$port - Network port to run on$host - Listening hostnamefunction start() : void
Start the Web Server on the selected port and host
function isRunning() : bool
Is the Web Server currently running?
function stop() : void
Stop the Web Server
function getServerRoot() : string
Get the HTTP root of the webserver
e.g.: http://127.0.0.1:8123
function getUrlOfResponse(\donatj\MockWebServer\ResponseInterface $response) : string
Get a URL providing the specified response.
function setResponseOfPath(string $path, \donatj\MockWebServer\ResponseInterface $response) : string
Set a specified path to provide a specific response
function setDefaultResponse(\donatj\MockWebServer\ResponseInterface $response) : void
Override the default server response, e.g. Fallback or 404
function getLastRequest() : ?\donatj\MockWebServer\RequestInfo
Get the previous requests associated request data.
function getRequestByOffset(int $offset) : ?\donatj\MockWebServer\RequestInfo
Get request by offset
If offset is non-negative, the request will be the index from the start of the server.
If offset is negative, the request will be that from the end of the requests.
function getHost() : string
Get the host of the server.
function getPort() : int
Get the port the network server is to be ran on.
function __construct(string $body [, array $headers = [] [, int $status = 200]])
Response constructor.
ResponseStack is used to store multiple responses for a request issued by the server in order.
When the stack is empty, the server will return a customizable response defaulting to a 404.
function __construct(\donatj\MockWebServer\ResponseInterface ...$responses)
ResponseStack constructor.
Accepts a variable number of ResponseInterface objects
function getPastEndResponse() : \donatj\MockWebServer\ResponseInterface
Gets the response returned when the stack is exhausted.
function setPastEndResponse(\donatj\MockWebServer\ResponseInterface $pastEndResponse) : void
Set the response to return when the stack is exhausted.
ResponseByMethod is used to vary the response to a request by the called HTTP Method.
<?php
namespace donatj\MockWebServer;
class ResponseByMethod {
public const METHOD_GET = 'GET';
public const METHOD_POST = 'POST';
public const METHOD_PUT = 'PUT';
public const METHOD_PATCH = 'PATCH';
public const METHOD_DELETE = 'DELETE';
public const METHOD_HEAD = 'HEAD';
public const METHOD_OPTIONS = 'OPTIONS';
public const METHOD_TRACE = 'TRACE';
}
function __construct([ array $responses = [] [, ?\donatj\MockWebServer\ResponseInterface $defaultResponse = null]])
MethodResponse constructor.
$responses - A map of responses keyed by their method.$defaultResponse - The fallthrough response to return if a response for a given
method is not found. If this is not defined the server will
return an HTTP 501 error.function setMethodResponse(string $method, \donatj\MockWebServer\ResponseInterface $response) : void
Set the Response for the Given Method
DelayedResponse wraps a response, causing it when called to be delayed by a specified number of microseconds.
This is useful for simulating slow responses and testing timeouts.
function __construct(\donatj\MockWebServer\ResponseInterface $response, int $delay [, ?callable $usleep = null])
$delay - Microseconds to delay the responseThe Built-In Default Response.
Results in an HTTP 200 with a JSON encoded version of the incoming Request
Basic Built-In 404 Response
How can I help you explore Laravel packages today?