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
- Host: GitHub
- URL: https://github.com/creativestyle/app-http-server-mock
- Owner: creativestyle
- Created: 2018-11-22T21:01:54.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-11-22T21:25:36.000Z (over 7 years ago)
- Last Synced: 2025-11-27T17:15:03.884Z (8 months ago)
- Language: PHP
- Size: 5.86 KB
- Stars: 2
- Watchers: 5
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README

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.