• Stars
    star
    131
  • Rank 275,036 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 7 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple mock web server in PHP for unit testing.

Mock Web Server

Latest Stable Version License CI

Simple, easy to use Mock Web Server for PHP unit testing. Gets along simply with PHPUnit and other unit testing frameworks.

Unit testing HTTP requests can be difficult, especially in cases where injecting a request library is difficult or not ideal. This helps greatly simplify the process.

Mock Web Server creates a local Web Server you can make predefined requests against.

Documentation

See: docs/docs.md

Requirements

  • php: >=7.1
  • ext-sockets: *
  • ext-json: *
  • ralouphie/getallheaders: ~2.0 || ~3.0

Installing

Install the latest version with:

composer require --dev 'donatj/mock-webserver'

Examples

Basic Usage

The following example shows the most basic usage. If you do not define a path, the server will simply bounce a JSON body describing the request back to you.

<?php

use donatj\MockWebServer\MockWebServer;

require __DIR__ . '/../vendor/autoload.php';

$server = new MockWebServer;
$server->start();

$url = $server->getServerRoot() . '/endpoint?get=foobar';

echo "Requesting: $url\n\n";
echo file_get_contents($url);

Outputs:

Requesting: http://127.0.0.1:61874/endpoint?get=foobar

{
    "_GET": {
        "get": "foobar"
    },
    "_POST": [],
    "_FILES": [],
    "_COOKIE": [],
    "HEADERS": {
        "Host": "127.0.0.1:61874",
        "Connection": "close"
    },
    "METHOD": "GET",
    "INPUT": "",
    "PARSED_INPUT": [],
    "REQUEST_URI": "\/endpoint?get=foobar",
    "PARSED_REQUEST_URI": {
        "path": "\/endpoint",
        "query": "get=foobar"
    }
}

Simple

<?php

use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Response;

require __DIR__ . '/../vendor/autoload.php';

$server = new MockWebServer;
$server->start();

// We define the server's response to requests of the /definedPath endpoint
$url = $server->setResponseOfPath(
	'/definedPath',
	new Response(
		'This is our http body response',
		[ 'Cache-Control' => 'no-cache' ],
		200
	)
);

echo "Requesting: $url\n\n";

$content = file_get_contents($url);

// $http_response_header is a little known variable magically defined
// in the current scope by file_get_contents with the response headers
echo implode("\n", $http_response_header) . "\n\n";
echo $content . "\n";

Outputs:

Requesting: http://127.0.0.1:61874/definedPath

HTTP/1.1 200 OK
Host: 127.0.0.1:61874
Date: Tue, 31 Aug 2021 19:50:15 GMT
Connection: close
X-Powered-By: PHP/7.3.25
Cache-Control: no-cache
Content-type: text/html; charset=UTF-8

This is our http body response

Change Default Response

<?php

use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Responses\NotFoundResponse;

require __DIR__ . '/../vendor/autoload.php';

$server = new MockWebServer;
$server->start();

// The default response is donatj\MockWebServer\Responses\DefaultResponse
// which returns an HTTP 200 and a descriptive JSON payload.
//
// Change the default response to donatj\MockWebServer\Responses\NotFoundResponse
// to get a standard 404.
//
// Any other response may be specified as default as well.
$server->setDefaultResponse(new NotFoundResponse);

$content = file_get_contents($server->getServerRoot() . '/PageDoesNotExist', false, stream_context_create([
	'http' => [ 'ignore_errors' => true ], // allow reading 404s
]));

// $http_response_header is a little known variable magically defined
// in the current scope by file_get_contents with the response headers
echo implode("\n", $http_response_header) . "\n\n";
echo $content . "\n";

Outputs:

HTTP/1.1 404 Not Found
Host: 127.0.0.1:61874
Date: Tue, 31 Aug 2021 19:50:15 GMT
Connection: close
X-Powered-By: PHP/7.3.25
Content-type: text/html; charset=UTF-8

VND.DonatStudios.MockWebServer: Resource '/PageDoesNotExist' not found!

PHPUnit

<?php

use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Response;

class ExampleTest extends PHPUnit\Framework\TestCase {

	/** @var MockWebServer */
	protected static $server;

	public static function setUpBeforeClass() : void {
		self::$server = new MockWebServer;
		self::$server->start();
	}

	public function testGetParams() : void {
		$result  = file_get_contents(self::$server->getServerRoot() . '/autoEndpoint?foo=bar');
		$decoded = json_decode($result, true);
		$this->assertSame('bar', $decoded['_GET']['foo']);
	}

	public function testGetSetPath() : void {
		// $url = http://127.0.0.1:61874/definedEndPoint
		$url    = self::$server->setResponseOfPath('/definedEndPoint', new Response('foo bar content'));
		$result = file_get_contents($url);
		$this->assertSame('foo bar content', $result);
	}

	public static function tearDownAfterClass() : void {
		// stopping the web server during tear down allows us to reuse the port for later tests
		self::$server->stop();
	}

}

Delayed Response Usage

