diff --git a/src/Test/DatabaseResetter.php b/src/Test/DatabaseResetter.php index 39cd9f6c1..1be8d38c8 100644 --- a/src/Test/DatabaseResetter.php +++ b/src/Test/DatabaseResetter.php @@ -26,20 +26,21 @@ final class DatabaseResetter { private static bool $hasBeenReset = false; + private static bool $isDAMADoctrineTestBundleEnabled = false; public static function hasBeenReset(): bool { return self::$hasBeenReset; } - public static function isDAMADoctrineTestBundleAvailable(): bool + public static function isDAMADoctrineTestBundleEnabled(bool $cached = false): bool { - return \class_exists(StaticDriver::class); - } + if($cached) { + return self::$isDAMADoctrineTestBundleEnabled; + } - public static function isDAMADoctrineTestBundleEnabled(): bool - { - return \class_exists(StaticDriver::class) && StaticDriver::isKeepStaticConnections(); + self::$isDAMADoctrineTestBundleEnabled = \class_exists(StaticDriver::class) && StaticDriver::isKeepStaticConnections(); + return self::$isDAMADoctrineTestBundleEnabled; } public static function resetDatabase(KernelInterface $kernel, bool $damaIsEnabled): void diff --git a/src/Test/ORMDatabaseResetter.php b/src/Test/ORMDatabaseResetter.php index c8540146d..bda309cfa 100644 --- a/src/Test/ORMDatabaseResetter.php +++ b/src/Test/ORMDatabaseResetter.php @@ -11,6 +11,7 @@ namespace Zenstruck\Foundry\Test; +use Doctrine\DBAL\Connection; use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Platforms\SqlitePlatform; use Doctrine\Persistence\ManagerRegistry; @@ -46,12 +47,14 @@ public function __construct(private Application $application, private ManagerReg public function resetDatabase(): void { - if (DatabaseResetter::isDAMADoctrineTestBundleAvailable() && $this->isResetUsingMigrations()) { + if (DatabaseResetter::isDAMADoctrineTestBundleEnabled(true) && $this->isResetUsingMigrations()) { try{ $this->runCommand($this->application, 'doctrine:migrations:up-to-date', ['--fail-on-unregistered' => true]); - // not required as the database schema is already up-to-date + $this->truncateAllTables(); + + // not required as the database schema is already up-to-date and all tables were truncated return; - }catch(\RuntimeException $e){ + }catch(\Throwable $e){ } } @@ -180,4 +183,37 @@ private function isResetUsingMigrations(): bool return self::RESET_MODE_MIGRATE === $this->resetMode; } + + + private function truncateAllTables(): void { + foreach ($this->connectionsToReset() as $connectionName) { + /** @var Connection $connection */ + $connection = $this->registry->getConnection($connectionName); + $databasePlatform = $connection->getDatabasePlatform(); + $truncateStatement = $databasePlatform->getTruncateTableSQL("%s"); + + switch($databasePlatform->getName()){ + case 'postgresql': $connection->executeStatement('SET CONSTRAINTS ALL DEFERRED;'); break; + case 'mysql': $connection->executeStatement('SET FOREIGN_KEY_CHECKS = 0;'); break; + } + + $tables = $connection->getSchemaManager()->listTableNames(); + $connection->beginTransaction(); + foreach ($tables as $tableName) + { + if ($tableName === 'doctrine_migration_versions') { + continue; + } + $connection->executeStatement(sprintf($truncateStatement, $tableName)); + } + $connection->commit(); + + switch($databasePlatform->getName()){ + case 'postgresql': $connection->executeStatement('SET CONSTRAINTS ALL IMMEDIATE;'); break; + case 'mysql': $connection->executeStatement('SET FOREIGN_KEY_CHECKS = 1;'); break; + } + + unset($connection); + } + } }