diff --git a/src/Illuminate/Foundation/Testing/DatabaseMigrations.php b/src/Illuminate/Foundation/Testing/DatabaseMigrations.php index 4301c24ad473..3b308fe905c6 100644 --- a/src/Illuminate/Foundation/Testing/DatabaseMigrations.php +++ b/src/Illuminate/Foundation/Testing/DatabaseMigrations.php @@ -24,6 +24,11 @@ public function runDatabaseMigrations() $this->artisan('migrate:rollback'); RefreshDatabaseState::$migrated = false; + + $database = $this->app->make('db'); + foreach (array_keys($database->getConnections()) as $name) { + $database->purge($name); + } }); } diff --git a/src/Illuminate/Foundation/Testing/DatabaseTruncation.php b/src/Illuminate/Foundation/Testing/DatabaseTruncation.php index cea2d8d09250..8aac9467757e 100644 --- a/src/Illuminate/Foundation/Testing/DatabaseTruncation.php +++ b/src/Illuminate/Foundation/Testing/DatabaseTruncation.php @@ -26,6 +26,13 @@ protected function truncateDatabaseTables(): void { $this->beforeTruncatingDatabase(); + $this->beforeApplicationDestroyed(function () { + $database = $this->app->make('db'); + foreach (array_keys($database->getConnections()) as $name) { + $database->purge($name); + } + }); + // Migrate and seed the database on first run... if (! RefreshDatabaseState::$migrated) { $this->artisan('migrate:fresh', $this->migrateFreshUsing()); diff --git a/tests/Foundation/Testing/DatabaseMigrationsTest.php b/tests/Foundation/Testing/DatabaseMigrationsTest.php index be759665dd78..faa0e648fc0e 100644 --- a/tests/Foundation/Testing/DatabaseMigrationsTest.php +++ b/tests/Foundation/Testing/DatabaseMigrationsTest.php @@ -3,6 +3,8 @@ namespace Illuminate\Tests\Foundation\Testing; use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract; +use Illuminate\Database\ConnectionInterface; +use Illuminate\Database\DatabaseManager; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole; use Illuminate\Foundation\Testing\DatabaseMigrations; @@ -104,4 +106,19 @@ public function testRefreshTestDatabaseWithDropTypesOption() $this->runDatabaseMigrations(); } + + public function testDisconnectionAfterTestCompletion() + { + $this->app->instance(ConsoleKernelContract::class, m::spy(ConsoleKernel::class)); + $this->app->instance('db', $database = m::mock(DatabaseManager::class)); + + $database->shouldReceive('getConnections')->once()->andReturn([ + 'default' => m::mock(ConnectionInterface::class), + 'mysql' => m::mock(ConnectionInterface::class), + ]); + $database->shouldReceive('purge')->with('default'); + $database->shouldReceive('purge')->with('mysql'); + + $this->runDatabaseMigrations(); + } } diff --git a/tests/Foundation/Testing/DatabaseTruncationTest.php b/tests/Foundation/Testing/DatabaseTruncationTest.php new file mode 100644 index 000000000000..ab40d112e0f7 --- /dev/null +++ b/tests/Foundation/Testing/DatabaseTruncationTest.php @@ -0,0 +1,60 @@ +setUpTheApplicationTestingHooks(); + } + + protected function tearDown(): void + { + $this->tearDownTheApplicationTestingHooks(); + + RefreshDatabaseState::$migrated = false; + } + + protected function refreshApplication() + { + $this->app = Testbench::create( + basePath: package_path('vendor/orchestra/testbench-core/laravel'), + ); + } + + public function testDisconnectionAfterTestCompletion() + { + $this->app->instance(ConsoleKernelContract::class, m::spy(ConsoleKernel::class)); + $this->app->instance('db', $database = m::mock(DatabaseManager::class)); + + $database->shouldReceive('getConnections')->once()->andReturn([ + 'default' => m::mock(ConnectionInterface::class), + 'mysql' => m::mock(ConnectionInterface::class), + ]); + $database->shouldReceive('purge')->with('default'); + $database->shouldReceive('purge')->with('mysql'); + + $this->truncateDatabaseTables(); + } +}