2017 © Pedro Peláez
 

symfony-bundle rest-api-test

This symfony bundle is made to simplify testing of your rest API. It can be useful in running of functional data fixtures based tests and acceptance (Behat) tests. It simplifies automatic tests development by provided test cases classes and commands

image

litvinab/rest-api-test

This symfony bundle is made to simplify testing of your rest API. It can be useful in running of functional data fixtures based tests and acceptance (Behat) tests. It simplifies automatic tests development by provided test cases classes and commands

  • Monday, July 17, 2017
  • by litvinab
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,277 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 47 % Grown

The README.md

RestAPITest

This symfony bundle is made to simplify testing of your rest API., (*1)

It can be useful for functional tests based on data fixtures and for acceptance tests., (*2)

Also current bundle provides some aliases of doctrine commands. It was made to prevent copy-paste of the same commands in your CI (for example, in Makefile, in setup of your tests, etc.), (*3)

Contents, (*4)

Installation

Install via Composer:, (*5)

composer require litvinab/rest-api-test

Enable Bundle

In your app/AppKernel.php file, add the following to your list of dev bundles:, (*6)

<?php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        // ...

        $bundles = [
            ...
            new Litvinab\Bundle\RestApiTestBundle\RestApiTestBundle()
        ];

        // ...
    }
}

Commands

  • bin/console db:create - alias of doctrine:database:create command;, (*7)

  • bin/console db:update-schema - alias of doctrine:schema:update --force command;, (*8)

  • bin/console db:load-fixtures - alias of doctrine:fixtures:load --no-interaction command;, (*9)

  • bin/console db:clear-cache - clean database metadata, query, result cache at the same time;, (*10)

Functional Data Fixtures Based Tests

Configuration

1. Setup parameters_test.yml with test database name and connection details;, (*11)

2. Create data fixtures and configure them right;, (*12)

How To Use

Extend test cases mentioned below and call method to reload test data fixtures before each test or before specified test., (*13)

Call db:create, db:create-schema before using to create test database and create database schema;, (*14)

FixturesWebTestCase

It can be useful in repositories tests to have the same database state at the beggining of each test., (*15)

Methods:, (*16)

getContainer() 

getEntityManager()  

getRepository($name) 

RestFixturesWebTestCase

Methods:, (*17)

getJson($endpoint, $headers = array())

postJson($endpoint, $json, array $headers = array())

putJson($endpoint, $json, array $headers = array())

deleteJson($endpoint, array $headers = array())

reloadDb() - reload data fixtures by prompt without db recreation and schema update

Asserts:, (*18)

assertResponse(Response $response, $expectedStatusCode, $expectedContent)

assertJsonResponse(Response $response, $expectedStatusCode, $expectedJson)

assertResponseCode(Response $response, $expectedStatusCode)

Examples

FixturesWebTestCase

<?php
namespace Project\Infrastructure\Repository;

use Litvinab\Bundle\RestApiTestBundle\TestCase\FixturesWebTestCase;
use Project\Domain\Entity\Attribute;

class AttributeRepositoryTest extends FixturesWebTestCase
{
    public function setUp()
    {
        parent::setUp();

        $this->reloadDb();
    }

    public function test_get_by_slug__success()
    {
        $repo = $this->getRepository('ProjectDomain:Attribute');
        $attr = $repo->getBySlug('currency');

        $this->assertInstanceOf(Attribute::class, $attr);
        $this->assertEquals(2, $attr->getId());
        $this->assertEquals('Product Currency', $attr->getCaption());
    }
}

RestFixturesWebTestCase

<?php

namespace Project\Application\Controller;

use Litvinab\Bundle\RestApiTestBundle\TestCase\RestFixturesWebTestCase;
use Symfony\Component\HttpFoundation\Response;

class AttributeControllerTest extends RestFixturesWebTestCase
{
    public function test_get_task__success()
    {
        $this->reloadDb();

        $response = $this->getJson('/api/attributes/currency');

        $expectedJson = '{"caption":"Product Currency","slug":"currency"}';
        $this->assertJsonResponse($response, Response::HTTP_OK, $expectedJson);
    }

