diff --git a/src/Tempest/Database/src/CachedConnectionInitializer.php b/src/Tempest/Database/src/CachedConnectionInitializer.php new file mode 100644 index 000000000..de0fc3b29 --- /dev/null +++ b/src/Tempest/Database/src/CachedConnectionInitializer.php @@ -0,0 +1,35 @@ +initializer->initialize($container); + + return self::$instance; + } +} diff --git a/src/Tempest/Database/src/ConnectionInitializer.php b/src/Tempest/Database/src/ConnectionInitializer.php index 677cc06ce..83d5e587d 100644 --- a/src/Tempest/Database/src/ConnectionInitializer.php +++ b/src/Tempest/Database/src/ConnectionInitializer.php @@ -7,27 +7,17 @@ use Tempest\Container\Container; use Tempest\Container\Initializer; use Tempest\Container\Singleton; -use Tempest\Core\AppConfig; final class ConnectionInitializer implements Initializer { - private static Connection|null $instance = null; - #[Singleton] public function initialize(Container $container): Connection { - // Reuse same connection instance in unit tests - if (self::$instance !== null && $container->get(AppConfig::class)->environment->isTesting()) { - return self::$instance; - } - $databaseConfig = $container->get(DatabaseConfig::class); $connection = new PDOConnection($databaseConfig->connection()); $connection->connect(); - self::$instance = $connection; - return $connection; } } diff --git a/tests/Integration/FrameworkIntegrationTestCase.php b/tests/Integration/FrameworkIntegrationTestCase.php index 97c6e7a1a..95320480d 100644 --- a/tests/Integration/FrameworkIntegrationTestCase.php +++ b/tests/Integration/FrameworkIntegrationTestCase.php @@ -14,6 +14,7 @@ use Tempest\Core\Application; use Tempest\Core\ShellExecutor; use Tempest\Core\ShellExecutors\NullShellExecutor; +use Tempest\Database\CachedConnectionInitializer; use Tempest\Database\DatabaseConfig; use Tempest\Database\Migrations\MigrationManager; use Tempest\Discovery\DiscoveryLocation; @@ -45,6 +46,7 @@ protected function setUp(): void $this->console = new ConsoleTester($this->container); // Database + $this->container->addInitializer(CachedConnectionInitializer::class); $databaseConfigPath = __DIR__ . '/../Fixtures/Config/database.config.php'; if (! file_exists($databaseConfigPath)) {