deozza/philarmony-api-tester-bundle

A bundle to test Symfony API

Installs: 113

Dependents: 1

Suggesters: 1

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:symfony-bundle

3.2.0 2019-09-27 13:49 UTC

This package is auto-updated.

Last update: 2024-03-28 01:02:16 UTC


README

php mysql sqlite symfony Stable License: MIT

Table of contents

About

PhilarmonyApiTester is a bundle made to test your API. It ensures the quality and the sturdiness of your application via unit and scenario testing.

Installation

You can install using composer, assuming it is already installed globally :

composer require deozza/philarmony-api-tester-bundle

Database preparation

You need to create a database specifically for your tests. To ensure the tests are executed with the same environment and database, this one will be reset at the end of each test.

We recommend the use of doctrine/doctrine-fxtures-bundle for the creation of a test database.

The database you use for the tests could be in MySQL or SQLITE3.

  • In the case you are using a MySQL database, you need to export it as a file, name it demo.sql { and store it as a file in the /var/data/db_test folder.
  • In the case you are using a SQLITE3 database, you need to name it demo.sqlite { and store it as a file in the /var/data/db_test folder.

For performance reasons, we recommend using a SQLITE3 database for your tests.

How to use

Creating a ControllerTest

To tests a feature of your application, you need to create a folder in the test folder. Inside it, create a ControllerTest for each Controller related to that feature. The ControllerTest must match the following example:

<?php
namespace App\Tests\FooFeature;

use Deozza\PhilarmonyApiTester\Service\TestAsserter;

class FooControllerTest extends TestAsserter
{
    const TEST_DATABASE_PATH = __DIR__."/path/to/db.sql";
  public function setUp()
  {
      parent::setTestDatabasePath(self::TEST_DATABASE_PATH);
      parent::setUp();
  }

  /**
  * @dataProvider addDataProvider
  */
  public function testUnit($kind, $test)
  {
    parent::launchTestByKind($kind, $test);
  }

  public function addDataProvider()
  {
      return [];
  }
}

Writing tests

The tests are written as an array following this structure:

["kind" => "unit", "test" => ["method" => "GET", "url" => "/path/to/the/tested/route", "status" => 200, "token" => "auth_token", "in" => "json_payload", "out" => "json_expected_response]]

They are written in the function addDataProvider.

<?php

public function addDataProvider()
  {
      return
      [
        ["kind" => "unit", "test" => ['method'=> 'GET'   , 'url' => 'api/foos'                                    , 'status' => 200, 'out' => 'getAllFoos'] ],
        ["kind" => "unit", "test" => ['method'=> 'POST'  , 'url' => 'api/foos'                                    , 'token' => 'token_user', 'status' => 201, 'in' => 'postValidFoo' , 'out' => 'postedFoo'] ],
        ["kind" => "unit", "test" => ['method'=> 'PATCH' , 'url' => 'api/foo/00400000-0000-5000-a000-000000000000', 'token' => 'token_user', 'status' => 200, 'in' => 'patchValidFoo', 'out' => 'patchedFoo'] ],
        ["kind" => "unit", "test" => ['method'=> 'PUT'   , 'url' => 'api/foo/00400000-0000-5000-a000-000000000000', 'token' => 'token_user', 'status' => 405] ],
        ["kind" => "unit", "test" => ['method'=> 'DELETE', 'url' => 'api/foo/00400000-0000-5000-a000-000000000000', 'token' => 'token_user', 'status' => 204] ],
      ];
  }
Key Description Facultative
kind The kind of test you are executing No
test The content of the test No
test.method The method of the request you are sending No
test.url The route you are testing No
test.token The authorization token used for the test Yes
test.status The expected http status of the response No
test.in The payload sent with the request Yes
test.out The expected response from the request Tes

For now, only GET, POST, PUT, PATCH and DELETE methods are handled by PhilarmonyApiTester

Different kinds of tests

With PhilarmonyApiTester, you are able to test your application by sending requests one by one ("unit" testing) and by sending a group of request ("scenario").

In (payloads)

You are able to test your application by sending a specific payload with the in option and check how it reacts.

Out (expected responses)

You are able to check the response of the requests you have sent with the option out.

Testing with patterns

Sometimes, value sent by your app are hard to test because they are unpredictable. Testing with patterns will allow PhilarmonyApiTester to assert these values are in a specific scope and even manipulate them for future tests.

Folder structure

To test your application, you need to match the following folder structure:

.
├── tests                                   #   The basic Symfony test directory
│   ├── Foo                                 #   Feature you need to test
│   │   │── FooControllerTest.php           #   Controller of the feature you want to test
│   │   │                                       
│   │   ├── Payloads                            
│   │   │   └── ...                             #   File posted to your API
│   │   │   
│   │   └── Responses                           #   All the expected responses coming from the endpoints of your API when testing it
│   │       └── ...
│   └──
└──

Road map

Nothing is in the road map !