Skip to content

Commit

Permalink
[10.x] Introducing beforeStartingTransaction callback and use it in…
Browse files Browse the repository at this point in the history
… `LazilyRefreshDatabase` (#49853)

* Introduced `beforeStartingTransaction` on `Connection`

* Use new `beforeStartingTransaction ` in `LazilyRefreshDatabase`

* Fix formatting

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
pascalbaljet and taylorotwell committed Jan 26, 2024
1 parent 7a567c4 commit c9de94a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
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

0 comments on commit c9de94a

Please sign in to comment.