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

[8.x] Adds new RefreshDatabaseLazily testing trait #38861

Merged
merged 13 commits into from
Sep 21, 2021
24 changes: 24 additions & 0 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ class Connection implements ConnectionInterface
*/
protected $pretending = false;

/**
* All of the callbacks that should be invoked before a query is executed.
*
* @var array
*/
protected $beforeExecutingCallbacks = [];

/**
* The instance of Doctrine connection.
*
Expand Down Expand Up @@ -641,6 +648,10 @@ public function prepareBindings(array $bindings)
*/
protected function run($query, $bindings, Closure $callback)
{
foreach ($this->beforeExecutingCallbacks as $beforeExecutingCallback) {
$beforeExecutingCallback($query, $bindings, $this);
}

$this->reconnectIfMissingConnection();

$start = microtime(true);
Expand Down Expand Up @@ -807,6 +818,19 @@ public function disconnect()
$this->setPdo(null)->setReadPdo(null);
}

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

return $this;
}

/**
* Register a database query listener with the connection.
*
Expand Down
34 changes: 34 additions & 0 deletions src/Illuminate/Foundation/Testing/LazilyRefreshDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Illuminate\Foundation\Testing;

trait LazilyRefreshDatabase
{
use RefreshDatabase {
refreshDatabase as baseRefreshDatabase;
}

/**
* Define hooks to migrate the database before and after each test.
*
* @return void
*/
public function refreshDatabase()
{
$database = $this->app->make('db');

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

RefreshDatabaseState::$lazilyRefreshed = true;

$this->baseRefreshDatabase();
});

$this->beforeApplicationDestroyed(function () {
RefreshDatabaseState::$lazilyRefreshed = false;
});
}
}
7 changes: 7 additions & 0 deletions src/Illuminate/Foundation/Testing/RefreshDatabaseState.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ class RefreshDatabaseState
* @var bool
*/
public static $migrated = false;

/**
* Indicates if a lazy refresh hook has been invoked.
*
* @var bool
*/
public static $lazilyRefreshed = false;
}
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* @method static void enableQueryLog()
* @method static void disableQueryLog()
* @method static void flushQueryLog()
* @method static \Illuminate\Database\Connection beforeExecuting(\Closure $callback)
* @method static void listen(\Closure $callback)
* @method static void rollBack(int $toLevel = null)
* @method static void setDefaultConnection(string $name)
Expand Down
11 changes: 11 additions & 0 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,17 @@ public function testLogQueryFiresEventsIfSet()
$connection->logQuery('foo', [], null);
}

public function testBeforeExecutingHooksCanBeRegistered()
{
$this->expectExceptionMessage('The callback was fired');

$connection = $this->getMockConnection();
$connection->beforeExecuting(function () {
throw new Exception('The callback was fired');
});
$connection->select('foo bar', ['baz']);
}

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