Skip to content

DB::afterCommit() callbacks and after commit jobs are incorrectly executed in the transaction in tests #50066

@oprypkhantc

Description

@oprypkhantc

Laravel Version

10.43.0

PHP Version

8.2.x

Database Driver & Version

No response

Description

Hey.

DB::afterCommit() and subsequently "after commit" jobs (and notifications, listeners, events etc) are incorrectly immediately executed when inside of a transaction in a PHPUnit test that uses DatabaseTransactions if the test has committed a transaction before:

class SomeTest extends TestCase {
	use DatabaseTransactions;

	public function testSomething(): void
	{
		DB::transaction(function () {});

		DB::transaction(function () {
			// This line should only execute after this closure
			DB::afterCommit(fn () => dd(123));
			
			// This line should execute first, before the line above
			dd(456);
		});
	}
}

which prints 123.

Steps To Reproduce

  1. Copy the test case above into a Laravel ^10.34 application and run it

Actual result is 123
Expected result is 456

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions