{self::DB_GROUP} ?? []; self::assertSafeTestDatabase($config->default ?? [], $dbConfig); try { $db = Database::connect(self::DB_GROUP); $db->initialize(); } catch (Throwable $exception) { throw new \RuntimeException( sprintf( 'Test database connection failed: group=%s host=%s port=%s database=%s', self::DB_GROUP, $dbConfig['hostname'] ?? '', $dbConfig['port'] ?? '', $dbConfig['database'] ?? '' ), 0, $exception ); } $db->disableForeignKeyChecks(); $tables = $db->resetDataCache()->listTables(); foreach ($tables as $table) { if (!in_array($table, self::$excludeTables, true) && $db->tableExists($table, false)) { $db->query('TRUNCATE TABLE ' . $db->escapeIdentifiers($table)); } } $db->enableForeignKeyChecks(); } private static function assertSafeTestDatabase(array $defaultConfig, array $testConfig): void { $defaultDatabase = (string) ($defaultConfig['database'] ?? ''); $testDatabase = (string) ($testConfig['database'] ?? ''); if ($testDatabase === '') { throw new \RuntimeException('Refusing to reset database: tests database name is empty.'); } if ($defaultDatabase !== '' && $testDatabase === $defaultDatabase) { throw new \RuntimeException(sprintf( 'Refusing to reset database "%s": tests database matches the default application database.', $testDatabase )); } if (! preg_match('/(^test_|_test$|^tests$|^alrahma_test$)/i', $testDatabase)) { throw new \RuntimeException(sprintf( 'Refusing to reset database "%s": test database names must clearly identify a test database.', $testDatabase )); } } }