Skip to content

Commit

Permalink
[10.x] Fix DB::afterCommit() broken in tests using DatabaseTransactio…
Browse files Browse the repository at this point in the history
…ns (#50068)

* Add failing test case for test database transactions manager

* Fix transaction commit in test causing all callbacks to execute in transaction
  • Loading branch information
oprypkhantc authored Feb 13, 2024
1 parent 97e87b6 commit af816e5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Illuminate/Database/DatabaseTransactionsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public function commit($connection, $levelBeingCommitted, $newTransactionLevel)
// shouldn't be any pending transactions, but going to clear them here anyways just
// in case. This method could be refactored to receive a level in the future too.
$this->pendingTransactions = $this->pendingTransactions->reject(
fn ($transaction) => $transaction->connection === $connection
fn ($transaction) => $transaction->connection === $connection &&
$transaction->level >= $levelBeingCommitted
)->values();

[$forThisConnection, $forOtherConnections] = $this->committedTransactions->partition(
Expand Down
17 changes: 17 additions & 0 deletions tests/Foundation/Testing/DatabaseTransactionsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ public function testItIgnoresTheBaseTransactionForCallbackApplicableTransactions
$this->assertEquals(2, $manager->callbackApplicableTransactions()[0]->level);
}

public function testCommittingDoesNotRemoveTheBasePendingTransaction()
{
$manager = new DatabaseTransactionsManager;

$manager->begin('foo', 1);

$manager->begin('foo', 2);
$manager->commit('foo', 2, 1);

$this->assertCount(0, $manager->callbackApplicableTransactions());

$manager->begin('foo', 2);

$this->assertCount(1, $manager->callbackApplicableTransactions());
$this->assertEquals(2, $manager->callbackApplicableTransactions()[0]->level);
}

public function testItExecutesCallbacksForTheSecondTransaction()
{
$testObject = new TestingDatabaseTransactionsManagerTestObject();
Expand Down

0 comments on commit af816e5

Please sign in to comment.