Skip to content

Commit af816e5

Browse files
authored
[10.x] Fix DB::afterCommit() broken in tests using DatabaseTransactions (#50068)
* Add failing test case for test database transactions manager * Fix transaction commit in test causing all callbacks to execute in transaction
1 parent 97e87b6 commit af816e5

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/Illuminate/Database/DatabaseTransactionsManager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ public function commit($connection, $levelBeingCommitted, $newTransactionLevel)
8383
// shouldn't be any pending transactions, but going to clear them here anyways just
8484
// in case. This method could be refactored to receive a level in the future too.
8585
$this->pendingTransactions = $this->pendingTransactions->reject(
86-
fn ($transaction) => $transaction->connection === $connection
86+
fn ($transaction) => $transaction->connection === $connection &&
87+
$transaction->level >= $levelBeingCommitted
8788
)->values();
8889

8990
[$forThisConnection, $forOtherConnections] = $this->committedTransactions->partition(

tests/Foundation/Testing/DatabaseTransactionsManagerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ public function testItIgnoresTheBaseTransactionForCallbackApplicableTransactions
3131
$this->assertEquals(2, $manager->callbackApplicableTransactions()[0]->level);
3232
}
3333

34+
public function testCommittingDoesNotRemoveTheBasePendingTransaction()
35+
{
36+
$manager = new DatabaseTransactionsManager;
37+
38+
$manager->begin('foo', 1);
39+
40+
$manager->begin('foo', 2);
41+
$manager->commit('foo', 2, 1);
42+
43+
$this->assertCount(0, $manager->callbackApplicableTransactions());
44+
45+
$manager->begin('foo', 2);
46+
47+
$this->assertCount(1, $manager->callbackApplicableTransactions());
48+
$this->assertEquals(2, $manager->callbackApplicableTransactions()[0]->level);
49+
}
50+
3451
public function testItExecutesCallbacksForTheSecondTransaction()
3552
{
3653
$testObject = new TestingDatabaseTransactionsManagerTestObject();

0 commit comments

Comments
 (0)