Tips for testing the database in Symfony application

Use SQLite Memory DB

SQLite In-memory database is a great alternative to test the interaction with database. As they exist only in the memory of the application, they are truly disposable. And it is also very easy to set up with Symfony appl…



Use SQLite Memory DB

SQLite In-memory database is a great alternative to test the interaction with database. As they exist only in the memory of the application, they are truly disposable. And it is also very easy to set up with Symfony applications that use Doctrine.

? Install php extension to support SQLite: more about install sqlite for php


## I installed the extension in alpine docker
RUN apk add --update \
    ...
    php7-mysqli \
    ...

? Check if sqlite installed and enabled

 php -i | grep sqlite

? Config Memory DB in Symfony

## app/config/packages/test/doctrine.yaml
doctrine:
    dbal:
        connections:
            default:
                driver: 'pdo_sqlite'
                url: '%env(resolve:DATABASE_URL)%'

? Config app env file

## app/.env.test.local

## :memory: will create the database in memory
DATABASE_URL="sqlite:///:memory:"

## %kernel.project_dir%/db/sqlite3.db3 will breate the database on filesystem
# DATABASE_URL="sqlite:///%kernel.project_dir%/db/sqlite3.db3"

? Create a DatabaseTestCase

<?php

declare(strict_types = 1);

namespace App\Tests\Utils;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use LogicException;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpKernel\KernelInterface;

class DatabaseTestCase extends KernelTestCase
{
    protected EntityManagerInterface $entityManager;

    protected function setUp(): void
    {
        $kernel = self::bootKernel();

        if ('test' !== $kernel->getEnvironment()) {
            throw new LogicException('Execution only in Test environment possible!');
        }

        $this->initDatabase($kernel);

        $this->entityManager = $kernel->getContainer()
            ->get('doctrine')
            ->getManager();
    }

    private function initDatabase(KernelInterface $kernel): void
    {
        $entityManager = $kernel->getContainer()->get('doctrine.orm.entity_manager');
        $metaData = $entityManager->getMetadataFactory()->getAllMetadata();
        $schemaTool = new SchemaTool($entityManager);
        $schemaTool->updateSchema($metaData);
    }
}

? Example to test the Repository class in symfony

final class ScheduleRepositoryTest extends DatabaseTestCase
{
    private ?ScheduleRepository $repository;

    protected function setUp(): void
    {
        parent::setUp();

        $this->repository = ScheduleRepositoryTest::$container->get(ScheduleRepository::class);
    }

    public function testFindDefault(): void
    {
        $this->assertEmpty($this->repository->findDefault());

        $this->insertDefaultSchedule();
        $this->assertInstanceOf(Schedule::class, $this->repository->findDefault());
    }

    private function insertDefaultSchedule(): void
    {
        $default = Schedule::defaultSchedule();

        $this->entityManager->persist($default);
        $this->entityManager->flush();
    }
}

The execution of the tests is pretty fast, see the screenshot
Alt Text



Use symfony Test-bundle to benefit from transaction and caching

By using this test bundle, it will begin a transaction before every testcase and roll it back again after the test finished for all configured DBAL connections. This results in a performance boost as there is no need to rebuild the schema, import a backup SQL dump or re-insert fixtures before every testcase. It also includes a StaticArrayCache that will be automatically configured as meta data & query cache for all EntityManagers. This improved the speed and memory usage for all testsuites dramatically.

? install the package

composer require --dev dama/doctrine-test-bundle

? Config the bundle in dama_doctrine_test_bundle.yaml

## app/config/packages/test/dama_doctrine_test_bundle.yaml
dama_doctrine_test:
    enable_static_connection: true
    enable_static_meta_data_cache: true
    enable_static_query_cache: true

? Enable the bundle in bundles.php

if ($env === 'test') {
    $bundles[] = new DAMA\DoctrineTestBundle\DAMADoctrineTestBundle();
}

? Enable the PHPunit listner in phpunit.xml.dist: example for php7.4, more config info

<phpunit>
    ...
    <listeners>
        <listener class="\DAMA\DoctrineTestBundle\PHPUnit\PHPUnitListener" />
    </listeners>
</phpunit>

Print Share Comment Cite Upload Translate
APA
Xun Zhou | Sciencx (2024-03-29T04:49:45+00:00) » Tips for testing the database in Symfony application. Retrieved from https://www.scien.cx/2021/07/16/tips-for-testing-the-database-in-symfony-application/.
MLA
" » Tips for testing the database in Symfony application." Xun Zhou | Sciencx - Friday July 16, 2021, https://www.scien.cx/2021/07/16/tips-for-testing-the-database-in-symfony-application/
HARVARD
Xun Zhou | Sciencx Friday July 16, 2021 » Tips for testing the database in Symfony application., viewed 2024-03-29T04:49:45+00:00,<https://www.scien.cx/2021/07/16/tips-for-testing-the-database-in-symfony-application/>
VANCOUVER
Xun Zhou | Sciencx - » Tips for testing the database in Symfony application. [Internet]. [Accessed 2024-03-29T04:49:45+00:00]. Available from: https://www.scien.cx/2021/07/16/tips-for-testing-the-database-in-symfony-application/
CHICAGO
" » Tips for testing the database in Symfony application." Xun Zhou | Sciencx - Accessed 2024-03-29T04:49:45+00:00. https://www.scien.cx/2021/07/16/tips-for-testing-the-database-in-symfony-application/
IEEE
" » Tips for testing the database in Symfony application." Xun Zhou | Sciencx [Online]. Available: https://www.scien.cx/2021/07/16/tips-for-testing-the-database-in-symfony-application/. [Accessed: 2024-03-29T04:49:45+00:00]
rf:citation
» Tips for testing the database in Symfony application | Xun Zhou | Sciencx | https://www.scien.cx/2021/07/16/tips-for-testing-the-database-in-symfony-application/ | 2024-03-29T04:49:45+00:00
https://github.com/addpipe/simple-recorderjs-demo