creativestyle/app-http-server-mock

An minimal app http server based on PHP cli-server for use in tests

Maintainers

👁 creativestyle

Package info

github.com/creativestyle/app-http-server-mock

Homepage

pkg:composer/creativestyle/app-http-server-mock

Statistics

Installs: 2 384

Dependents: 3

Suggesters: 0

Stars: 2

Open Issues: 1

v1.0 2018-11-22 21:22 UTC

Requires

  • php: >=7.1.0

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT a54ec9dadfaee1ec4d7b83f01072dc9a8a1f0444

  • Filip Sobalski <filip.sobalski.woop@creativestyle.pl>

httpphpunitserverappserverwebserverunit-testing

This package is auto-updated.

Last update: 2026-06-23 14:44:24 UTC


README

👁 Travis Build Status

PHP HTTP App Server for use in tests

This library allows to query HTTP endpoints in your unit/integration tests without spinning up a whole webserver.

It uses PHP's built-in web-server underneath, but it's completely opaque and you don't have to worry about anything.

Usage

WARNING This has to be installed as a composer dependency - it may not work if you just drop it in.

composer require --dev creativestyle/app-http-server-mock

Now you need to subclass Creativestyle\AppHttpServerMock\Server and implement the only abstract method registerRequestHandlers:

<?php

namespace YourNamespace;

use Creativestyle\AppHttpServerMock\Server;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class YourTestServer extends Server
{
 protected function registerRequestHandlers()
 {
 $this->registerRequestHandler('GET', '/', function(Request $request) {
 return new Response('Hello');
 });
 
 $this->registerRequestHandler(['PUT', 'POST'], '/number/(?<num>\d+)/test', function(Request $request, array $args) {
 return [
 'arrays' => [
 'are',
 'transformed',
 'into',
 'json' => ['how' => 'automatically']
 ],
 'your_method' => $request->getMethod(),
 'your_number' => $args['num']
 ];
 });
 }
}

And now you can just use your server in the tests:

<?php

namespace YouNamespace\Tests;

use YourNamespace\YourTestServer;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use PHPUnit\Framework\TestCase;

class YourTest extends TestCase
{
 /**
 * @var YourTestServer
 */
 private static $testServer;

 public static function setUpBeforeClass()
 {
 self::$testServer = new YourTestServer();
 self::$testServer->start();
 }

 public static function tearDownAfterClass()
 {
 self::$testServer->stop();
 }

 private function getClient()
 {
 return new Client([
 'base_uri' => self::$testServer->getBaseUrl(),
 'http_errors' => false
 ]);
 }

 public function testSomething()
 {
 $response = $this->getClient()->get('/');

 $this->assertEquals(200, $response->getStatusCode());
 $this->assertEquals('Hello', $response->getBody()->getContents());
 }
}

Of course you could use the server in the setUp() and tearDown() methods but it's non-optimal from the perf. perspective as the server would be started/stopped before/after each test.

To get more usage examples and see what's possible see the /tests subdirectory of this package - it should be all self-explanatory.