Giter Site home page Giter Site logo

app-http-server-mock's Introduction

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.

app-http-server-mock's People

Contributors

pinkeen avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

app-http-server-mock's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.