From 2680624fc7605ba56f7f89b7026ef1c09d418c46 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Tue, 5 Nov 2024 11:44:49 -0300 Subject: [PATCH] Adds tests for afterCommit callbacks when using multi-dbs --- ...freshDatabaseOnMultipleConnectionsTest.php | 46 ++++++++++++++----- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/tests/Integration/Database/EloquentTransactionWithAfterCommitUsingRefreshDatabaseOnMultipleConnectionsTest.php b/tests/Integration/Database/EloquentTransactionWithAfterCommitUsingRefreshDatabaseOnMultipleConnectionsTest.php index 44c215c619f..b09a3f32cd0 100644 --- a/tests/Integration/Database/EloquentTransactionWithAfterCommitUsingRefreshDatabaseOnMultipleConnectionsTest.php +++ b/tests/Integration/Database/EloquentTransactionWithAfterCommitUsingRefreshDatabaseOnMultipleConnectionsTest.php @@ -2,6 +2,7 @@ namespace Illuminate\Tests\Integration\Database; +use Illuminate\Support\Facades\DB; use Orchestra\Testbench\Attributes\WithConfig; use function Orchestra\Testbench\artisan; @@ -9,18 +10,6 @@ #[WithConfig('database.connections.second', ['driver' => 'sqlite', 'database' => ':memory:', 'foreign_key_constraints' => false])] class EloquentTransactionWithAfterCommitUsingRefreshDatabaseOnMultipleConnectionsTest extends EloquentTransactionWithAfterCommitUsingRefreshDatabaseTest { - /** {@inheritDoc} */ - protected function setUp(): void - { - $this->afterApplicationCreated(function () { - $this->markTestSkippedWhen( - $this->usesSqliteInMemoryDatabaseConnection(), 'Skipped when default database is configured to use In-Memory SQLite Database' - ); - }); - - parent::setUp(); - } - /** {@inheritDoc} */ protected function connectionsToTransact() { @@ -32,4 +21,37 @@ protected function afterRefreshingDatabase() { artisan($this, 'migrate', ['--database' => 'second']); } + + public function testAfterCommitCallbacksAreCalledCorrectlyWhenNoAppTransaction() + { + $called = false; + + DB::afterCommit(function () use (&$called) { + $called = true; + }); + + $this->assertTrue($called); + } + + public function testAfterCommitCallbacksAreCalledWithWrappingTransactionsCorrectly() + { + $calls = []; + + DB::transaction(function () use (&$calls) { + DB::afterCommit(function () use (&$calls) { + $calls[] = 'first transaction callback'; + }); + + DB::connection('second')->transaction(function () use (&$calls) { + DB::connection('second')->afterCommit(function () use (&$calls) { + $calls[] = 'second transaction callback'; + }); + }); + }); + + $this->assertEquals([ + 'second transaction callback', + 'first transaction callback', + ], $calls); + } }