Fakeserver
in package
A way to emulate a web server from tests or the command line.
This is pretty minimalistic, but should work to help tests access web pages. I'm planning on adding some additional features down the road.
Table of Contents
- $is_cli : bool
- Are we running under the CLI SAPI?
- $is_cli_server : bool
- Are we running under the standalone web server?
- __construct() : mixed
- Build a Fakeserver plugin instance.
- addHeader() : mixed
- Add an HTTP header.
- addHeaders() : mixed
- Add a bunch of headers.
- addUpload() : mixed
- Add a file to the $_FILES superglobal.
- loadPostJson() : mixed
- Load $_POST data from a JSON file.
- loadUploadsJson() : mixed
- Load a JSON file that will add a bunch of uploads at once.
- parseArgv() : mixed
- Parse the contents of $_SERVER['argv'] (command line arguments).
- parseGet() : mixed
- Parse arguments into the $_GET superglobal.
- setMethod() : mixed
- Set the request method.
- setPathInfo() : mixed
- Set the Path Info
- setProto() : mixed
- Set the server protocol.
- updateRequest() : mixed
- Update the $_REQUEST from $_GET and $_POST (in that order).
- uriHasExts() : mixed
- Check to see if the URI ends in a certain extension.
- uriMatches() : mixed
- Check to see if the REQUEST_URI matches a regex.
- useGet() : mixed
- Use GET if a request method has not been set yet.
- usePost() : mixed
- Use POST if a request method has not been set yet.
Properties
$is_cli read-only
Are we running under the CLI SAPI?
public
bool
$is_cli
Will be auto-detected during construction.
$is_cli_server read-only
Are we running under the standalone web server?
public
bool
$is_cli_server
Will be auto-detected during construction.
Methods
__construct()
Build a Fakeserver plugin instance.
public
__construct([array<string|int, mixed> $opts = [] ]) : mixed
Parameters
- $opts : array<string|int, mixed> = []
-
(Optional) Named options for this plugin:
'method' If specified, we call setMethod() with this value. 'proto' If specified, we call setProto() with this value. 'parse' If true, we call parseArgv(). If not specified, the default is the same as $this->is_cli.
Return values
mixed —addHeader()
Add an HTTP header.
public
addHeader(string $name, string $value) : mixed
Parameters
- $name : string
-
An HTTP header name.
This will be converted to uppercase, dashes replaced by underscores, and a prefix of 'HTTP_' added. So for instance, a header name of 'Accept-Language' will become 'HTTP_ACCEPT_LANGUAGE'.
- $value : string
-
The value to set the header to.
Return values
mixed —addHeaders()
Add a bunch of headers.
public
addHeaders(mixed $headers) : mixed
Parameters
- $headers : mixed
-
The headers we want to set (see below).
Header values:
May be a string, in which case it's a full set of headers separated by newlines. May be an array of strings, in which case each string is a header-string (see below). May be an array of arrays, in which case each child array must have two children, the first is the name of the header (without a colon) and the second is the value to set the header.
Header strings must be in the format:
"Header-Name: header value here"
The header name will be split from the header values by the first colon character found in the string.
Current limitations, no arrays or multiple value headers are currently supported. If a later header has the same name as an earlier one, it will overwrite the previous one.
Return values
mixed —addUpload()
Add a file to the $_FILES superglobal.
public
addUpload(string $name, string $file[, array<string|int, mixed> $filespec = [] ]) : mixed
Parameters
- $name : string
-
The name to add. Array style names are supported.
Array style names come in two styles:
"myfile[]" A flat array called "myfile". "myfile[foo]" An associative array called "myfile" with a child property of "foo" inside it.
- $file : string
-
The file we are adding (must exist.)
- $filespec : array<string|int, mixed> = []
-
(Optional) May contain the following properties:
'name' The name of the file (defaults to basename($file)). 'type' The MIME type of the file (defaults to mime_content_type($file)). 'error' An upload error code (defaults to UPLOAD_ERR_OK). 'size' The size of the file (defaults to filesize($file)).
Return values
mixed —loadPostJson()
Load $_POST data from a JSON file.
public
loadPostJson(string $file[, bool $overwrite = false ]) : mixed
Parameters
- $file : string
-
The JSON file to load into $_POST.
- $overwrite : bool = false
-
(false) Overwrite existing $_POST keys?
Return values
mixed —loadUploadsJson()
Load a JSON file that will add a bunch of uploads at once.
public
loadUploadsJson(mixed $config) : mixed
Parameters
- $config : mixed
-
Either an array representing the JSON, or a string representing the path to the JSON file.
The format of the JSON should be:
[ { "name": "myupload1", // The upload name. "path": "./path/to/file.xml", // The path to the file. "spec": {"type":"application/xml"} // Any $filespec opts here. } // More files here. ]
Or if unique names are in use (no "foo[]" type names), you could do:
{ "myimage": // The upload name. { "path": "./path/to/file.jpg", // The path to the file. "spec": {"name":"image.jpg"}, // Any $filespec opts here. } }
For each upload defined, this will call addUpload().
Return values
mixed —parseArgv()
Parse the contents of $_SERVER['argv'] (command line arguments).
public
parseArgv() : mixed
The first element (name of script) is ignored. Then we look for:
"-p"
If an argument starts with a slash, and no PATH_INFO has been set, we will call setPathInfo($arg);
Any other arguments will be passed to parseGet().
Return values
mixed —parseGet()
Parse arguments into the $_GET superglobal.
public
parseGet(mixed $args) : mixed
Parameters
- $args : mixed
-
Either a string, or an array. If it's an array, it will be joined with '&' symbol. If it's a string, we will trim a leading '?' from it.
We then pass the $args to the PHP parse_str() function.
If $_SERVER['QUERY_STRING'] as not been set, it will be set as well.
Return values
mixed —setMethod()
Set the request method.
public
setMethod(mixed $method) : mixed
Parameters
- $method : mixed
Return values
mixed —setPathInfo()
Set the Path Info
public
setPathInfo(mixed $pathinfo) : mixed
Parameters
- $pathinfo : mixed
Return values
mixed —setProto()
Set the server protocol.
public
setProto(mixed $proto) : mixed
Parameters
- $proto : mixed
-
If a string, it's the protocol spec. If it's anything else, and 'SERVER_PROTOCOL' is not yet set, it will be set to 'HTTP/1.0'.
Return values
mixed —updateRequest()
Update the $_REQUEST from $_GET and $_POST (in that order).
public
updateRequest() : mixed
Return values
mixed —uriHasExts()
Check to see if the URI ends in a certain extension.
public
uriHasExts([mixed $exts = null ]) : mixed
This is a quick wrapper for uriMatches() that simply checks for specific file extensions. Useful if you are using the 'cli-server' SAPI.
Parameters
- $exts : mixed = null
-
A list of extensions to check for. May be a pipe separated string, or an array of file extensions. No leading dot. Default: 'png|jpg|css|js|php'
Return values
mixed —See uriMatches() for return values.
uriMatches()
Check to see if the REQUEST_URI matches a regex.
public
uriMatches(string $regex) : mixed
Useful for the 'cli-server' SAPI, as we can do:
if ($fakeserver->uriMatches('/\.(?:png|jpg|css|js)$/'))
{ // It's a static file, let the PHP web server handle it.
return false;
}
else
{ // Show our content.
}
Parameters
- $regex : string
-
The regular expression we are testing against.
Return values
mixed —If the regular expression matches, this will be an array with the matched values. If the regex does not match this will be boolean false.
useGet()
Use GET if a request method has not been set yet.
public
useGet() : mixed
Return values
mixed —usePost()
Use POST if a request method has not been set yet.
public
usePost() : mixed