From 44ed60011907944c9a1ca164dceb79cc1db624a1 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Fri, 18 Oct 2024 13:51:14 -0300 Subject: [PATCH 1/2] Tweaks the RefreshDatabase trait for better support of multi-dbs When working with multiple databases and some of them are in-memory while others are regular disk-based databases, we usually have to override some of the traits methods to make it work. Also, the database restoration process doesn't handle multiple DBs that well. With these changes, when an app needs to support multiple databases, the user will have to do a few steps: 1. Set the `$connectionsToTransact` protected property on the TestCase listing all connections they use 1. Override the `migrateDatabase` method and call all the migrate:fresh commands for all their connections Restoring the databases between tests should now be completely handled by the trait, no matter which database connection uses in-memory databases or not. --- .../Foundation/Testing/RefreshDatabase.php | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/RefreshDatabase.php b/src/Illuminate/Foundation/Testing/RefreshDatabase.php index 4c4e084ab0fa..31d20a75fd64 100644 --- a/src/Illuminate/Foundation/Testing/RefreshDatabase.php +++ b/src/Illuminate/Foundation/Testing/RefreshDatabase.php @@ -18,7 +18,7 @@ public function refreshDatabase() { $this->beforeRefreshingDatabase(); - if ($this->usingInMemoryDatabase()) { + if ($this->usingInMemoryDatabases()) { $this->restoreInMemoryDatabase(); } @@ -32,11 +32,23 @@ public function refreshDatabase() * * @return bool */ - protected function usingInMemoryDatabase() + protected function usingInMemoryDatabase(string $name) { - $default = config('database.default'); + return config("database.connections.{$name}.database") === ':memory:'; + } - return config("database.connections.$default.database") === ':memory:'; + /** + * Determines if any of the connections transacting is using in-memory databases. + */ + protected function usingInMemoryDatabases(): bool + { + foreach ($this->connectionsToTransact() as $name) { + if ($this->usingInMemoryDatabase($name)) { + return true; + } + } + + return false; } /** @@ -63,7 +75,7 @@ protected function restoreInMemoryDatabase() protected function refreshTestDatabase() { if (! RefreshDatabaseState::$migrated) { - $this->artisan('migrate:fresh', $this->migrateFreshUsing()); + $this->migrateDatabases(); $this->app[Kernel::class]->setArtisan(null); @@ -89,7 +101,7 @@ public function beginDatabaseTransaction() $connection->setTransactionManager($transactionsManager); - if ($this->usingInMemoryDatabase()) { + if ($this->usingInMemoryDatabase($name)) { RefreshDatabaseState::$inMemoryConnections[$name] ??= $connection->getPdo(); } @@ -121,7 +133,15 @@ public function beginDatabaseTransaction() protected function connectionsToTransact() { return property_exists($this, 'connectionsToTransact') - ? $this->connectionsToTransact : [null]; + ? $this->connectionsToTransact : [config('database.default')]; + } + + /** + * Perform the database migration command. + */ + protected function migrateDatabases(): void + { + $this->artisan('migrate:fresh', $this->migrateFreshUsing()); } /** From e57771bf310a15d7f73878bc1ce3eed3c59b8758 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 29 Oct 2024 15:35:08 -0500 Subject: [PATCH 2/2] formatting --- .../Foundation/Testing/RefreshDatabase.php | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/RefreshDatabase.php b/src/Illuminate/Foundation/Testing/RefreshDatabase.php index 31d20a75fd64..f183ef439c26 100644 --- a/src/Illuminate/Foundation/Testing/RefreshDatabase.php +++ b/src/Illuminate/Foundation/Testing/RefreshDatabase.php @@ -28,19 +28,11 @@ public function refreshDatabase() } /** - * Determine if an in-memory database is being used. + * Determine if any of the connections transacting is using in-memory databases. * * @return bool */ - protected function usingInMemoryDatabase(string $name) - { - return config("database.connections.{$name}.database") === ':memory:'; - } - - /** - * Determines if any of the connections transacting is using in-memory databases. - */ - protected function usingInMemoryDatabases(): bool + protected function usingInMemoryDatabases() { foreach ($this->connectionsToTransact() as $name) { if ($this->usingInMemoryDatabase($name)) { @@ -51,6 +43,16 @@ protected function usingInMemoryDatabases(): bool return false; } + /** + * Determine if a given database connection is an in-memory database. + * + * @return bool + */ + protected function usingInMemoryDatabase(string $name) + { + return config("database.connections.{$name}.database") === ':memory:'; + } + /** * Restore the in-memory database between tests. * @@ -85,6 +87,16 @@ protected function refreshTestDatabase() $this->beginDatabaseTransaction(); } + /** + * Migrate the database. + * + * @return void + */ + protected function migrateDatabases() + { + $this->artisan('migrate:fresh', $this->migrateFreshUsing()); + } + /** * Begin a database transaction on the testing database. * @@ -136,14 +148,6 @@ protected function connectionsToTransact() ? $this->connectionsToTransact : [config('database.default')]; } - /** - * Perform the database migration command. - */ - protected function migrateDatabases(): void - { - $this->artisan('migrate:fresh', $this->migrateFreshUsing()); - } - /** * Perform any work that should take place before the database has started refreshing. *