Download the PHP package edmondscommerce/mock-server without Composer
On this page you can find all versions of the php package edmondscommerce/mock-server. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download edmondscommerce/mock-server
More information about edmondscommerce/mock-server
Files in edmondscommerce/mock-server
Package mock-server
Short Description Mock HTTP and PHP Server for testing purposes
License MIT
Informations about the package mock-server
Mock Web Server and Router
By Edmonds Commerce
Installation
PHP 7.0
Install via composer
composer require edmondscommerce/mock-server:~1 --dev
PHP 7.1+
Install via composer
composer require edmondscommerce/mock-server:~2 --dev
Usage
To start the web server, you need to instantiate the \EdmondsCommerce\MockServer\MockServer
and call 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.
The default values are fairly sensible though. Based upon the project root, the default configuration expects you to have 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.
Note - this is exactly as it has been configured in this library.
Router
An example of a basic router set up can be found in router.php
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.
The router supports static file routes, callback routes and text routes.
Note - there are helpful includes routerBottom.php which handle some basic boilerplate for you.
Template
Here is a template router for you to start with:
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
If you want the server to listen on a specific IP address, you can do this by exporting a config variable:
Router Types
Static Files (css/js/html/etc...)
Static files that are located in the htdocs folder will be served without any further configuration
For the full list of support file types, see: StaticRouter::STATIC_EXTENSIONS_SUPPORTED
Callback
The callback router sets a closure which will be passed the request object and must return a response object.
See this test for an exmaple of a callback.
File Download
The download router will return a file as a download. Internally it sets a callback that then returns a BinaryFileResponse object
Static Text
Second param of addRoute($uri, $response)
is the text that will be returned after visiting specified uri.
See this test for an example of text route.
Static
Second param of addStaticRuote($uri, $response)
is the the file content that will be returned after visiting specified uri.
Do not use this to return files that are in the htdocs folder, it's pointless. This is largely being kept for legacy reasons.
All versions of mock-server with dependencies
ext-fileinfo Version *
symfony/http-foundation Version ~4
symfony/routing Version ~4