Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Introducing beforeStartingTransaction callback and use it in LazilyRefreshDatabase #49853

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Concerns/ManagesTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ protected function handleTransactionException(Throwable $e, $currentAttempt, $ma
*/
public function beginTransaction()
{
foreach ($this->beforeStartingTransaction as $callback) {
$callback($this);
}

$this->createTransaction();

$this->transactions++;
Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ class Connection implements ConnectionInterface
*/
protected $pretending = false;

/**
* All of the callbacks that should be invoked before a transaction is started.
*
* @var \Closure[]
*/
protected $beforeStartingTransaction = [];

/**
* All of the callbacks that should be invoked before a query is executed.
*
Expand Down Expand Up @@ -1021,6 +1028,19 @@ public function disconnect()
$this->doctrineConnection = null;
}

/**
* Register a hook to be run just before a database transaction is started.
*
* @param \Closure $callback
* @return $this
*/
public function beforeStartingTransaction(Closure $callback)
{
$this->beforeStartingTransaction[] = $callback;

return $this;
}

/**
* Register a hook to be run just before a database query is executed.
*
Expand Down
7 changes: 5 additions & 2 deletions src/Illuminate/Foundation/Testing/LazilyRefreshDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ public function refreshDatabase()
{
$database = $this->app->make('db');

$database->beforeExecuting(function () {
$callback = function () {
if (RefreshDatabaseState::$lazilyRefreshed) {
return;
}

RefreshDatabaseState::$lazilyRefreshed = true;

$this->baseRefreshDatabase();
});
};

$database->beforeStartingTransaction($callback);
$database->beforeExecuting($callback);

$this->beforeApplicationDestroyed(function () {
RefreshDatabaseState::$lazilyRefreshed = false;
Expand Down
12 changes: 12 additions & 0 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,18 @@ public function testBeforeExecutingHooksCanBeRegistered()
$connection->select('foo bar', ['baz']);
}

public function testBeforeStartingTransactionHooksCanBeRegistered()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('The callback was fired');

$connection = $this->getMockConnection();
$connection->beforeStartingTransaction(function () {
throw new Exception('The callback was fired');
});
$connection->beginTransaction();
}

public function testPretendOnlyLogsQueries()
{
$connection = $this->getMockConnection();
Expand Down
Loading