Skip to content

Commit

Permalink
Adds tests for afterCommit callbacks when using multi-dbs
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysm committed Nov 5, 2024
1 parent cb8ed94 commit 2680624
Showing 1 changed file with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,14 @@

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Support\Facades\DB;
use Orchestra\Testbench\Attributes\WithConfig;

use function Orchestra\Testbench\artisan;

#[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()
{
Expand All @@ -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);
}
}

0 comments on commit 2680624

Please sign in to comment.