From 15eca8a62c5e20bad08b1e2527caa75319ff2ef7 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Wed, 13 Mar 2024 11:50:32 +0100 Subject: [PATCH] dev: Enable DAMA DoctrineTestBundle in tests This package drastically improves performance of the tests by rolling back changes in database after each test. --- .phpunit.xml.dist | 1 + composer.json | 1 + composer.lock | 70 ++++++++++++++++++++++++++++++++++- config/bundles.php | 1 + config/packages/doctrine.yaml | 3 ++ symfony.lock | 9 +++++ tests/MigrationsTest.php | 14 +++++++ 7 files changed, 98 insertions(+), 1 deletion(-) diff --git a/.phpunit.xml.dist b/.phpunit.xml.dist index 54130a12..07f749a1 100644 --- a/.phpunit.xml.dist +++ b/.phpunit.xml.dist @@ -29,5 +29,6 @@ + diff --git a/composer.json b/composer.json index 1ba5f373..2ddd9766 100644 --- a/composer.json +++ b/composer.json @@ -103,6 +103,7 @@ } }, "require-dev": { + "dama/doctrine-test-bundle": "^8.0", "phpstan/phpstan": "^1.8", "phpunit/phpunit": "^10.5", "rector/rector": "^0.19.5", diff --git a/composer.lock b/composer.lock index ca0f527e..229669e0 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f6ab4715380374d706cf6820d9996955", + "content-hash": "120259f4b6f03a476001445460212e47", "packages": [ { "name": "carbonphp/carbon-doctrine-types", @@ -7884,6 +7884,74 @@ } ], "packages-dev": [ + { + "name": "dama/doctrine-test-bundle", + "version": "v8.0.2", + "source": { + "type": "git", + "url": "https://github.com/dmaicher/doctrine-test-bundle.git", + "reference": "f10de294e41570d027a301554a609c394d40e669" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/dmaicher/doctrine-test-bundle/zipball/f10de294e41570d027a301554a609c394d40e669", + "reference": "f10de294e41570d027a301554a609c394d40e669", + "shasum": "" + }, + "require": { + "doctrine/dbal": "^3.3 || ^4.0", + "doctrine/doctrine-bundle": "^2.2.2", + "ext-json": "*", + "php": "^7.3 || ^8.0", + "psr/cache": "^1.0 || ^2.0 || ^3.0", + "symfony/cache": "^5.4 || ^6.3 || ^7.0", + "symfony/framework-bundle": "^5.4 || ^6.3 || ^7.0" + }, + "require-dev": { + "behat/behat": "^3.0", + "friendsofphp/php-cs-fixer": "^3.27", + "phpstan/phpstan": "^1.2", + "phpunit/phpunit": "^8.0 || ^9.0 || ^10.0", + "symfony/phpunit-bridge": "^6.3", + "symfony/process": "^5.4 || ^6.3 || ^7.0", + "symfony/yaml": "^5.4 || ^6.3 || ^7.0" + }, + "type": "symfony-bundle", + "extra": { + "branch-alias": { + "dev-master": "8.x-dev" + } + }, + "autoload": { + "psr-4": { + "DAMA\\DoctrineTestBundle\\": "src/DAMA/DoctrineTestBundle" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "David Maicher", + "email": "mail@dmaicher.de" + } + ], + "description": "Symfony bundle to isolate doctrine database tests and improve test performance", + "keywords": [ + "doctrine", + "isolation", + "performance", + "symfony", + "testing", + "tests" + ], + "support": { + "issues": "https://github.com/dmaicher/doctrine-test-bundle/issues", + "source": "https://github.com/dmaicher/doctrine-test-bundle/tree/v8.0.2" + }, + "time": "2024-02-15T08:28:14+00:00" + }, { "name": "fakerphp/faker", "version": "v1.23.1", diff --git a/config/bundles.php b/config/bundles.php index 8412c495..0d8ade35 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -6,6 +6,7 @@ return [ Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], + DAMA\DoctrineTestBundle\DAMADoctrineTestBundle::class => ['test' => true], Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true], Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true], Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true], diff --git a/config/packages/doctrine.yaml b/config/packages/doctrine.yaml index 263ac0b3..2fc78acc 100644 --- a/config/packages/doctrine.yaml +++ b/config/packages/doctrine.yaml @@ -11,6 +11,9 @@ doctrine: #server_version: '15' profiling_collect_backtrace: '%kernel.debug%' + + use_savepoints: true + orm: auto_generate_proxy_classes: true enable_lazy_ghost_objects: true diff --git a/symfony.lock b/symfony.lock index d5a2aae4..d821995d 100644 --- a/symfony.lock +++ b/symfony.lock @@ -1,4 +1,13 @@ { + "dama/doctrine-test-bundle": { + "version": "8.0", + "recipe": { + "repo": "github.com/symfony/recipes-contrib", + "branch": "main", + "version": "7.2", + "ref": "896306d79d4ee143af9eadf9b09fd34a8c391b70" + } + }, "doctrine/doctrine-bundle": { "version": "2.11", "recipe": { diff --git a/tests/MigrationsTest.php b/tests/MigrationsTest.php index ed8e90c1..ae415b6c 100644 --- a/tests/MigrationsTest.php +++ b/tests/MigrationsTest.php @@ -6,6 +6,7 @@ namespace App\Tests; +use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\Console\Command\Command; use Zenstruck\Foundry\Test\ResetDatabase; @@ -15,6 +16,19 @@ class MigrationsTest extends KernelTestCase use CommandTestsHelper; use ResetDatabase; + public static function setUpBeforeClass(): void + { + // We need to disable DoctrineTestBundle for this test, or it fails + // when using MariaDB, with "Exception in third-party event subscriber: + // There is no active transaction". + StaticDriver::setKeepStaticConnections(false); + } + + public static function tearDownAfterClass(): void + { + StaticDriver::setKeepStaticConnections(true); + } + public function testMigrationsUpWork(): void { // erase the database structure