diff --git a/tests/integration/Core/Tester.php b/tests/integration/Core/Tester.php index 9663f3758c..d153f96257 100644 --- a/tests/integration/Core/Tester.php +++ b/tests/integration/Core/Tester.php @@ -306,8 +306,8 @@ class Tester die("Permission denied for directory [".$this->installPath."].\n"); } - Utils::checkCreateDatabase($configData['database']); $this->reset($fileManager, $latestEspoDir); + Utils::fixUndefinedVariables(); chdir($this->installPath); @@ -327,6 +327,7 @@ class Tester $installer->saveConfig($configData); + $this->createDatabase(); $this->dropTables(); $installer = new \Installer(); //reload installer to get all config data @@ -334,6 +335,45 @@ class Tester $installer->setSuccess(); } + private function createDatabase(): void + { + $app = new Application(); + + $databaseHelper = $app->getContainer() + ->getByClass(InjectableFactory::class) + ->create(DatabaseHelper::class); + + $config = $app->getContainer()->getByClass(Config::class); + + $pdo = $databaseHelper->getPDO(); + $platform = $databaseHelper->getDbalConnection()->getDatabasePlatform(); + + $dbname = $config->get('database.dbname'); + + if (!$dbname) { + throw new \RuntimeException('No "dbname" in database config.'); + } + + $quotedDbname = $platform->quoteIdentifier($dbname); + + $sql = "CREATE DATABASE IF NOT EXISTS {$quotedDbname}"; + + if ($config->get('database.platform') === 'Postgresql') { + $sqlCheck = "SELECT datname FROM pg_database WHERE datname = " . $platform->quoteStringLiteral($dbname); + + $sth = $pdo->prepare($sqlCheck); + $sth->execute(); + + if ($sth->fetch()) { + return; + } + + $sql = "CREATE DATABASE {$quotedDbname}"; + } + + $pdo->query($sql); + } + private function dropTables(): void { $app = new Application(); @@ -345,7 +385,17 @@ class Tester $pdo = $databaseHelper->getPDO(); $platform = $databaseHelper->getDbalConnection()->getDatabasePlatform(); - $rows = $pdo->query("SHOW TABLES"); + $config = $app->getContainer()->getByClass(Config::class); + + $showTablesSql = "SHOW TABLES"; + + if ($config->get('database.platform') === 'Postgresql') { + $showTablesSql = + "SELECT table_name FROM information_schema.tables " . + "WHERE table_schema='public' AND table_type='BASE TABLE'"; + } + + $rows = $pdo->query($showTablesSql); while ($row = $rows->fetch(\PDO::FETCH_NUM)) { $table = $row[0]; diff --git a/tests/integration/Core/Utils.php b/tests/integration/Core/Utils.php index f7378f113b..b503e9b8d2 100644 --- a/tests/integration/Core/Utils.php +++ b/tests/integration/Core/Utils.php @@ -29,9 +29,6 @@ namespace tests\integration\Core; -use Espo\Core\Exceptions\Error; -use PDO; - class Utils { /** @@ -139,31 +136,4 @@ class Utils } } } - - public static function checkCreateDatabase(array $databaseParams): void - { - if (!isset($databaseParams['dbname'])) { - throw new \RuntimeException('Option "dbname" is not found.'); - } - - $dbname = $databaseParams['dbname']; - unset($databaseParams['dbname']); - - $sql = "CREATE DATABASE IF NOT EXISTS `{$dbname}`"; - - $pdo = static::createPdoConnection($databaseParams); - - $pdo->query($sql); - } - - private static function createPdoConnection(array $params): PDO - { - $platform = !empty($params['platform']) ? strtolower($params['platform']) : 'mysql'; - $port = empty($params['port']) ? '' : ';port=' . $params['port']; - $dbname = empty($params['dbname']) ? '' : ';dbname=' . $params['dbname']; - - $dsn = $platform . ':host=' . $params['host'] . $port . $dbname; - - return new PDO($dsn, $params['user'], $params['password']); - } }