2017 © Pedro Peláez
 

library symfony-test

A library for testing Symfony applications

image

tomaskadlec/symfony-test

A library for testing Symfony applications

  • Friday, March 11, 2016
  • by tomaskadlec
  • Repository
  • 1 Watchers
  • 0 Stars
  • 161 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 5 % Grown

The README.md

symfony-test

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)., (*1)

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., (*2)

Instalation

The library supports Symfony >=3. Install it using composer:, (*3)

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., (*4)

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., (*5)

ObjectManager

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., (*6)

class MyTestCase extends ApplicationTestCase {

   use ObjectManagerAwareTrait;

    protected function getManagerName()
    {
        return 'default';
    }

    public function testThis()
    {
        $this->objectManager->
    }
}

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., (*7)

class MyTestCase extends ApplicationTestCase {

   use SecurityAwareTrait;

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

    public function testThis()
    {
        // ...
        $this->assertTrue($this->securityContext->isGranted(...))
    }
}

Fixtures

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., (*8)

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)., (*9)

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., (*10)

Using a fixture

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

Exceptions

If an error occurs exception is thrown * ReferenceException - generic error, * ReferenceDoesNotExistException - key does not exist (get, remove), * ReferenceExistsException - key exists (add)., (*12)

The Versions

11/03 2016

dev-master

9999999-dev https://ict.fit.cvut.cz/gitlab/symfony/library-unittest

A library for testing Symfony applications

  Sources   Download

MIT

The Requires

 

by Tomas Kadlec

test phpunit unit test

11/03 2016

1.0.2

1.0.2.0 https://ict.fit.cvut.cz/gitlab/symfony/library-unittest

A library for testing Symfony applications

  Sources   Download

MIT

The Requires

 

by Tomas Kadlec

test phpunit unit test