draw/tester

Library base on phpunit to test your data with a fluent interface.

v0.7.24 2021-10-12 20:10 UTC

README

Php Data Tester

https://travis-ci.org/mpoiriert/php-data-tester.svg?branch=master

This library is a wrapper around PHPUnit Assert class to be able to use a fluent interface on the data you want to test.

The library can be install via Composer/Packagist.

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\DataTester\Tester;

class SimpleTest extends TestCase
{
    public function test()
    {
        $data = [
          'key1' => 'value1',
          'key2' => (object)['toto' => 'value']
        ];

        $tester = new Tester($data);
        $tester->assertInternalType('array')
            ->assertCount(2)
            ->path('[key1]')->assertSame('value1');
        $tester->path('[key2].toto')->assertSame('value');
}

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

Php Http Tester

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

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!