Skip to content

Commit

Permalink
[TASK] Extract some details from FunctionalTestCase::setUp()
Browse files Browse the repository at this point in the history
This change modifies the `FunctionalTestCase` and extract
database setup related code into two dedicated internal
methods:

* initializeTestDatabase()
* initializeTestDatabaseAndTruncateTables()

Note that these are marked as internal but needs to be
kept `protected` to allow one special testcase in the
TYPO3 core [1].

[1] https://review.typo3.org/c/Packages/TYPO3.CMS/+/84942
  • Loading branch information
sbuerk committed Jun 28, 2024
1 parent c7b6a28 commit b778734
Showing 1 changed file with 49 additions and 18 deletions.
67 changes: 49 additions & 18 deletions Classes/Core/Functional/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,7 @@ protected function setUp(): void
// in a test case, so environment is set up only once per test case.
GeneralUtility::purgeInstances();
$this->container = $testbase->setUpBasicTypo3Bootstrap($this->instancePath);
if ($this->initializeDatabase) {
$testbase->initializeTestDatabaseAndTruncateTables($dbPathSqlite, $dbPathSqliteEmpty);
}
$this->initializeTestDatabaseAndTruncateTables($testbase, $this->initializeDatabase, $dbPathSqlite, $dbPathSqliteEmpty);
$testbase->loadExtensionTables();
} else {
DatabaseSnapshot::initialize(dirname($this->getInstancePath()) . '/functional-sqlite-dbs/', $this->identifier);
Expand Down Expand Up @@ -361,22 +359,16 @@ protected function setUp(): void
$frameworkExtension
);
$this->container = $testbase->setUpBasicTypo3Bootstrap($this->instancePath);
if ($this->initializeDatabase) {
if ($dbDriver !== 'pdo_sqlite') {
$testbase->setUpTestDatabase($dbName, $originalDatabaseName);
} else {
$testbase->setUpTestDatabase($dbPathSqlite, $originalDatabaseName);
}
}
$this->initializeTestDatabase(
$this->container,
$testbase,
$dbDriver,
$this->initializeDatabase,
($dbDriver !== 'pdo_sqlite' ? $dbName : $dbPathSqlite),
$originalDatabaseName,
$dbPathSqliteEmpty
);
$testbase->loadExtensionTables();
if ($this->initializeDatabase) {
$testbase->createDatabaseStructure($this->container);
if ($dbDriver === 'pdo_sqlite') {
// Copy sqlite file '/path/functional-sqlite-dbs/test_123.sqlite' to
// '/path/functional-sqlite-dbs/test_123.empty.sqlite'. This is re-used for consecutive tests.
copy($dbPathSqlite, $dbPathSqliteEmpty);
}
}
}

parent::setUp();
Expand All @@ -402,6 +394,45 @@ protected function tearDown(): void
parent::tearDown();
}

/**
* Note executed only for the first test permutation for testcase.
*
* @internal only. Protected for special case \TYPO3\CMS\Core\Tests\Functional\Database\Schema\SchemaMigratorTest
*/
protected function initializeTestDatabase(
ContainerInterface $container,
Testbase $testbase,
string $dbDriver,
bool $initializeDatabase,
string $dbName,
string $originalDatabaseName,
string $dbPathSqliteEmpty,
): void {
if (!$initializeDatabase) {
return;
}
$testbase->setUpTestDatabase($dbName, $originalDatabaseName);
$testbase->createDatabaseStructure($container);
if ($dbDriver === 'pdo_sqlite') {
// Copy sqlite file '/path/functional-sqlite-dbs/test_123.sqlite' to
// '/path/functional-sqlite-dbs/test_123.empty.sqlite'. This is re-used for consecutive tests.
copy($dbName, $dbPathSqliteEmpty);
}
}

/**
* Note only executed for subsequential test permutations for testcase, not the first one.
*
* @internal only. Protected for special case \TYPO3\CMS\Core\Tests\Functional\Database\Schema\SchemaMigratorTest
*/
protected function initializeTestDatabaseAndTruncateTables(Testbase $testbase, bool $initializeDatabase, string $dbPathSqlite, string $dbPathSqliteEmpty): void
{
if (!$initializeDatabase) {
return;
}
$testbase->initializeTestDatabaseAndTruncateTables($dbPathSqlite, $dbPathSqliteEmpty);
}

protected function getConnectionPool(): ConnectionPool
{
return $this->get(ConnectionPool::class);
Expand Down

0 comments on commit b778734

Please sign in to comment.