DatabaseConnectionResolver
class DatabaseConnectionResolver extends ConnectionResolver implements FlushableConnectionResolver
Database connection resolver for the testing environment.
Testbench needs stable bare connections across its setup, transaction, and assertion helpers. The resolver therefore retains each borrowed wrapper alongside its bare connection and explicitly discards both at teardown.
Constants
| DEFAULT_CONNECTION_CONTEXT_KEY |
Context key for per-coroutine default connection override. Shared with DatabaseManager::usingConnection() to ensure all access paths respect the override. |
Properties
| protected string|null | $default | The config-derived default connection name, captured at construction. |
from ConnectionResolver |
| protected PoolFactory | $factory | from ConnectionResolver | |
| protected PooledConnection> | $nonCoroutineConnections | Pooled wrappers retained by non-coroutine task execution. |
from ConnectionResolver |
| static protected ConnectionInterface> | $connections | Connections for testing environment. |
|
| static protected ConnectionInterface> | $pooledConnections | Borrowed pooled wrappers that own the cached bare connections. |
|
| static protected int|null | $containerId | The object ID of the container when connections were cached. |
|
| static protected bool | $rebindingRegistered | Whether the dispatcher rebinding hook has been registered. |
Methods
Get a database connection instance.
Release connections retained by non-coroutine task execution.
Discard connections retained by non-coroutine task execution.
Set the default connection name for the current execution context.
Get the context key for storing a connection.
Reset all cached connections to clean state for another setup phase.
Discard all cached connections and clear resolver lifecycle state.
Register a rebinding hook for the event dispatcher.
Resolve the cache key that owns the pooled wrapper.
Flush a cached connection.
Discard every cached pooled wrapper.
Details
in
ConnectionResolver at line 51
__construct(Container $container)
No description
at line 174
ConnectionInterface
connection(UnitEnum|string|null $name = null)
Get a database connection instance.
Creates connections through the pool factory and retains their owning wrappers until terminal test teardown.
in
ConnectionResolver at line 149
void
releaseConnections()
| internal |
Release connections retained by non-coroutine task execution.
in
ConnectionResolver at line 163
void
discardConnections()
| internal |
Discard connections retained by non-coroutine task execution.
in
ConnectionResolver at line 179
string|null
getDefaultConnection()
Get the default connection name.
Checks Context first for per-coroutine override (from setDefaultConnection() or DatabaseManager::usingConnection()), then falls back to the config-derived default captured at construction.
in
ConnectionResolver at line 191
void
setDefaultConnection(string|null $name)
Set the default connection name for the current execution context.
Writes to coroutine Context so concurrent requests in the same Swoole worker are not affected. A null value clears the override and getDefaultConnection() falls back to the config-derived default.
in
ConnectionResolver at line 203
protected string
getContextKey(string $name)
Get the context key for storing a connection.
in
ConnectionResolver at line 211
protected void
terminateConnections(Closure $terminate)
Detach and terminate retained non-coroutine connections.
at line 72
static void
resetCachedConnections()
Reset all cached connections to clean state for another setup phase.
Called after Application is created to prevent test pollution (query logs, event listeners, transaction state, etc.) from leaking between tests.
When the container changes (new test with fresh Application), cached connections are flushed since they hold references to the old container's services. A rebinding hook is registered so Event::fake() automatically updates cached connections with the new dispatcher.
Tests only. The cached connections are process-global, so runtime use can reset or discard a connection borrowed by another coroutine.
at line 98
static void
flushCachedConnections()
Discard all cached connections and clear resolver lifecycle state.
Tests only. The cached connections are process-global, so runtime use can discard a connection borrowed by another coroutine.
at line 111
static protected void
registerDispatcherRebinding(Container $container)
Register a rebinding hook for the event dispatcher.
When Event::fake() swaps the dispatcher, this callback updates all cached connections to use the new (fake) dispatcher.
at line 135
protected string
connectionCacheKey(string $name, DbPool|null $pool = null)
Resolve the cache key that owns the pooled wrapper.
at line 155
void
flush(string $name)
Flush a cached connection.
Discard the owning pooled wrapper before clearing the bare connection.
at line 242
static protected void
discardCachedConnections()
Discard every cached pooled wrapper.