Giter Club home page Giter Club logo

php-http-mock's Introduction

php-http-mock

ci coverage

A simple solution to mock external APIs using local php server without binary deps

Designed for PHPUnit, but you can use it with anything.

How it works

Whenever you call ApiMocker::start, a new local php server is launched (via php -S).

The server reads its route configuration (URLs, methods, and corresponding responses) from a temporary file. This file is re-read on every request, so you can reconfigure routes and responses after the server is started.

The file is written to and created by the php-http-mock package automatically and passed to the server via an env variable.

Example

<?php

declare(strict_types=1);

namespace Test\Integration\App\HttpMock\Client;

use App\HttpMock\Client\ApiMocker;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final class ApiMockerTest extends TestCase
{
    private ApiMocker $apiMocker;
    private HttpClientInterface $httpClient;

    protected function setUp(): void
    {
        $this->apiMocker = ApiMocker::create();
        $this->apiMocker->start();

        $this->httpClient = HttpClient::createForBaseUri($this->apiMocker->getBaseUri());
    }

    protected function tearDown(): void
    {
        $this->apiMocker->stop();
    }

    /**
     * @test
     */
    public function allowsDifferentResponsesOnTheSameUri(): void
    {
        $this->apiMocker->routeWillReturn('/expected-uri', responseCode: 400, responseBody: 'get response');
        $this->apiMocker->routeWillReturn('/expected-uri', 'POST', 500, 'post response');

        $getResponse = $this->httpClient->request('GET', '/expected-uri');
        $this->assertSame(400, $getResponse->getStatusCode());
        $this->assertSame('get response', $getResponse->getContent(false));

        $postResponse = $this->httpClient->request('POST', '/expected-uri');
        $this->assertSame(500, $postResponse->getStatusCode());
        $this->assertSame('post response', $postResponse->getContent(false));
    }

    /**
     * @test
     */
    public function returnsRequestsMadeToApiServer(): void
    {
        $this->apiMocker->routeWillReturn('/request?to=keep', 'POST');
        $this->httpClient->request('POST', '/request?to=keep', ['body' => 'expected-body']);

        $request = $this->apiMocker->lastRequestOn('/request?to=keep', 'POST');

        $this->assertSame('expected-body', $request->getContent());
    }
}

Commands for development

composer test - self-evident

composer cov - generates coverage html & xml

coverage ci - runs php-cs-fixer, psalm and tests

coverage fixcs - fixes code style automatically

php-http-mock's People

Contributors

elnoro avatar

Watchers

 avatar

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.