trait DatabaseTruncation

This concern may be combined with RefreshDatabase or DatabaseTransactions; each trait should manage its own connections. It cannot be combined with DatabaseMigrations or LazilyRefreshDatabase.

Traits

Provides per-worker database isolation for parallel testing.

Properties

static protected array $allTables

The cached names of the database tables for each connection.

Methods

array
migrateFreshUsing()

The parameters that should be used when running "migrate:fresh".

bool
shouldDropViews()

Determine if views should be dropped when refreshing the database.

bool
shouldDropTypes()

Determine if types should be dropped when refreshing the database.

bool
shouldSeed()

Determine if the seed task should be run when refreshing the database.

mixed
seeder()

Determine the specific seeder class that should be used when refreshing the database.

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
truncateDatabaseTables()

Truncate the database tables for all configured connections.

void
restoreInMemoryDatabases()

Restore the in-memory databases between tests.

void
cacheInMemoryDatabases()

Cache the in-memory databases after migration.

bool
usingInMemoryDatabasesForTruncation()

Determine if any connection being truncated uses an in-memory database.

bool
hasMissingInMemoryDatabaseForTruncation()

Determine if an in-memory database being truncated is missing its cached PDO.

bool
usingInMemoryDatabaseForTruncation(string|null $name)

Determine if the given connection uses an in-memory database.

void
truncateTablesForAllConnections()

Truncate the database tables for all configured connections.

void
truncateTablesForConnection(ConnectionInterface $connection, string|null $name)

Truncate the database tables for the given database connection.

array
getAllTablesForConnection(ConnectionInterface $connection, string|null $name)

Get all the tables that belong to the connection.

bool
tableExistsIn(array $table, array $tables)

Determine if a table exists in the given list, with or without its schema.

array
connectionsToTruncate()

The database connections that should have their tables truncated.

array|null
tablesToTruncate(ConnectionInterface $connection, string|null $connectionName)

Get the tables that should be truncated.

array
exceptTables(ConnectionInterface $connection, string|null $connectionName)

Get the tables that should not be truncated.

void
beforeTruncatingDatabase()

Perform any work that should take place before the database has started truncating.

void
afterTruncatingDatabase()

Perform any work that should take place once the database has finished truncating.

Details

protected array migrateFreshUsing()

The parameters that should be used when running "migrate:fresh".

Return Value

array

protected bool shouldDropViews()

Determine if views should be dropped when refreshing the database.

Return Value

bool

protected bool shouldDropTypes()

Determine if types should be dropped when refreshing the database.

Return Value

bool

protected bool shouldSeed()

Determine if the seed task should be run when refreshing the database.

Return Value

bool

protected mixed seeder()

Determine the specific seeder class that should be used when refreshing the database.

Return Value

mixed

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 before Testbench database requirements and from database testing traits (RefreshDatabase, DatabaseMigrations, DatabaseTruncation, 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 36
protected void truncateDatabaseTables()

Truncate the database tables for all configured connections.

Return Value

void

at line 101
protected void restoreInMemoryDatabases()

Restore the in-memory databases between tests.

Return Value

void

at line 131
protected void cacheInMemoryDatabases()

Cache the in-memory databases after migration.

Return Value

void

at line 154
protected bool usingInMemoryDatabasesForTruncation()

Determine if any connection being truncated uses an in-memory database.

Return Value

bool

at line 168
protected bool hasMissingInMemoryDatabaseForTruncation()

Determine if an in-memory database being truncated is missing its cached PDO.

Return Value

bool

at line 189
protected bool usingInMemoryDatabaseForTruncation(string|null $name)

Determine if the given connection uses an in-memory database.

Parameters

string|null $name

Return Value

bool

at line 202
protected void truncateTablesForAllConnections()

Truncate the database tables for all configured connections.

Return Value

void

at line 219
protected void truncateTablesForConnection(ConnectionInterface $connection, string|null $name)

Truncate the database tables for the given database connection.

Parameters

ConnectionInterface $connection
string|null $name

Return Value

void

at line 254
protected array getAllTablesForConnection(ConnectionInterface $connection, string|null $name)

Get all the tables that belong to the connection.

Parameters

ConnectionInterface $connection
string|null $name

Return Value

array

at line 268
protected bool tableExistsIn(array $table, array $tables)

Determine if a table exists in the given list, with or without its schema.

Parameters

array $table
array $tables

Return Value

bool

at line 278
protected array connectionsToTruncate()

The database connections that should have their tables truncated.

Return Value

array

at line 288
protected array|null tablesToTruncate(ConnectionInterface $connection, string|null $connectionName)

Get the tables that should be truncated.

Parameters

ConnectionInterface $connection
string|null $connectionName

Return Value

array|null

at line 298
protected array exceptTables(ConnectionInterface $connection, string|null $connectionName)

Get the tables that should not be truncated.

Parameters

ConnectionInterface $connection
string|null $connectionName

Return Value

array

at line 314
protected void beforeTruncatingDatabase()

Perform any work that should take place before the database has started truncating.

Return Value

void

at line 321
protected void afterTruncatingDatabase()

Perform any work that should take place once the database has finished truncating.

Return Value

void