From d317133015a7028854f05ad68b97666c7b6d634d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BCrk?= Date: Wed, 14 Aug 2024 19:00:45 +0200 Subject: [PATCH] [TASK] Streamline `Testbase->setUpTestDatabase()` The `Testbase->setUpTestDatabase()` takes care of correct database setup for each test instance and that the database is created. To accomplish that, current TYPO3 connections are closed. Doctrine DriverManager is used as lowlevel tool to create the instance database. In case that the database has not been created, which occures on the first test execution per test-case, an exception is thrown and disturbes the `getDatabasePlatform <-> getServerVersion()` detection with the Doctrine construct. That has been mitigated by catching the exception within `Connection->getServerVersion()` and returning an empty string ending up retrieving at least a the lowest default platform for the driver. That was mainly a workaround in functional test runs. Doctrine DBAL 3.9.x & 4.1.x will introduce a new version based postgres platform class along with a new exception, if the server version does not match the version pattern - which is the case for an emtpy string and not returning a PostgresSQL platform anymore - failing to create the database. This change catches the exception for non existing database to prepare the removal of the connection workaround in `Connection->getServerVersion()`, but still ensures the ConnectionPool instances are closed. Minor code cleanups within the method are applied in the same run. --- Classes/Core/Testbase.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Classes/Core/Testbase.php b/Classes/Core/Testbase.php index 9b04635d..e8885df9 100644 --- a/Classes/Core/Testbase.php +++ b/Classes/Core/Testbase.php @@ -668,10 +668,14 @@ public function setUpPackageStates( public function setUpTestDatabase(string $databaseName, string $originalDatabaseName): void { // First close existing connections from a possible previous test case and - // tell our ConnectionPool there are no current connections anymore. + // tell our ConnectionPool there are no current connections anymore. In case + // database does not exist yet, an exception is thrown which we catch here. $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); - $connection = $connectionPool->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); - $connection->close(); + try { + $connection = $connectionPool->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); + $connection->close(); + } catch (DBALException) { + } $connectionPool->resetConnections(); // Drop database if exists. Directly using the Doctrine DriverManager to @@ -689,8 +693,8 @@ public function setUpTestDatabase(string $databaseName, string $originalDatabase $configuration->setSchemaManagerFactory($coreSchemaFactory); } $driverConnection = DriverManager::getConnection($connectionParameters, $configuration); + $schemaManager = $driverConnection->createSchemaManager(); - $platform = $driverConnection->getDatabasePlatform(); $isSQLite = self::isSQLite($driverConnection); // doctrine/dbal no longer supports createDatabase() and dropDatabase() statements. Guard it.