    public function test_get_task__failure_with_integer_id()
    {
        $response = $this->getJson('/api/attributes/3');

        $this->assertResponseCode($response, Response::HTTP_NOT_FOUND);
    }
}    

Acceptance (Behat) Tests

Configuration

1. Add route to routing.yml:, (*19)

rest_api_test:
    resource: "@RestApiTestBundle/Resources/config/routing.yml"
    prefix:   /api/test

2. Add parameters to parameters.yml (rest_api_test.access_token value should be secured enough):, (*20)

    rest_api_test.controller_on: true
    rest_api_test.access_token: "5j%]4fX.)z[HTR{M"

rest_api_test.controller_on (false by default) - parameter to turn ON/OFF endpoint to reload database in symfony prod environment., (*21)

Be careful! It should be turned ON for acceptance tests (external tests of rest API) only.
Do not turn ON it at production server! It would be good to setup additional security rules for this endpoint, for example, white list of IPs., (*22)

rest_api_test.access_token (empty string by default) - random strong password, for example, 5j%]4fX.)z[HTR{M; This token required to access to reload db endpoint; It will be applied only ifrest_api_test.controller_on is set to true;, (*23)

How To Use

Your application side:, (*24)

1. Setup data fixtures if they weren't setup yet;, (*25)

2. Setup separated application instance with not production database;, (*26)

Bundle provides possibility to reload data fixtures in prod symfony environment to make possible an end-user testing;, (*27)

3. check configuration setup in parameters.yml;, (*28)

Behat tests runner side:, (*29)

Make GET request to reload database state of your application. It should be made before each acceptance test., (*30)

Endpoint: http://YOUR_DOMAIN/api/test/db/reload, (*31)

Custom header: Rest-API-Test-Access-Token: 5j%]4fX.)z[HTR{M (value is value of rest_api_test.access_token parameter);, (*32)

As result database mentioned in parameters.yml should be reloaded with your test data fixtures;, (*33)

The Versions

17/07 2017

dev-master

9999999-dev https://github.com/litvinab/rest-api-test

This symfony bundle is made to simplify testing of your rest API. It can be useful in running of functional data fixtures based tests and acceptance (Behat) tests. It simplifies automatic tests development by provided test cases classes and commands

  Sources   Download

The Requires

 

The Development Requires

by Anton Litvin

test rest api behat test case

17/07 2017

v1.2.0

1.2.0.0 https://github.com/litvinab/rest-api-test

This symfony bundle is made to simplify testing of your rest API. It can be useful in running of functional data fixtures based tests and acceptance (Behat) tests. It simplifies automatic tests development by provided test cases classes and commands

  Sources   Download

The Requires

 

The Development Requires

by Anton Litvin

test rest api behat test case

13/06 2017

v1.1.1

1.1.1.0 https://github.com/litvinab/rest-api-test

Contains Symfony command and controller to reload test database from scratch, data fixtures based test case to make automatic tests pretty simple and readable

  Sources   Download

The Requires

 

The Development Requires

by Anton Litvin

test rest api test case

13/06 2017

dev-feature/fixtures-only

dev-feature/fixtures-only https://github.com/litvinab/rest-api-test

Contains Symfony command and controller to reload test database from scratch, data fixtures based test case to make automatic tests pretty simple and readable

  Sources   Download

The Requires

 

The Development Requires

by Anton Litvin

test rest api test case

12/06 2017

v1.1.0

1.1.0.0 https://github.com/litvinab/rest-api-test

Contains Symfony command and controller to reload test database from scratch, data fixtures based test case to make automatic tests pretty simple and readable

  Sources   Download

The Requires

 

The Development Requires

by Anton Litvin

test rest api test case

10/05 2017

v1.0.0

1.0.0.0 https://github.com/litvinab/rest-api-test

Contains Symfony command and controller to reload test database from scratch, data fixtures based test case to make automatic tests pretty simple and readable

  Sources   Download

The Requires

 

The Development Requires

by Anton Litvin

test rest api test case