Skip to content

Commit

Permalink
[8.x] Adds new RefreshDatabaseLazily testing trait (#38861)
Browse files Browse the repository at this point in the history
* Adds a new `LazyRefreshDatabase` testing trait and a `QueryExecuting` database event.

* Refactor

* Refactor

* Event test

* Naming change

* Switches out event for an array of callbacks

* Refactor

* Refactor

* Refactor

* Refactor

* Refactor

* formatting

* fix doc block

Co-authored-by: Taylor Otwell <taylorotwell@gmail.com>
  • Loading branch information
lukeraymonddowning and taylorotwell authored Sep 21, 2021
1 parent f267d7c commit 3d1ead4
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
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

0 comments on commit 3d1ead4

Please sign in to comment.