Skip to content

[11.x] Skip the number of connections transacting while testing to run callbacks #53377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@

class DatabaseTransactionsManager extends BaseManager
{
/**
* Number of connections transacting on tests to be skiped and run the callbacks correctly.
*/
protected int $connectionsTransacting = 1;

/**
* @param int $connectionsTransacting Number of root connections transacting on tests (to skip for callbacks).
*/
public function __construct(int $connectionsTransacting = 1)
{
parent::__construct();

$this->connectionsTransacting = $connectionsTransacting;
}

/**
* Register a transaction callback.
*
Expand All @@ -31,7 +46,7 @@ public function addCallback($callback)
*/
public function callbackApplicableTransactions()
{
return $this->pendingTransactions->skip(1)->values();
return $this->pendingTransactions->skip($this->connectionsTransacting)->values();
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Foundation/Testing/RefreshDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ public function beginDatabaseTransaction()
{
$database = $this->app->make('db');

$this->app->instance('db.transactions', $transactionsManager = new DatabaseTransactionsManager);
$connections = $this->connectionsToTransact();

foreach ($this->connectionsToTransact() as $name) {
$this->app->instance('db.transactions', $transactionsManager = new DatabaseTransactionsManager(count($connections)));

foreach ($connections as $name) {
$connection = $database->connection($name);

$connection->setTransactionManager($transactionsManager);
Expand Down