By default responses will happen instantly. If you're looking to test timeouts, the DelayedResponse response wrapper may be useful.

<?php

use donatj\MockWebServer\DelayedResponse;
use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Response;

require __DIR__ . '/../vendor/autoload.php';

$server = new MockWebServer;
$server->start();

$response = new Response(
	'This is our http body response',
	[ 'Cache-Control' => 'no-cache' ],
	200
);

// Wrap the response in a DelayedResponse object, which will delay the response
$delayedResponse = new DelayedResponse(
	$response,
	100000 // sets a delay of 100000 microseconds (.1 seconds) before returning the response
);

$realtimeUrl = $server->setResponseOfPath('/realtime', $response);
$delayedUrl  = $server->setResponseOfPath('/delayed', $delayedResponse);

echo "Requesting: $realtimeUrl\n\n";

// This request will run as quickly as possible
$start = microtime(true);
file_get_contents($realtimeUrl);
echo "Realtime Request took: " . (microtime(true) - $start) . " seconds\n\n";

echo "Requesting: $delayedUrl\n\n";

// The request will take the delayed time + the time it takes to make and transfer the request
$start = microtime(true);
file_get_contents($delayedUrl);
echo "Delayed Request took: " . (microtime(true) - $start) . " seconds\n\n";

Outputs:

Requesting: http://127.0.0.1:61874/realtime

Realtime Request took: 0.015669107437134 seconds

Requesting: http://127.0.0.1:61874/delayed

Delayed Request took: 0.10729098320007 seconds

Multiple Responses from the Same Endpoint

Response Stack

If you need an ordered set of responses, that can be done using the ResponseStack.

<?php

use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Response;
use donatj\MockWebServer\ResponseStack;

require __DIR__ . '/../vendor/autoload.php';

$server = new MockWebServer;
$server->start();

// We define the servers response to requests of the /definedPath endpoint
$url = $server->setResponseOfPath(
	'/definedPath',
	new ResponseStack(
		new Response("Response One"),
		new Response("Response Two")
	)
);

echo "Requesting: $url\n\n";

$contentOne = file_get_contents($url);
$contentTwo = file_get_contents($url);
// This third request is expected to 404 which will error if errors are not ignored
$contentThree = file_get_contents($url, false, stream_context_create([ 'http' => [ 'ignore_errors' => true ] ]));

// $http_response_header is a little known variable magically defined
// in the current scope by file_get_contents with the response headers
echo $contentOne . "\n";
echo $contentTwo . "\n";
echo $contentThree . "\n";

Outputs:

Requesting: http://127.0.0.1:61874/definedPath

Response One
Response Two
Past the end of the ResponseStack

Response by Method

If you need to vary responses to a single endpoint by method, you can do that using the ResponseByMethod response object.

<?php

use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Response;
use donatj\MockWebServer\ResponseByMethod;

require __DIR__ . '/../vendor/autoload.php';

$server = new MockWebServer;
$server->start();

// Create a response for both a POST and GET request to the same URL

$response = new ResponseByMethod([
	ResponseByMethod::METHOD_GET  => new Response("This is our http GET response"),
	ResponseByMethod::METHOD_POST => new Response("This is our http POST response", [], 201),
]);

$url = $server->setResponseOfPath('/foo/bar', $response);

foreach( [ ResponseByMethod::METHOD_GET, ResponseByMethod::METHOD_POST ] as $method ) {
	echo "$method request to $url:\n";

	$context = stream_context_create([ 'http' => [ 'method' => $method ] ]);
	$content = file_get_contents($url, false, $context);

	echo $content . "\n\n";
}

Outputs:

GET request to http://127.0.0.1:61874/foo/bar:
This is our http GET response

POST request to http://127.0.0.1:61874/foo/bar:
This is our http POST response

More Repositories

1

PhpUserAgent

Lightning Fast, Minimalist PHP User Agent String Parser.
PHP
564
star
2

CsvToMarkdownTable

Simple JavaScript/Node.js CSV to Markdown Table Converter
JavaScript
291
star
3

RewriteRule-Generator

Tool to generate .htaccess RewriteRule's from a tabbed set of data
PHP
128
star
4

Circle-Generator

Tool to generate circles for block building games like Minecraft
TypeScript
109
star
5

SimpleCalendar

A simple PHP calendar rendering class
PHP
81
star
6

CLI-Toolkit

PHP CLI Toolkit
PHP
60
star
7

sqlread

SQL Dump Parser - Query MySQL Dumps Directly without loading them into MySQL
Go
48
star
8

hookah

GitHub Webhooks Made Easy!
Go
32
star
9

mjpeg-php

Toy long-polling motion jpeg example script.
PHP
31
star
10

imgdedup

CLI tool for image duplicate detection
Go
31
star
11

imgboard

Super Fun JavaScript-Free Drawing Board
Go
29
star
12

Flags

A GNU-style PHP command line argument parser inspired by Go's Flag package.
PHP
19
star
13

mpo

JPEG-MPO Decoder / Converter Library and CLI Tool
Go
17
star
14

gojs

