Skip to content

Base classes for writing unit and integration tests in Symfony2

Notifications You must be signed in to change notification settings

seegno/SeegnoTestBundle

Repository files navigation

SeegnoTestBundle

Latest Version Build Status Code Climate Test Coverage License

Introduction

This Bundle provides base classes for unit and integration tests in order to assist in setting test databases and data fixtures.

Installation

1. Download SeegnoTestBundle using composer

Install SeegnoTestBundle by running the command:

$ composer require --dev seegno/test-bundle

2. Enable the bundle

Enable the bundle in the kernel:

<?php
// app/AppKernel.php

public function registerBundles()
{
    // ...

    if (in_array($this->getEnvironment(), array('test'))) {
        // ...
        $bundles[] = new Seegno\TestBundle\SeegnoTestBundle();
    }
}

3. Prepare your Application for tests

Integration (functional)

Please add the following configuration in config_test.yml:

seegno_test:
    database:
        driver: ORM # Types available: ORM (SQL) and ODM (MongoDB)

Warning!!

It's very important that you configure a different database for tests in your config_test.yml file since all data is purged from the database in integration tests.

Usage

Unit tests

use Seegno\TestBundle\TestCase\BaseTestCase;

class SomeClassTest extends BaseTestCase
{
    //...
}

Available features:

$this->getFaker(); // Get a faker instance.
$this->setReflectionProperty($class, $propertyName, $propertyValue); // Set a class property using reflection.

Integration tests

use Seegno\TestBundle\TestCase\IntegrationTestCase;

class SomeClassTest extends IntegrationTestCase
{
    //...
}

Available features:

$this->getContainer(); // Get an instance of the dependency injection container.
$this->getSession(); // Get session.
$this->get('SERVICE'); // Get services.
$this->initializeDatabase(); // Initialize test database (SQL or MongoDB).

Web tests

use Seegno\TestBundle\TestCase\WebTestCase;

class SomeClassTest extends WebTestCase
{
    //...
}

Available features:

$this->authenticateUser($client, $user, $credentials, $roles, $firewall); // Authenticate a user.
$this->getClient(); // Get client that simulates a browser and makes requests to a Kernel object.
$this->assertResponseFields($response, $object, $groups); // Assert that object properties keys are in the response.

Run tests

Before running the tests, make sure you have the test database updated.

php app/console doctrine:database:drop --env=test --force
php app/console doctrine:database:create --env=test
php app/console doctrine:schema:create --env=test

To run the tests on your local machine, just use the phpunit command:

phpunit

About

Base classes for writing unit and integration tests in Symfony2

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages