DatabaseTransactionsManager
class DatabaseTransactionsManager extends DatabaseTransactionsManager
Testing-aware transaction manager that properly handles RefreshDatabase's wrapper transaction.
When RefreshDatabase wraps tests in a transaction, this manager ensures that afterCommit callbacks execute at the appropriate time (when returning to the wrapper level) rather than waiting for level 0 which never happens during wrapped tests.
Constants
| protected COMMITTED_CONTEXT_KEY |
|
| protected PENDING_CONTEXT_KEY |
|
| protected CURRENT_CONTEXT_KEY |
|
Properties
| protected array<int, null|string> | $connectionsTransacting | The names of the connections transacting during tests. |
Methods
Get all committed transactions for the current coroutine.
Set committed transactions for the current coroutine.
Get all pending transactions for the current coroutine.
Set pending transactions for the current coroutine.
Get current transaction map for the current coroutine.
Set current transaction for a connection.
Get current transaction for a connection.
Start a new database transaction.
Commit the root database transaction and execute callbacks.
Move relevant pending transactions to a committed state.
Rollback the active database transaction.
Remove all pending, completed, and current transactions for the given connection name.
Remove and return all committed descendants of the given transaction.
Execute commit callbacks for every detached transaction.
Execute rollback callbacks for every detached transaction.
Register a transaction callback.
Register a callback for transaction rollback.
Get the latest applicable transaction, optionally limited to a connection.
Get the transactions that are applicable to callbacks.
Determine if after commit callbacks should be executed for the given transaction level.
Get all of the pending transactions.
Get all of the committed transactions.
Check whether there are pending transactions in non-coroutine storage.
Copy transaction state from the current coroutine to non-coroutine storage.
Clear all transaction state from non-coroutine storage.
Create a new database transaction manager instance.
Details
in
DatabaseTransactionsManager at line 31
protected Collection
getCommittedTransactionsInternal()
Get all committed transactions for the current coroutine.
in
DatabaseTransactionsManager at line 41
protected void
setCommittedTransactions(Collection $transactions)
Set committed transactions for the current coroutine.
in
DatabaseTransactionsManager at line 51
protected Collection
getPendingTransactionsInternal()
Get all pending transactions for the current coroutine.
in
DatabaseTransactionsManager at line 61
protected void
setPendingTransactions(Collection $transactions)
Set pending transactions for the current coroutine.
in
DatabaseTransactionsManager at line 71
protected array
getCurrentTransaction()
Get current transaction map for the current coroutine.
in
DatabaseTransactionsManager at line 79
protected void
setCurrentTransactionForConnection(string $connection, DatabaseTransactionRecord|null $transaction)
Set current transaction for a connection.
in
DatabaseTransactionsManager at line 89
protected DatabaseTransactionRecord|null
getCurrentTransactionForConnection(string $connection)
Get current transaction for a connection.
in
DatabaseTransactionsManager at line 97
void
begin(string $connection, int $level)
Start a new database transaction.
in
DatabaseTransactionsManager at line 117
Collection
commit(string $connection, int $levelBeingCommitted, int $newTransactionLevel)
Commit the root database transaction and execute callbacks.
in
DatabaseTransactionsManager at line 153
void
stageTransactions(string $connection, int $levelBeingCommitted)
Move relevant pending transactions to a committed state.
in
DatabaseTransactionsManager at line 176
void
rollback(string $connection, int $newTransactionLevel)
Rollback the active database transaction.
in
DatabaseTransactionsManager at line 224
protected void
removeAllTransactionsForConnection(string $connection)
Remove all pending, completed, and current transactions for the given connection name.
in
DatabaseTransactionsManager at line 264
protected Collection
removeCommittedTransactionsThatAreChildrenOf(DatabaseTransactionRecord $transaction)
Remove and return all committed descendants of the given transaction.
in
DatabaseTransactionsManager at line 290
protected void
executeCommitCallbacks(Collection $transactions)
Execute commit callbacks for every detached transaction.
in
DatabaseTransactionsManager at line 314
protected void
executeRollbackCallbacks(Collection $transactions)
Execute rollback callbacks for every detached transaction.
in
DatabaseTransactionsManager at line 347
void
addCallback(callable $callback, string|null $connection = null)
Register a transaction callback.
A null connection selects the most recently started applicable transaction on any connection. The callback runs when that transaction and its enclosing stack on the same connection commit.
in
DatabaseTransactionsManager at line 366
void
addCallbackForRollback(callable $callback, string|null $connection = null)
Register a callback for transaction rollback.
A null connection selects the most recently started applicable transaction on any connection. The callback runs when that transaction and its enclosing stack on the same connection roll back.
in
DatabaseTransactionsManager at line 376
protected DatabaseTransactionRecord|null
latestApplicableTransaction(string|null $connection)
Get the latest applicable transaction, optionally limited to a connection.
at line 45
Collection
callbackApplicableTransactions()
Get the transactions that are applicable to callbacks.
Skips the RefreshDatabase wrapper transaction(s) so callbacks are only associated with transactions created within the test itself.
at line 56
bool
afterCommitCallbacksShouldBeExecuted(int $level)
Determine if after commit callbacks should be executed for the given transaction level.
Returns true at level 1 (the RefreshDatabase wrapper level) instead of level 0, since the wrapper transaction is never committed during tests.
in
DatabaseTransactionsManager at line 418
Collection
getPendingTransactions()
Get all of the pending transactions.
in
DatabaseTransactionsManager at line 428
Collection
getCommittedTransactions()
Get all of the committed transactions.
in
DatabaseTransactionsManager at line 436
static bool
hasNonCoroutinePendingTransactions()
Check whether there are pending transactions in non-coroutine storage.
in
DatabaseTransactionsManager at line 449
static void
copyToNonCoroutineState()
Copy transaction state from the current coroutine to non-coroutine storage.
Tests only. This copies coroutine-local transaction state into worker-global storage, so request-time use can leak or race across concurrent requests.
in
DatabaseTransactionsManager at line 464
static void
clearNonCoroutineState()
Clear all transaction state from non-coroutine storage.
Tests only. This mutates worker-global transaction state, so request-time use can race with concurrent requests using the same storage.
at line 32
__construct(array $connectionsTransacting)
Create a new database transaction manager instance.