Mock Web Server and Router
, (*1)
Installation
PHP 7.0
Install via composer, (*2)
composer require edmondscommerce/mock-server:~1 --dev, (*3)
PHP 7.1+
Install via composer, (*4)
composer require edmondscommerce/mock-server:~2 --dev, (*5)
Usage
To start the web server, you need to instantiate the \EdmondsCommerce\MockServer\MockServer and call startServer, (*6)
<?php
$mockServer=\EdmondsCommerce\MockServer\Factory::getMockServer();
$mockServer->startServer();
Configuration
When using the Factory, the configuration for the MockServer is pulled from MockServerConfig which in turn checks for values in the $_SERVER superglobal - generally populated with any thing that has been exported from your environment., (*7)
The default values are fairly sensible though. Based upon the project root, the default configuration expects you to have a tests folder which in turn contains a MockServer folder. Inside the MockServer folder we expect a router.php file and a htdocs folder which contains static assets to be served directly., (*8)
Note - this is exactly as it has been configured in this library., (*9)
Router
An example of a basic router set up can be found in router.php, (*10)
The router file should contain an instance of the \EdmondsCommerce\MockServer\StaticRouter which is a wrapper around
Symfony's router class. The file should load the Composer autoloader and create the static router before registering routes for different URIs., (*11)
The router supports static file routes, callback routes and text routes., (*12)
Note - there are helpful includes routerTop.php and routerBottom.php which handle some basic boilerplate for you., (*13)
Template
Here is a template router for you to start with:, (*14)
<?php declare(strict_types=1);
require __DIR__.'/../../vendor/edmondscommerce/mock-server/src/include/routerTop.php';
$router = \EdmondsCommerce\MockServer\Factory::getStaticRouter();
//Add your routes here
/**
* IMPORTANT - you have to `return` the required routerBottom
*/
return require __DIR__.'/../../vendor/edmondscommerce/mock-server/src/include/routerBottom.php';
Starting the server manually
To start the server manually, you can simply use start-mock-server which will start a backgrounded mock server. If you want it in the foreground, simply pass "foreground" as an argument, (*15)
./bin/start-mock-server foreground
If you want the server to listen on a specific IP address, you can do this by exporting a config variable:, (*16)
export MockServer_Ip="0.0.0.0"
Router Types
Static Files (css/js/html/etc...)
Static files that are located in the htdocs folder will be served without any further configuration, (*17)
For the full list of support file types, see: StaticRouter::STATIC_EXTENSIONS_SUPPORTED, (*18)
Callback
The callback router sets a closure which will be passed the request object and must return a response object., (*19)
See this test for an exmaple of a callback., (*20)
File Download
The download router will return a file as a download. Internally it sets a callback that then returns a BinaryFileResponse object, (*21)
Static Text
Second param of addRoute($uri, $response) is the text that will be returned after visiting specified uri., (*22)
See this test for an example of text route., (*23)
Static
Second param of addStaticRuote($uri, $response) is the the file content that will be returned after visiting specified uri., (*24)
Do not use this to return files that are in the htdocs folder, it's pointless. This is largely being kept for legacy reasons., (*25)