From 2f65457e6fc4f17c751fadc71bd2ca93e593cf28 Mon Sep 17 00:00:00 2001 From: KentarouTakeda Date: Wed, 13 Dec 2023 01:35:08 +0900 Subject: [PATCH] Disconnecting the database connection after testing (#49327) * Disconnection after testing with traits for database testing * Revert "Disconnection after testing with traits for database testing" This reverts commit 71c6653640d56d65d88d5addc85731974430e6d0. * Disconnection at teardown * No need to disconnect for individual traits * fix: Behavior when run without database * Avoid changing the behavior before fixing as much as possible. * Update TestCase.php --------- Co-authored-by: Taylor Otwell --- src/Illuminate/Foundation/Testing/DatabaseTransactions.php | 2 +- src/Illuminate/Foundation/Testing/RefreshDatabase.php | 2 +- src/Illuminate/Foundation/Testing/TestCase.php | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/DatabaseTransactions.php b/src/Illuminate/Foundation/Testing/DatabaseTransactions.php index 83a686f3558c..c6fb714ada4b 100644 --- a/src/Illuminate/Foundation/Testing/DatabaseTransactions.php +++ b/src/Illuminate/Foundation/Testing/DatabaseTransactions.php @@ -33,7 +33,7 @@ public function beginDatabaseTransaction() $connection->unsetEventDispatcher(); $connection->rollBack(); $connection->setEventDispatcher($dispatcher); - $connection->disconnect(); + $database->purge($name); } }); } diff --git a/src/Illuminate/Foundation/Testing/RefreshDatabase.php b/src/Illuminate/Foundation/Testing/RefreshDatabase.php index 0f916ac55b51..c68b19bbd54d 100644 --- a/src/Illuminate/Foundation/Testing/RefreshDatabase.php +++ b/src/Illuminate/Foundation/Testing/RefreshDatabase.php @@ -109,7 +109,7 @@ public function beginDatabaseTransaction() $connection->unsetEventDispatcher(); $connection->rollBack(); $connection->setEventDispatcher($dispatcher); - $connection->disconnect(); + $database->purge($name); } }); } diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index a7315a2c5124..e38e65430a08 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -197,6 +197,12 @@ protected function tearDown(): void ParallelTesting::callTearDownTestCaseCallbacks($this); + $database = $this->app['db'] ?? null; + + foreach (array_keys($database?->getConnections() ?? []) as $name) { + $database->purge($name); + } + $this->app->flush(); $this->app = null;