PHP code example of karmabunny / visor

1. Go to this page and download the library: Download karmabunny/visor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

karmabunny / visor example snippets


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);
    }
}

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']);
    }
}

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);
    }
}