An open API service indexing awesome lists of open source software.

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

An minimal app http server based on PHP cli-server for use in tests
https://github.com/creativestyle/app-http-server-mock

Last synced: 5 months ago
JSON representation

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

Awesome Lists containing this project

README

          

![Travis Build Status](https://travis-ci.org/creativestyle/app-http-server-mock.svg?branch=master)

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
registerRequestHandler('GET', '/', function(Request $request) {
return new Response('Hello');
});

$this->registerRequestHandler(['PUT', 'POST'], '/number/(?\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
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.