Skip to content

Commit

Permalink
Disconnecting the database connection after testing (#49327)
Browse files Browse the repository at this point in the history
* Disconnection after testing with traits for database testing

* Revert "Disconnection after testing with traits for database testing"

This reverts commit 71c6653.

* 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 <taylor@laravel.com>
  • Loading branch information
KentarouTakeda and taylorotwell committed Dec 12, 2023
1 parent 9693e32 commit 2f65457
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Testing/DatabaseTransactions.php
Expand Up @@ -33,7 +33,7 @@ public function beginDatabaseTransaction()
$connection->unsetEventDispatcher();
$connection->rollBack();
$connection->setEventDispatcher($dispatcher);
$connection->disconnect();
$database->purge($name);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Testing/RefreshDatabase.php
Expand Up @@ -109,7 +109,7 @@ public function beginDatabaseTransaction()
$connection->unsetEventDispatcher();
$connection->rollBack();
$connection->setEventDispatcher($dispatcher);
$connection->disconnect();
$database->purge($name);
}
});
}
Expand Down
6 changes: 6 additions & 0 deletions src/Illuminate/Foundation/Testing/TestCase.php
Expand Up @@ -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;
Expand Down

0 comments on commit 2f65457

Please sign in to comment.