2017 © Pedro Peláez
 

symfony-bundle functional-test-bundle

Symfony bundle for functional testing

image

speicher210/functional-test-bundle

Symfony bundle for functional testing

  • Thursday, July 26, 2018
  • by wingu
  • Repository
  • 4 Watchers
  • 0 Stars
  • 2,975 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 2 Open issues
  • 18 Versions
  • 37 % Grown

The README.md

Speicher210 Functional Test Bundle

Introduction

This Bundle provides base classes and functionality for writing and running functional tests with focus on testing REST endpoint. It provides help in setting up test database and loading fixtures and mocking services in DI (even private services)., (*1)

Installation

Download the Bundle

$ composer require --dev speicher210/functional-test-bundle

Enable the Bundle

Symfony

<?php
// config/bundles.php

return [
    // ...
    Speicher210\FunctionalTestBundle\Speicher210FunctionalTestBundle::class => ['dev' => true, 'test' => true],
    // ...
];

Basic usage

Bootstrap for PHPUnit:, (*2)

<?php

declare(strict_types=1);

require dirname(__DIR__) . '/config/bootstrap.php';
require_once dirname(__DIR__) . '/vendor/speicher210/functional-test-bundle/src/Test/bootstrap.php';

// $kernel variable will contain the current Kernel instance

<?php

declare(strict_types=1);

use Speicher210\FunctionalTestBundle\Test\RestControllerWebTestCase;
use Symfony\Component\HttpFoundation\Request;

final class MyUserEndpointTest extends RestControllerWebTestCase
{
    public function testReturn404IfUserIsNotFound() : void
    {
        $this->assertRestRequestReturns404('/api/user/1', Request::METHOD_GET);
    }

    public function testReturns200AndUserData() : void
    {
        $this->assertRestGetPath('/api/user/1');
    }
}

The assertions are done using snapshots for comparison. By default the framework will look under Expected directory (from where the test class is located) for a file with the same name as the test and suffixed with -1.json., (*3)

Example of expected output can be: Expected/testReturns200AndUserData-1.json, (*4)

{
  "id": "1",
  "first_name": "John",
  "last_name": "Doe",
  "email": "@string@",
  "sign_up_date_time": "@string@.isDateTime()"
}

The -1.jsonsuffix will be incremented for every REST assertion made under one test. In the expected any functionality from coduo/php-matcher can be used., (*5)

It is possible to automatically update content of expected files during test execution to the actual content by adding this extension to your phpunit config:, (*6)

<extensions>
    <extension class="Speicher210\FunctionalTestBundle\Extension\RestRequestFailTestExpectedOutputFileUpdater" />
</extensions>

Fixtures are loaded using Doctrine fixtures. By default the framework will look by default under Fixtures directory (from where the test class is located) for a PHP file with the same name as the test. This file must return an array of class names that extend Speicher210\FunctionalTestBundle\Test\Loader\AbstractLoader class. Example of fixture file can be:, (*7)

<?php
// Fixtures/testReturns200AndUserData.php

return [
    \App\Tests\Fixtures\Loader\LoadOneUser::class
];

In order to rebuild and reset the database you need to create a bootstrap file for PHPUnit. In your own bootstrap file you can include the file Test/bootstrap.php which will reset the test database. The database will not be rebuild for every test, but only once when the tests start. This means that data must be removed before running next test. This can be achieved by running tests in a transaction. For this add the extension to your phpunit config:, (*8)

<extensions>
    <extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension" />
</extensions>

Accessing and mocking services

Mocking services can be achieved by using, (*9)

<?php

$this->mockContainerService('my_service_id', $myServiceMock);

Services can also be accessed by using, (*10)

<?php

$this->getContainerService('my_service_id');

The Versions

17/07 2018
05/07 2018
14/06 2018

0.6.1

0.6.1.0

Symfony bundle for functional testing

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dragos Protung

01/06 2018

0.6.0

0.6.0.0

Symfony bundle for functional testing

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dragos Protung

28/02 2018

0.5.0

0.5.0.0

Symfony bundle for functional testing

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dragos Protung

09/02 2018

0.4.0

0.4.0.0

Symfony bundle for functional testing

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dragos Protung

25/01 2018

0.3.1

0.3.1.0

Symfony bundle for functional testing

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dragos Protung

06/10 2017
09/08 2017
08/08 2017
13/07 2017
07/07 2017
03/07 2017