From 163c3773cb6ed617614c5987868e2054f471b59e Mon Sep 17 00:00:00 2001 From: Yassine Guedidi Date: Tue, 18 Feb 2025 00:50:08 +0100 Subject: [PATCH] Replace dbname suffix by explicit test database names --- .env.test | 3 +++ app/config/config_test.yml | 2 -- app/config/tests/.env.sqlite | 2 +- tests/Command/InstallCommandTest.php | 21 +++++++++++++++------ 4 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 .env.test diff --git a/.env.test b/.env.test new file mode 100644 index 000000000..c604b65a8 --- /dev/null +++ b/.env.test @@ -0,0 +1,3 @@ +DATABASE_URL=sqlite:///%kernel.project_dir%/data/db/wallabag_test.sqlite?charset=utf8 +#DATABASE_URL=mysql://root:wallaroot@mariadb:3306/wallabag_test?charset=utf8mb4 +#DATABASE_URL=postgres://wallabag:wallapass@postgres:5432/wallabag_test?charset=utf8 diff --git a/app/config/config_test.yml b/app/config/config_test.yml index a9bb06a65..5a2adb377 100644 --- a/app/config/config_test.yml +++ b/app/config/config_test.yml @@ -4,7 +4,6 @@ imports: parameters: fosuser_registration: true - wallabag_dbname_suffix: '_test' database_url: '%env(resolve:DATABASE_URL)%' domain_name: '%env(DOMAIN_NAME)%' @@ -25,7 +24,6 @@ web_profiler: doctrine: dbal: - dbname_suffix: '%wallabag_dbname_suffix%' # for MySQL and PostgreSQL use_savepoints: true orm: diff --git a/app/config/tests/.env.sqlite b/app/config/tests/.env.sqlite index 12f18d778..672ce6818 100644 --- a/app/config/tests/.env.sqlite +++ b/app/config/tests/.env.sqlite @@ -1 +1 @@ -DATABASE_URL=sqlite:///%kernel.project_dir%/data/db/wallabag%wallabag_dbname_suffix%.sqlite?charset=utf8 +DATABASE_URL=sqlite:///%kernel.project_dir%/data/db/wallabag.sqlite?charset=utf8 diff --git a/tests/Command/InstallCommandTest.php b/tests/Command/InstallCommandTest.php index ca25ba0e6..294536d73 100644 --- a/tests/Command/InstallCommandTest.php +++ b/tests/Command/InstallCommandTest.php @@ -41,11 +41,18 @@ class InstallCommandTest extends WallabagTestCase $connection = $this->getTestClient()->getContainer()->get(ManagerRegistry::class)->getConnection(); $this->originalDatabaseUrl = $this->getTestClient()->getContainer()->getParameter('database_url'); - $dbnameSuffix = $this->getTestClient()->getContainer()->getParameter('wallabag_dbname_suffix'); - $tmpDatabaseName = 'wallabag_' . bin2hex(random_bytes(5)); + $tmpDatabaseName = 'wallabag_test_' . bin2hex(random_bytes(5)); if ($connection->getDatabasePlatform() instanceof SqlitePlatform) { - $tmpDatabaseUrl = str_replace('wallabag' . $dbnameSuffix . '.sqlite', $tmpDatabaseName . $dbnameSuffix . '.sqlite', $this->originalDatabaseUrl); + $tmpDatabaseName = $this->getTestClient()->getContainer()->getParameter('kernel.project_dir') . '/data/db/' . $tmpDatabaseName . '.sqlite'; + + /** @see \Doctrine\DBAL\Tools\DsnParser::parse */ + $url = preg_replace('#^((?:pdo-)?sqlite3?):///#', '$1://localhost/', $this->originalDatabaseUrl); + + $tmpDatabaseUrl = (string) (new Uri($url))->withPath($tmpDatabaseName); + + // Add back the leading "/" that was removed by withPath, and remove the "localhost" part + $tmpDatabaseUrl = str_replace('//localhost', '///', $tmpDatabaseUrl); } else { $tmpDatabaseUrl = (string) (new Uri($this->originalDatabaseUrl))->withPath($tmpDatabaseName); } @@ -54,8 +61,7 @@ class InstallCommandTest extends WallabagTestCase if ($connection->getDatabasePlatform() instanceof PostgreSQLPlatform) { // PostgreSQL requires that the database exists before connecting to it - $tmpTestDatabaseName = $tmpDatabaseName . $dbnameSuffix; - $connection->executeQuery('CREATE DATABASE ' . $tmpTestDatabaseName); + $connection->executeQuery('CREATE DATABASE ' . $tmpDatabaseName); } // The environnement has been changed, recreate the client in order to update connection @@ -72,7 +78,10 @@ class InstallCommandTest extends WallabagTestCase if ($connection->getDatabasePlatform() instanceof SqlitePlatform) {// Remove the real environnement variable $_ENV['DATABASE_URL'] = $_SERVER['DATABASE_URL'] = $this->originalDatabaseUrl; - $databasePath = parse_url($databaseUrl, \PHP_URL_PATH); + /** @see \Doctrine\DBAL\Tools\DsnParser::parse */ + $url = preg_replace('#^((?:pdo-)?sqlite3?):///#', '$1://localhost/', $databaseUrl); + + $databasePath = parse_url($url, \PHP_URL_PATH); if (file_exists($databasePath)) { unlink($databasePath);