draw/http-tester

0.4.0 2018-12-09 00:55 UTC

This package is auto-updated.

Last update: 2024-04-06 01:51:06 UTC


README

image

This library is meant to be a testing framework for http call. It is framework agnostic. By default it does a curl call to the specified url but you can use/create a adapter for the framework you are using.

The library can be install via Composer/Packagist.

In that example we are trying to have a browser flow so the usage of phpunit annotation @depends and @test make it more readable.

Here is a quick example of how to use it in a PHPUnit TestCase:

<?php

namespace Your\Project\Name;

use PHPUnit\Framework\TestCase;
use Draw\HttpTester\HttpTesterTrait;

class SimpleTest extends TestCase
{
    use HttpTesterTrait

    /**
     * @test
     */
    public function notAuthorizeProfileAccess()
    {
        static::$client->get('http://your.domain.com/api/me')
            ->assertStatus(403);
    }

    /**
     * @test
     * @depends notAuthorizeProfileAccess
     */
    public function login()
    {
        $testResponse = static::$client->post(
            'http://your.domain.com/api/tokens',
            json_encode([
                'username' => 'my-username',
                'password' => 'my-password'
            ])
        );

        $content = $testResponse
          ->assertSuccessful()
          ->assertCookie('session') // We are not debating the usage of cookie here ;)
          ->getResponseBodyContents();

        // Continue with the test of you content
        $this->assertJson($content);
    }

    /**
     * @test
     * @depends login
     */
    public function getMyProfile()
    {
        // The same client is during all test. Cookies are sent automatically between request
        $testResponse = static::$client->get('http://your.domain.com/api/me')

        $content = $testResponse
          ->assertSuccessful()
          ->getResponseBodyContents();

        // Continue with the test of you content
        $this->assertJson($content);
    }
}

If you need to use it in another context and can still relay on PHPUnit Assertion you can simply create your the client manually and use it:

<?php

use Draw\HttpTester\Client;

$client = new Client();

$client->post(
    'http://your.domain.com/api/tokens',
    json_encode([
        'username' => 'my-username',
        'password' => 'my-password'
    ])
);

By default the client will use the DrawHttpTesterCurlRequestExecutioner but you can make your own by implementing the DrawHttpTesterRequestExecutionerInterface.

## Currently Supported Request Executioner

=========== ========================================================== ================ Executioner Class Package =========== ========================================================== ================ Curl DrawHttpTesterCurlRequestExecutioner draw/http-tester Laravel 4.2 DrawHttpTesterBridgeLaravel4Laravel4RequestExecutioner draw/http-tester

** Not available yet ** There is a lot more features available, just Read the Docs!