This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.
This package is abandoned and no longer maintained. No replacement package was suggested.

A library for testing Symfony applications

1.0.2 2016-03-11 15:17 UTC

This package is not auto-updated.

Last update: 2019-06-16 17:38:34 UTC


symfony-test library simplifies use of the KernelTestCase class. It provides traits that can handle initialization of:

  • kernel and container (ContainerAwareTrait),
  • security context (SecurityContextAwareTrait),
  • object manager (ObjectManagerAwareTrait).

The ContainerAwareTrait implements setUp and tearDown that boots / shuts down the kernel and also calls all setUp* methods defined in the other provided traits (you can use it too in yout traits). The library provides also following classes

  • ApplicationTestCase, direct descendant of KernelTestCase with initialized container,
  • WebTestCase, direct descendant of WebTestCase with initialized container.


The library supports Symfony >=3. Install it using composer:

path/to/project $ composer require tomaskadlec/symfony-test

Basic usage

Use of ApplicationTestCase or ContainerAwareTrait adds a property named $container and populates it with current instance.

class MyTestCase extends ApplicationTestCase
    public function testThis()
        $service = $this->container->get('...');
        // ...

Initialization of common services

The library can handle initialization of common services - object manager and security can be initialized this way.


ObjectManagerAwareTrait allows to easily access databases. A manager can be selected (instead of default one) by overriding getManagerName() method. Name of the manager is supposed to be retuned by this method.

class MyTestCase extends ApplicationTestCase {

   use ObjectManagerAwareTrait;

    protected function getManagerName()
        return 'default';

    public function testThis()

Security - TokenStorage

SecurityAwareTrait initializes security.token_storage with an authenticated token. An instance of UserInterface (represents the desired user) must be provided as a return value of getUser() method.

class MyTestCase extends ApplicationTestCase {

   use SecurityAwareTrait;

    protected function getUser()
        $user = new MyUser(...); // must implement UserInterface!!!
        // ...
        return $user;

    public function testThis()
        // ...


ApplicationTestCase adds support for loading fixtures into memory and allows to reference them by simple keys. A fixture is an object implementing a FixtureInterface. Handling fixtures via the library is sufficient for simple use cases. If your fixtures are more complicated and/or stored in database consider using doctrine/doctrine-fixtures-bundle or nelmio/alice.

Preparing a fixture

One has to implement aforementioned interface. Method load(ExecutorInterface $executor) is responsible for creating instances and registering them as references. Reference repository is shared amongst all instances of executors. An executor is passed into the method and can be used to

  • addReference($key, $object),
  • removeReference($key),
  • getReference($key).

Loading fixtures is done by ApplicationTestCase::loadFixtures(array $fixtures). $fixtures is an array of fully qualified class names that references to classes that implement FixtureInterface and shall be loaded.

Using a fixture

Fixture represents an expected object therefore it can be used directly. Retrieving fixtures is done using ApplicationTestCase::getFixtures()::getReference($key).


If an error occurs exception is thrown

  • ReferenceException - generic error,
  • ReferenceDoesNotExistException - key does not exist (get, remove),
  • ReferenceExistsException - key exists (add).