Skip to content

cosmorogers/http-mock

 
 

Repository files navigation

HTTP Mock for PHP

Gitter Build Status Dependency Status Average time to resolve an issue Percentage of issues still open

Mock HTTP requests on the server side in your PHP unit tests.

HTTP Mock for PHP mocks the server side of an HTTP request to allow integration testing with the HTTP side. It uses PHP’s builtin web server to start a second process that handles the mocking. The server allows registering request matcher and responses from the client side.

BIG FAT WARNING: software like this is inherently insecure. Only use in trusted, controlled environments.

The client side API

Using HTTP mock with PHPUnit

class ExampleTest extends PHPUnit_Framework_TestCase
{
    use \InterNations\Component\HttpMock\PHPUnit\HttpMockTrait;

    public static function setUpBeforeClass()
    {
        static::setUpHttpMockBeforeClass('8082', 'localhost');
    }

    public static function tearDownAfterClass()
    {
        static::tearDownHttpMockAfterClass();
    }

    public function setUp()
    {
        $this->setUpHttpMock();
    }

    public function tearDown()
    {
        $this->tearDownHttpMock();
    }

    public function testSimpleRequest()
    {
        $this->http->mock
            ->when()
                ->methodIs('GET')
                ->pathIs('/foo')
            ->then()
                ->body('mocked body')
            ->end();
        $this->http->setUp();

        $this->assertSame('mocked body', file_get_contents('http://localhost:8082/foo'));
    }

    public function testAccessingRecordedRequests()
    {
        $this->http->mock
            ->when()
                ->methodIs('POST')
                ->pathIs('/foo')
            ->then()
                ->body('mocked body')
            ->end();
        $this->http->setUp();

        $this->assertSame('mocked body', $this->http->client->post('http://localhost:8082/foo')->send()->getBody(true));

        $this->assertSame('POST', $this->http->requests->latest()->getMethod());
        $this->assertSame('/foo', $this->http->requests->latest()->getPath());
    }
}

The server

Setting up expectation for request recording

POST /_expectation
{
    response (required): serialized Symfony response
    matcher (optional): serialized list of closures
    limiter (optional): serialized closure that limits the validity of the expectation
}

Accessing latest recorded request

GET /_request/latest
Content-Type: text/plain

RECORDED REQUEST

Accessing recorded request by index

GET /_request/{{index}}
Content-Type: text/plain

RECORDED REQUEST

Shift recorded request

GET /_request/shift
Content-Type: text/plain

RECORDED REQUEST

Pop recorded request

GET /_request/pop
Content-Type: text/plain

RECORDED REQUEST

Deleting expectations

DELETE /_expectation

Deleting recorded requests

DELETE /_request

Deleting everything

DELETE /_all

Introspection

GET /_me

About

Mock HTTP requests on the server side in your PHP unit tests

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%