karmabunny / visor
A supervisor for PHP-CLI servers
Installs: 543
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 0
Open Issues: 0
pkg:composer/karmabunny/visor
Requires
- karmabunny/interfaces: ^1.0
- karmabunny/router: ^2.7.16
- nyholm/psr7: ^1.8
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.5
README
This is a small utility to wrap the built-in PHP cli-server feature.
There are two three parts to this library:
-
An abstract 'server' instance that will manage the lifecycle of any cli-server compatible script.
-
An 'echo server' implementation that, uh, echos. What you say at it, it says back.
-
A 'mock server' implementation that will reply with defined responses.
Install
Using Composer:
composer require karmabunny/visor
Usage
This is ideal for creating small integration tests with a local application or creating mock servers and testing HTTP libraries.
Server Instance
use karmabunny\visor\Server; use PHPUnit\Framework\TestCase; /** * The application bootstrap is found at: 'index.php'. This must be capable * of accepting cli-server requests. */ class MyServer extends Server { protected function getTargetScript(): string { return __DIR__ . '/index.php'; } } class MyServerTest extends TestCase { public function testThings() { // This create a server at localhost:8080 $server = MyServer::create(); // One can then perform tests against the application. $res = file_get_contents($server->getHostUrl() . '/health'); $this->assertEquals('ok', $res); } }
Echo Server
use karmabunny\visor\EchoServer; use PHPUnit\Framework\TestCase; class ClientTest extends TestCase { public function testThings() { // This creates an echo server at localhost:8080 $server = EchoServer::create(); // Imagine this to be some kind of client that hits a remote // server of sorts. $res = file_get_contents($server->getHostUrl() . '/hello?test=123'); // Not only is 'res' a JSON body of the payload, the payload is // also accessible from the the server instance. $payload = $server->getLatestPayload(); $this->assertEquals('/hello', $payload['path']); $this->assertEquals(['test' => '123'], $payload['query']); } }
Mock Server
use karmabunny\visor\MockServer; use PHPUnit\Framework\TestCase; class FakeTest extends TestCase { public function testThings() { // This creates a mock server at localhost:8080 $server = MockServer::create(); $server->setMock('/mock-this', [], 'a fake response'); $res = file_get_contents($server->getHostUrl() . '/mock-this'); $payload = $server->getLatestPayload(); $this->assertEquals('/mock-this', $payload['path']); $this->assertEquals('a fake response', $res); } }
Config
| name | - | default |
|---|---|---|
| host | a binding address | localhost |
| port | HTTP port number | 8080 |
| wait | pause until the server is ready (milliseconds) | 100 |
| path | working directory of the server | - |
By default the log file path is randomised in a temporary system directory.
Log files
The server emit a log file to aid testing and debugging.
This includes logs from the server process, the application, and visor itself.
Server that extend the base class can use Server::log(). Applications can use the native error_log().
Mock + Echo Server
The included implementations will log additional data also.
latest.jsonis the request payload stored for introspection. Used bygetLastPayload().mocks.jsonis a store of response objects for the mock server.
Echo (+ Mock) payloads
Both Mock and Echo servers store the request object in a specific format.
Note that the body is unchanged, if you've sent a JSON or URL payload this will be 'as is' in it's encoded string form.
path- the request path, without the query stringquery- an key-value array, fromparse_str()method- always uppercaseheaders- key-value pairs, keys are lowercasebody- string body, fromphp://input
The JSON-encoded log file looks like this:
{
"path": "/hello-world.json",
"query": {
"rando1": "7bb1166f0cf451cc3eb4cbb977ad932f674aac6c"
},
"method": "POST",
"headers": {
"host": "localhost:8080",
"connection": "close",
"content-length": "53",
"content-type": "application/json"
},
"body": "{\"rando2\":\"267f3bf70d8939c2c7e77d1f8ea164e1df071bba\"}"
}