2017 © Pedro Peláez
 

library httptest

image

jcchavezs/httptest

  • Saturday, June 2, 2018
  • by jcchavezs
  • Repository
  • 1 Watchers
  • 6 Stars
  • 496 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 7 Versions
  • 79 % Grown

The README.md

Http Test

Build Status CircleCI Latest Stable Version Minimum PHP Version Total Downloads License, (*1)

Library for for HTTP integration tests., (*2)

HttpTest is strongly inspired on the httptest go library, (*3)

Description

When testing functions that include HTTP calls, developers often create a wrapper class around cURL functions and mock that class in order to unit test it. This technique unit tests the class but it is also important to test the actual HTTP call which requires an HTTP server listening to those calls. This library provides such a server and allow developers to do assertions both in the client and server side., (*4)

Installation

composer require --dev jcchavezs/httptest

Usage

Test a cURL HTTP request:, (*5)

<?php

namespace HttpTest\Tests\Integration;

use HttpTest\HttpTestServer;
use PHPUnit_Framework_TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

final class TestServerTest extends PHPUnit_Framework_TestCase
{
    const TEST_BODY = 'test_body';
    const TEST_STATUS_CODE = 202;

    public function testHttpSuccess()
    {
        $t = $this;

        $server = HttpTestServer::create(
            function (RequestInterface $request, ResponseInterface &$response) use ($t) {
                /* Assert the HTTP call includes the expected values */
                $t->assertEquals('POST', $request->getMethod());
                $t->assertEquals('application/json', $request->getHeader('Content-Type')[0]);
                $t->assertEquals(self::TEST_BODY, (string) $request->getBody());
                $response = $response->withStatus(self::TEST_STATUS_CODE);
            }
        );

        $server->start();

        $handle = curl_init($server->getUrl());
        curl_setopt($handle, CURLOPT_POST, 1);
        curl_setopt($handle, CURLOPT_POSTFIELDS, self::TEST_BODY);
        curl_setopt($handle, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json',
            'Content-Length: ' . strlen(self::TEST_BODY),
        ]);

        if (curl_exec($handle) === true) {
            $statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
            curl_close($handle);

            // Assert client behaviour based on the server response
            $this->assertEquals(self::TEST_STATUS_CODE, $statusCode);
        } else {
            // Stop the server before as `$this->fail(...)` throws an exception
            // In a try/catch block, this should be in the finally block
            $server->stop();

            $this->fail(curl_error($handle));
        }

        $server->stop();
    }
}

Important: httptest-php uses pcntl_fork to run the server in a separated thread. Consider this when writing the test and more important, stop the server as soon as you are done with calls because objects are copied from the parent process to the child process and that could end up in having in the assertions having actual value multiplied by 2 when counting calls to external resources (e.g. writing log entries to a file can have double of expected lines if server is stopped after the write)., (*6)

Tests

composer test

The Versions

02/06 2018

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

 

The Development Requires

31/05 2018

0.2.1

0.2.1.0

  Sources   Download

MIT

The Requires

 

The Development Requires

31/05 2018

0.2

0.2.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

14/05 2018

dev-hides_fork_implementation

dev-hides_fork_implementation

  Sources   Download

MIT

The Requires

 

The Development Requires

10/05 2018

dev-adds_7_2_to_travis_matrix

dev-adds_7_2_to_travis_matrix

  Sources   Download

MIT

The Requires

 

The Development Requires

09/01 2018

0.1-beta2

0.1.0.0-beta2

  Sources   Download

MIT

The Requires

 

The Development Requires

06/01 2018