Skip to content
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

[10.x] Fix DB::afterCommit() broken in tests using DatabaseTransactions #50068

Merged
merged 2 commits into from Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Illuminate/Database/DatabaseTransactionsManager.php
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
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