In this article, check out some of the most useful tips for performing functional testing with Symfony.

Taking part in testing more than 50 projects we saw how tests can give confidence in the code base, begin to save time for the entire development team and help to comply with business requirements.

For those who come from other ecosystems, let us first explain what the term “functional tests” means in Symfony. The following definition is given in the documentation:

“Functional tests verify the integration of different levels of the application (from routing to views)”

In essence, these are end-to-end tests. You write code that sends HTTP requests to the application. You get an HTTP response and make assumptions based on it. However, you can contact the database and make changes to the data storage level, which sometimes gives additional opportunities for checking the status.

Today, we have prepared 10 tips on writing functional tests with Symfony, based on our experience.

1. Testing with Data Storage Tier

Probably the first thing you want to do when running functional tests is to separate the test base from the development base. This is done to create a clean bench for running tests and allows you to create and manage the desired state of the application. Besides, tests shouldn’t write random data to a copy of the development database.

Typically, when running tests, a Symfony application is connected to another database. If you have Symfony 4 or 5, then you can define environment variables in the .env.test file that will be used for testing. Also, configure PHPUnit to change the environment variable APP_ENV to test. Fortunately, this happens by default when you install the Symfony component of PHPUnit Bridge.

For below 4 versions, you can use kernel booting in test mode when you run functional tests. Using the config_test.yml files, you can define your test configuration.

2. LiipFunctionalTestBundle

This package contains some important supporting tools for writing Symfony tests. Sometimes he tries to do too much and can interfere, but overall facilitates the work.

For example, during testing, you can emulate user input, upload fixture data, count database queries to check regression performance, etc. We recommend installing this package at the beginning of testing a new Symfony application.

3. Cleaning the database after each test

When do I need to clean the base during testing? The Symfony toolkit does not give a hint. We prefer to update the database after each test method. The test suite looks like this:

<?php

namespace Tests;

use Tests\BaseTestCase;

class SomeControllerTest extends TestCase
{
    public function test_a_registered_user_can_login()
    {
        // Clean slate. Database is empty.
        // Create your world. Create users, roles and data.
        // Execute logic
        // Assert the outcome.
        // Database is reset.
    }
}

A great way to clean up a database is to load empty fixtures into a special setUp method in PHPUnit. You can do this if you have installed LiipFunctionalTestBundle.

<?php
namespace Tests;
class BaseTestCase extends PHPUnit_Test_Case
{
    public function setUp()
    {
        $this->loadFixtures([]);
    }
}

#testing #symfony #developer

10 Functional Testing Tips in Symfony
1.75 GEEK