Run JavaScript in Go in WASM in the Browser
JavaScript
13
star
15

XLS-XML-Formula-Expander

Parses through SpreadsheetML (Excel 2003 XML) Spreadsheets, and evaluates their formulas.
PHP
10
star
16

force-color.org

Website data for force-color.org
SCSS
10
star
17

BeautifulPHPCLI-talk

Beautiful PHP CLI Talk Code Samples
PHP
9
star
18

Pushover-PHP

Simple Pushover API Wrapper for PHP
PHP
9
star
19

wordwrap

UTF-8 Unicode Safe Golang Byte Size WordWrap
Go
9
star
20

gifopt

Simple Interframe Gif Optimizer
Go
8
star
21

pocketcasts-go

Go connector for Pocket Casts Private API
Go
8
star
22

Boomerang

API Endpoint E2E Testing Application
PHP
7
star
23

tabasco

Chrome Extension to Wrangle Tabs
JavaScript
6
star
24

PHP-JPGMPO-to-Stereo-JPEG

Converts Nintendo 3DS MPO files to side by side JPEG Images
PHP
5
star
25

PHP-Minecraft-Bukkit-Server-Status

PHP
5
star
26

CorpusPHP

Corpus PHP Framework
PHP
5
star
27

PhpIniBuilder

PHP `parse_ini_string` Compatible INI Builder
PHP
4
star
28

printf-parser

A PHP compatible tokenizing printf string parser
PHP
4
star
29

Hidden-File-Toggle-Widget

An OS X widget to toggle display of hidden files.
JavaScript
4
star
30

unic

Like UNIX `sort | uniq` except it's quicker and maintains order. Uses a Cuckoo Filter.
Go
4
star
31

SlenderGrid

Painfully Simple CSS Grid Using Sass
CSS
4
star
32

jqmux

Go HTTP Multiplexer routing on JSON http request bodies via jq queries
Go
3
star
33

tcgif

True Color Gif Generator
Go
3
star
34

mddom

A Basic Markdown "DOM" for building Markdown out of Objects
PHP
3
star
35

hmacsig

HMAC Signature Validation Middleware (e.g. for GitHub Webhooks)
Go
3
star
36

DatePicker

Simple No-Framework No-Nonsense JavaScript Date Picker
JavaScript
3
star
37

Pinch

Super Simple Event Driven PHP IRC Bot
PHP
3
star
38

masq

Masq ain't SQL. A reimplementation of Misstep in Go using a more traditional parse tree
Go
3
star
39

gifdump

Dump the frames of a gif, composed or uncomposed
Go
3
star
40

appsettings

Simple Hierarchical Key/Value Store for Simple Go Runtime App Setting Storage
Go
3
star
41

TickerGraph

Simple JavaScript Real Time Ticker Graph
TypeScript
3
star
42

AlikeColorFinder

Finds Alike Colors in CSS, SCSS, LESS etc.
PHP
3
star
43

pngpal

CLI tool to convert PNGs to palleted PNGs for amazing space savings.
Go
2
star
44

DB-Analyze

PHP Database Analyzer
PHP
2
star
45

JDLightbox

Super Simple No Frills No Framework Lightbox
JavaScript
2
star
46

parsestr

Provides PHP "parse_str" style URL parsing to Golang
Go
2
star
47

ghemoji

Golang tools for working with old style github :emoji:
Go
2
star
48

symlink-check-action

Action to validate all symlinks in a project are valid
Shell
1
star
49

3D-Package-JS

Fun Little JavaScript 3D Engine
HTML
1
star
50

FolderFileOlderThan

Simple VB.Net scanner to scan a folders folders for files max modified dates
Visual Basic
1
star
51

Clipboard-2-Desktop

Tiny windows app, saves clipboard contents to the desktop
Visual Basic
1
star
52

hqxgo

HQX Go
Go
1
star
53

3D-Package

PHP
1
star
54

php-dnf-solver

PHP DNF (Disjunctive Normal Form) Signature Compatibility Solver
PHP
1
star
55

Spacecat

Exciting and New
1
star
56

diskWatcher

Watches disk space, notifies with pushover on problems
PHP
1
star
57

Webarchive-PHP

Webarchive-PHP is a small library for exporting Safari "Webarchive" files.
PHP
1
star
58

Advanced-File-Filter

An Advanced .Net File Filtering Library
Visual Basic
1
star
59

Amazon-FWS

Simple Amazon Fulfillment Web Service Connector
PHP
1
star
60

depr

Tool for archiving old "depr"ecated files/projects
Go
1
star
61

CanvasFuntime

1
star
62

jdzoom

A JavaScript Zoom application
JavaScript
1
star
63

imgavg

A simple go tool to average a collection of images faster than Photoshop.
Go
1
star
64

explainer

Simple naive mysql database profiler
Go
1
star
65

File-Exist-O-Tron

Determines if a list of files exist in a directory or below.
Visual Basic
1
star
66

prefwatch

Watch system perference plists for changes and report
Go
1
star
67

mddoc

A simple, directed, markdown documentation generator for PHP
PHP
1
star