trait DatabaseTransactions

Traits

Provides per-worker database isolation for parallel testing.

Properties

static protected string|null $originalDatabaseName

The original database name before parallel suffixing.

from  InteractsWithParallelDatabase

Methods

void
configureParallelDatabaseName(mixed $app)

Rewrite the default connection's database name for parallel testing.

void
ensureParallelDatabaseExists()

Ensure the per-worker database exists, creating it if needed.

bool
shouldManageParallelDatabase(mixed $driver, string $database)

Determine if the database should be managed for parallel testing.

string
parallelTestDatabase(string $database, string $token)

Get the per-worker test database name.

void
beginDatabaseTransaction()

Handle database transactions on the specified connections.

void
setUpDatabaseTransactionsInCoroutine()

Start database transaction in the test coroutine.

void
tearDownDatabaseTransactionsInCoroutine()

Rollback database transaction in the test coroutine.

void
beginDatabaseTransactionWork()

Start transactions on all connections.

void
rollbackDatabaseTransactionWork()

Rollback transactions on all connections.

array
connectionsToTransact()

The database connections that should have transactions.

Details

protected void configureParallelDatabaseName(mixed $app)

Rewrite the default connection's database name for parallel testing.

Config-only — does not create connections or purge pools. Called early in CreatesApplication (after config is loaded, before defineEnvironment) so that custom connections derived from the default connection inherit the per-worker database name.

No-op when not running in parallel or when using in-memory SQLite.

Parameters

mixed $app

Return Value

void

protected void ensureParallelDatabaseExists()

Ensure the per-worker database exists, creating it if needed.

Called from database testing traits (RefreshDatabase, DatabaseMigrations, DatabaseTransactions) after the app is booted and connections are available. The config has already been rewritten by configureParallelDatabaseName().

No-op when not running in parallel or when using in-memory SQLite.

Return Value

void

protected bool shouldManageParallelDatabase(mixed $driver, string $database)

Determine if the database should be managed for parallel testing.

Parameters

mixed $driver
string $database

Return Value

bool

protected string parallelTestDatabase(string $database, string $token)

Get the per-worker test database name.

Parameters

string $database
string $token

Return Value

string

at line 25
void beginDatabaseTransaction()

Handle database transactions on the specified connections.

For tests using RunTestsInCoroutine, this method does nothing - the actual transaction work is done in setUpDatabaseTransactionsInCoroutine() to keep all transaction state in the same coroutine.

For non-coroutine tests, this starts the transaction immediately and registers a rollback callback.

Return Value

void

at line 47
protected void setUpDatabaseTransactionsInCoroutine()

Start database transaction in the test coroutine.

Called by RunTestsInCoroutine before the test runs. Keeps all transaction state in the same coroutine, avoiding Context handoff issues.

Return Value

void

at line 57
protected void tearDownDatabaseTransactionsInCoroutine()

Rollback database transaction in the test coroutine.

Called by RunTestsInCoroutine after the test runs.

Return Value

void

at line 65
protected void beginDatabaseTransactionWork()

Start transactions on all connections.

Return Value

void

at line 96
protected void rollbackDatabaseTransactionWork()

Rollback transactions on all connections.

Return Value

void

at line 121
protected array connectionsToTransact()

The database connections that should have transactions.

Return Value

array