From 08a0ebc970bc0dec8d39eaaf3d6458754f3ef3d5 Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Sat, 18 Sep 2021 12:13:29 +0100 Subject: [PATCH 01/13] Adds a new `LazyRefreshDatabase` testing trait and a `QueryExecuting` database event. --- src/Illuminate/Database/Connection.php | 3 + .../Database/Events/QueryExecuting.php | 50 +++++ .../Testing/LazyRefreshDatabase.php | 189 ++++++++++++++++++ .../Testing/RefreshDatabaseState.php | 7 + .../Foundation/Testing/TestCase.php | 4 + tests/Database/DatabaseConnectionTest.php | 1 + 6 files changed, 254 insertions(+) create mode 100644 src/Illuminate/Database/Events/QueryExecuting.php create mode 100644 src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php diff --git a/src/Illuminate/Database/Connection.php b/src/Illuminate/Database/Connection.php index c3b148d36fe8..af74ba9a222f 100755 --- a/src/Illuminate/Database/Connection.php +++ b/src/Illuminate/Database/Connection.php @@ -8,6 +8,7 @@ use Exception; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Events\QueryExecuted; +use Illuminate\Database\Events\QueryExecuting; use Illuminate\Database\Events\StatementPrepared; use Illuminate\Database\Events\TransactionBeginning; use Illuminate\Database\Events\TransactionCommitted; @@ -641,6 +642,8 @@ public function prepareBindings(array $bindings) */ protected function run($query, $bindings, Closure $callback) { + $this->event(new QueryExecuting($query, $bindings, $this)); + $this->reconnectIfMissingConnection(); $start = microtime(true); diff --git a/src/Illuminate/Database/Events/QueryExecuting.php b/src/Illuminate/Database/Events/QueryExecuting.php new file mode 100644 index 000000000000..686e6e665a4f --- /dev/null +++ b/src/Illuminate/Database/Events/QueryExecuting.php @@ -0,0 +1,50 @@ +sql = $sql; + $this->bindings = $bindings; + $this->connection = $connection; + $this->connectionName = $connection->getName(); + } +} diff --git a/src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php b/src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php new file mode 100644 index 000000000000..50b6c6a1527e --- /dev/null +++ b/src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php @@ -0,0 +1,189 @@ +app->make('events'); + + $events->listen(QueryExecuting::class, function () { + if (RefreshDatabaseState::$lazilyRefreshed) { + return; + } + + RefreshDatabaseState::$lazilyRefreshed = true; + + $this->usingInMemoryDatabase() + ? $this->refreshInMemoryDatabase() + : $this->refreshTestDatabase(); + }); + + $this->beforeApplicationDestroyed(function () { + RefreshDatabaseState::$lazilyRefreshed = false; + }); + } + + /** + * Determine if an in-memory database is being used. + * + * @return bool + */ + protected function usingInMemoryDatabase() + { + $default = config('database.default'); + + return config("database.connections.$default.database") === ':memory:'; + } + + /** + * Refresh the in-memory database. + * + * @return void + */ + protected function refreshInMemoryDatabase() + { + $this->artisan('migrate', $this->migrateUsing()); + + $this->app[Kernel::class]->setArtisan(null); + } + + /** + * The parameters that should be used when running "migrate". + * + * @return array + */ + protected function migrateUsing() + { + return [ + '--seed' => $this->shouldSeed(), + ]; + } + + /** + * Refresh a conventional test database. + * + * @return void + */ + protected function refreshTestDatabase() + { + if (! RefreshDatabaseState::$migrated) { + $this->artisan('migrate:fresh', $this->migrateFreshUsing()); + + $this->app[Kernel::class]->setArtisan(null); + + RefreshDatabaseState::$migrated = true; + } + + $this->beginDatabaseTransaction(); + } + + /** + * The parameters that should be used when running "migrate:fresh". + * + * @return array + */ + protected function migrateFreshUsing() + { + $seeder = $this->seeder(); + + return array_merge( + [ + '--drop-views' => $this->shouldDropViews(), + '--drop-types' => $this->shouldDropTypes(), + ], + $seeder ? ['--seeder' => $seeder] : ['--seed' => $this->shouldSeed()] + ); + } + + /** + * Begin a database transaction on the testing database. + * + * @return void + */ + public function beginDatabaseTransaction() + { + $database = $this->app->make('db'); + + foreach ($this->connectionsToTransact() as $name) { + $connection = $database->connection($name); + $dispatcher = $connection->getEventDispatcher(); + + $connection->unsetEventDispatcher(); + $connection->beginTransaction(); + $connection->setEventDispatcher($dispatcher); + } + + $this->beforeApplicationDestroyed(function () use ($database) { + foreach ($this->connectionsToTransact() as $name) { + $connection = $database->connection($name); + $dispatcher = $connection->getEventDispatcher(); + + $connection->unsetEventDispatcher(); + $connection->rollback(); + $connection->setEventDispatcher($dispatcher); + $connection->disconnect(); + } + }); + } + + /** + * The database connections that should have transactions. + * + * @return array + */ + protected function connectionsToTransact() + { + return property_exists($this, 'connectionsToTransact') + ? $this->connectionsToTransact : [null]; + } + + /** + * Determine if views should be dropped when refreshing the database. + * + * @return bool + */ + protected function shouldDropViews() + { + return property_exists($this, 'dropViews') ? $this->dropViews : false; + } + + /** + * Determine if types should be dropped when refreshing the database. + * + * @return bool + */ + protected function shouldDropTypes() + { + return property_exists($this, 'dropTypes') ? $this->dropTypes : false; + } + + /** + * Determine if the seed task should be run when refreshing the database. + * + * @return bool + */ + protected function shouldSeed() + { + return property_exists($this, 'seed') ? $this->seed : false; + } + + /** + * Determine the specific seeder class that should be used when refreshing the database. + * + * @return mixed + */ + protected function seeder() + { + return property_exists($this, 'seeder') ? $this->seeder : false; + } +} diff --git a/src/Illuminate/Foundation/Testing/RefreshDatabaseState.php b/src/Illuminate/Foundation/Testing/RefreshDatabaseState.php index 1f33087396f6..1cb0e996d3b0 100644 --- a/src/Illuminate/Foundation/Testing/RefreshDatabaseState.php +++ b/src/Illuminate/Foundation/Testing/RefreshDatabaseState.php @@ -10,4 +10,11 @@ class RefreshDatabaseState * @var bool */ public static $migrated = false; + + /** + * Indicates if a lazy hook has been fired. + * + * @var bool + */ + public static $lazilyRefreshed = false; } diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index ee19a864b591..40fedc9c4c37 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -121,6 +121,10 @@ protected function setUpTraits() $this->refreshDatabase(); } + if (isset($uses[LazyRefreshDatabase::class])) { + $this->refreshDatabaseLazily(); + } + if (isset($uses[DatabaseMigrations::class])) { $this->runDatabaseMigrations(); } diff --git a/tests/Database/DatabaseConnectionTest.php b/tests/Database/DatabaseConnectionTest.php index 9b160371de67..7954f864dc75 100755 --- a/tests/Database/DatabaseConnectionTest.php +++ b/tests/Database/DatabaseConnectionTest.php @@ -8,6 +8,7 @@ use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Connection; use Illuminate\Database\Events\QueryExecuted; +use Illuminate\Database\Events\QueryExecuting; use Illuminate\Database\Events\TransactionBeginning; use Illuminate\Database\Events\TransactionCommitted; use Illuminate\Database\Events\TransactionRolledBack; From eda8bedb8421df89195ea9d42a75129163e99f8c Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Sat, 18 Sep 2021 12:24:37 +0100 Subject: [PATCH 02/13] Refactor --- .../Testing/LazyRefreshDatabase.php | 161 +----------------- .../Foundation/Testing/TestCase.php | 6 +- 2 files changed, 6 insertions(+), 161 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php b/src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php index 50b6c6a1527e..445af439d156 100644 --- a/src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php +++ b/src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php @@ -2,17 +2,20 @@ namespace Illuminate\Foundation\Testing; -use Illuminate\Contracts\Console\Kernel; use Illuminate\Database\Events\QueryExecuting; trait LazyRefreshDatabase { + use RefreshDatabase { + refreshDatabase as standardRefreshDatabase; + } + /** * Define hooks to migrate the database before and after each test. * * @return void */ - public function refreshDatabaseLazily() + public function refreshDatabase() { $events = $this->app->make('events'); @@ -32,158 +35,4 @@ public function refreshDatabaseLazily() RefreshDatabaseState::$lazilyRefreshed = false; }); } - - /** - * Determine if an in-memory database is being used. - * - * @return bool - */ - protected function usingInMemoryDatabase() - { - $default = config('database.default'); - - return config("database.connections.$default.database") === ':memory:'; - } - - /** - * Refresh the in-memory database. - * - * @return void - */ - protected function refreshInMemoryDatabase() - { - $this->artisan('migrate', $this->migrateUsing()); - - $this->app[Kernel::class]->setArtisan(null); - } - - /** - * The parameters that should be used when running "migrate". - * - * @return array - */ - protected function migrateUsing() - { - return [ - '--seed' => $this->shouldSeed(), - ]; - } - - /** - * Refresh a conventional test database. - * - * @return void - */ - protected function refreshTestDatabase() - { - if (! RefreshDatabaseState::$migrated) { - $this->artisan('migrate:fresh', $this->migrateFreshUsing()); - - $this->app[Kernel::class]->setArtisan(null); - - RefreshDatabaseState::$migrated = true; - } - - $this->beginDatabaseTransaction(); - } - - /** - * The parameters that should be used when running "migrate:fresh". - * - * @return array - */ - protected function migrateFreshUsing() - { - $seeder = $this->seeder(); - - return array_merge( - [ - '--drop-views' => $this->shouldDropViews(), - '--drop-types' => $this->shouldDropTypes(), - ], - $seeder ? ['--seeder' => $seeder] : ['--seed' => $this->shouldSeed()] - ); - } - - /** - * Begin a database transaction on the testing database. - * - * @return void - */ - public function beginDatabaseTransaction() - { - $database = $this->app->make('db'); - - foreach ($this->connectionsToTransact() as $name) { - $connection = $database->connection($name); - $dispatcher = $connection->getEventDispatcher(); - - $connection->unsetEventDispatcher(); - $connection->beginTransaction(); - $connection->setEventDispatcher($dispatcher); - } - - $this->beforeApplicationDestroyed(function () use ($database) { - foreach ($this->connectionsToTransact() as $name) { - $connection = $database->connection($name); - $dispatcher = $connection->getEventDispatcher(); - - $connection->unsetEventDispatcher(); - $connection->rollback(); - $connection->setEventDispatcher($dispatcher); - $connection->disconnect(); - } - }); - } - - /** - * The database connections that should have transactions. - * - * @return array - */ - protected function connectionsToTransact() - { - return property_exists($this, 'connectionsToTransact') - ? $this->connectionsToTransact : [null]; - } - - /** - * Determine if views should be dropped when refreshing the database. - * - * @return bool - */ - protected function shouldDropViews() - { - return property_exists($this, 'dropViews') ? $this->dropViews : false; - } - - /** - * Determine if types should be dropped when refreshing the database. - * - * @return bool - */ - protected function shouldDropTypes() - { - return property_exists($this, 'dropTypes') ? $this->dropTypes : false; - } - - /** - * Determine if the seed task should be run when refreshing the database. - * - * @return bool - */ - protected function shouldSeed() - { - return property_exists($this, 'seed') ? $this->seed : false; - } - - /** - * Determine the specific seeder class that should be used when refreshing the database. - * - * @return mixed - */ - protected function seeder() - { - return property_exists($this, 'seeder') ? $this->seeder : false; - } } diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index 40fedc9c4c37..31e7de08aaf9 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -117,14 +117,10 @@ protected function setUpTraits() { $uses = array_flip(class_uses_recursive(static::class)); - if (isset($uses[RefreshDatabase::class])) { + if (isset($uses[RefreshDatabase::class]) || isset($uses[LazyRefreshDatabase::class])) { $this->refreshDatabase(); } - if (isset($uses[LazyRefreshDatabase::class])) { - $this->refreshDatabaseLazily(); - } - if (isset($uses[DatabaseMigrations::class])) { $this->runDatabaseMigrations(); } From 6d6fcef70cb1a5b80f5f62de2aafbe8dc57c7d14 Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Sat, 18 Sep 2021 17:21:38 +0100 Subject: [PATCH 03/13] Refactor --- src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php b/src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php index 445af439d156..7baba5c76eb7 100644 --- a/src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php +++ b/src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php @@ -26,9 +26,7 @@ public function refreshDatabase() RefreshDatabaseState::$lazilyRefreshed = true; - $this->usingInMemoryDatabase() - ? $this->refreshInMemoryDatabase() - : $this->refreshTestDatabase(); + $this->standardRefreshDatabase(); }); $this->beforeApplicationDestroyed(function () { From 7b32f47ca701c0e87c9ead848fa56db922251947 Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Sat, 18 Sep 2021 17:30:40 +0100 Subject: [PATCH 04/13] Event test --- tests/Database/DatabaseConnectionTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Database/DatabaseConnectionTest.php b/tests/Database/DatabaseConnectionTest.php index 7954f864dc75..1262892da075 100755 --- a/tests/Database/DatabaseConnectionTest.php +++ b/tests/Database/DatabaseConnectionTest.php @@ -407,6 +407,18 @@ public function testLogQueryFiresEventsIfSet() $connection->logQuery('foo', [], null); } + public function testAnEventIsFiredBeforeAndAfterQuerying() + { + $connection = $this->getMockConnection(); + $connection->setEventDispatcher($events = m::mock(Dispatcher::class)); + $events->shouldReceive('dispatch')->once()->with(m::type(QueryExecuting::class)); + $events->shouldReceive('dispatch')->once()->with(m::type(QueryExecuted::class)); + + $connection->pretend(function ($connection) { + $connection->select('foo bar', ['baz']); + }); + } + public function testPretendOnlyLogsQueries() { $connection = $this->getMockConnection(); From f0ad0a4874a04a6ef7635c64046c3ef72daf6488 Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Sat, 18 Sep 2021 17:32:30 +0100 Subject: [PATCH 05/13] Naming change --- .../{LazyRefreshDatabase.php => RefreshDatabaseLazily.php} | 2 +- src/Illuminate/Foundation/Testing/TestCase.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/Illuminate/Foundation/Testing/{LazyRefreshDatabase.php => RefreshDatabaseLazily.php} (96%) diff --git a/src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php b/src/Illuminate/Foundation/Testing/RefreshDatabaseLazily.php similarity index 96% rename from src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php rename to src/Illuminate/Foundation/Testing/RefreshDatabaseLazily.php index 7baba5c76eb7..2fa8fa9c5273 100644 --- a/src/Illuminate/Foundation/Testing/LazyRefreshDatabase.php +++ b/src/Illuminate/Foundation/Testing/RefreshDatabaseLazily.php @@ -4,7 +4,7 @@ use Illuminate\Database\Events\QueryExecuting; -trait LazyRefreshDatabase +trait RefreshDatabaseLazily { use RefreshDatabase { refreshDatabase as standardRefreshDatabase; diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index 31e7de08aaf9..8caeb701df05 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -117,7 +117,7 @@ protected function setUpTraits() { $uses = array_flip(class_uses_recursive(static::class)); - if (isset($uses[RefreshDatabase::class]) || isset($uses[LazyRefreshDatabase::class])) { + if (isset($uses[RefreshDatabase::class]) || isset($uses[RefreshDatabaseLazily::class])) { $this->refreshDatabase(); } From b9c4edb4fcec224d9ace8c78d6d3350c7a4e88c6 Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Sun, 19 Sep 2021 08:34:51 +0100 Subject: [PATCH 06/13] Switches out event for an array of callbacks --- src/Illuminate/Database/Connection.php | 22 +++++++- .../Database/Events/QueryExecuting.php | 50 ------------------- .../Testing/RefreshDatabaseLazily.php | 4 +- src/Illuminate/Support/Facades/DB.php | 1 + .../ParallelTestingServiceProvider.php | 2 +- tests/Database/DatabaseConnectionTest.php | 13 +++-- 6 files changed, 31 insertions(+), 61 deletions(-) delete mode 100644 src/Illuminate/Database/Events/QueryExecuting.php diff --git a/src/Illuminate/Database/Connection.php b/src/Illuminate/Database/Connection.php index af74ba9a222f..60db855384f4 100755 --- a/src/Illuminate/Database/Connection.php +++ b/src/Illuminate/Database/Connection.php @@ -162,6 +162,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. * @@ -642,7 +649,9 @@ public function prepareBindings(array $bindings) */ protected function run($query, $bindings, Closure $callback) { - $this->event(new QueryExecuting($query, $bindings, $this)); + foreach ($this->beforeExecutingCallbacks as $beforeExecutingCallback) { + $beforeExecutingCallback($query, $bindings, $this); + } $this->reconnectIfMissingConnection(); @@ -810,6 +819,17 @@ 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 void + */ + public function beforeExecuting(Closure $callback) + { + $this->beforeExecutingCallbacks[] = $callback; + } + /** * Register a database query listener with the connection. * diff --git a/src/Illuminate/Database/Events/QueryExecuting.php b/src/Illuminate/Database/Events/QueryExecuting.php deleted file mode 100644 index 686e6e665a4f..000000000000 --- a/src/Illuminate/Database/Events/QueryExecuting.php +++ /dev/null @@ -1,50 +0,0 @@ -sql = $sql; - $this->bindings = $bindings; - $this->connection = $connection; - $this->connectionName = $connection->getName(); - } -} diff --git a/src/Illuminate/Foundation/Testing/RefreshDatabaseLazily.php b/src/Illuminate/Foundation/Testing/RefreshDatabaseLazily.php index 2fa8fa9c5273..16775b1c8313 100644 --- a/src/Illuminate/Foundation/Testing/RefreshDatabaseLazily.php +++ b/src/Illuminate/Foundation/Testing/RefreshDatabaseLazily.php @@ -17,9 +17,9 @@ trait RefreshDatabaseLazily */ public function refreshDatabase() { - $events = $this->app->make('events'); + $database = $this->app->make('db'); - $events->listen(QueryExecuting::class, function () { + $database->beforeExecuting(function () { if (RefreshDatabaseState::$lazilyRefreshed) { return; } diff --git a/src/Illuminate/Support/Facades/DB.php b/src/Illuminate/Support/Facades/DB.php index 997f790c0dd3..66ceee50f15a 100755 --- a/src/Illuminate/Support/Facades/DB.php +++ b/src/Illuminate/Support/Facades/DB.php @@ -28,6 +28,7 @@ * @method static void enableQueryLog() * @method static void disableQueryLog() * @method static void flushQueryLog() + * @method static void beforeExecuting(\Closure $callback) * @method static void listen(\Closure $callback) * @method static void rollBack(int $toLevel = null) * @method static void setDefaultConnection(string $name) diff --git a/src/Illuminate/Testing/ParallelTestingServiceProvider.php b/src/Illuminate/Testing/ParallelTestingServiceProvider.php index 20b900d2e58e..62be1365c5f8 100644 --- a/src/Illuminate/Testing/ParallelTestingServiceProvider.php +++ b/src/Illuminate/Testing/ParallelTestingServiceProvider.php @@ -23,7 +23,7 @@ public function boot() } /** - * Register the service provider. + * Register the service provider.` * * @return void */ diff --git a/tests/Database/DatabaseConnectionTest.php b/tests/Database/DatabaseConnectionTest.php index 1262892da075..fecabd312b9d 100755 --- a/tests/Database/DatabaseConnectionTest.php +++ b/tests/Database/DatabaseConnectionTest.php @@ -407,16 +407,15 @@ public function testLogQueryFiresEventsIfSet() $connection->logQuery('foo', [], null); } - public function testAnEventIsFiredBeforeAndAfterQuerying() + public function testBeforeExecutingHooksCanBeRegistered() { - $connection = $this->getMockConnection(); - $connection->setEventDispatcher($events = m::mock(Dispatcher::class)); - $events->shouldReceive('dispatch')->once()->with(m::type(QueryExecuting::class)); - $events->shouldReceive('dispatch')->once()->with(m::type(QueryExecuted::class)); + $this->expectExceptionMessage('The callback was fired'); - $connection->pretend(function ($connection) { - $connection->select('foo bar', ['baz']); + $connection = $this->getMockConnection(); + $connection->beforeExecuting(function() { + throw new Exception('The callback was fired'); }); + $connection->select('foo bar', ['baz']); } public function testPretendOnlyLogsQueries() From 6d011b1738e284aa025c1977599a416e42d4a596 Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Sun, 19 Sep 2021 08:36:55 +0100 Subject: [PATCH 07/13] Refactor --- src/Illuminate/Testing/ParallelTestingServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Testing/ParallelTestingServiceProvider.php b/src/Illuminate/Testing/ParallelTestingServiceProvider.php index 62be1365c5f8..20b900d2e58e 100644 --- a/src/Illuminate/Testing/ParallelTestingServiceProvider.php +++ b/src/Illuminate/Testing/ParallelTestingServiceProvider.php @@ -23,7 +23,7 @@ public function boot() } /** - * Register the service provider.` + * Register the service provider. * * @return void */ From a2dbb950d833168eb1e9f9387b4f49dc03ffda2f Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Sun, 19 Sep 2021 08:37:15 +0100 Subject: [PATCH 08/13] Refactor --- tests/Database/DatabaseConnectionTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Database/DatabaseConnectionTest.php b/tests/Database/DatabaseConnectionTest.php index fecabd312b9d..0c1deae102f0 100755 --- a/tests/Database/DatabaseConnectionTest.php +++ b/tests/Database/DatabaseConnectionTest.php @@ -8,7 +8,6 @@ use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Connection; use Illuminate\Database\Events\QueryExecuted; -use Illuminate\Database\Events\QueryExecuting; use Illuminate\Database\Events\TransactionBeginning; use Illuminate\Database\Events\TransactionCommitted; use Illuminate\Database\Events\TransactionRolledBack; From 437f0ccc6e830d9f8037a97c4d89bc4ff820d01c Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Sun, 19 Sep 2021 08:38:35 +0100 Subject: [PATCH 09/13] Refactor --- src/Illuminate/Database/Connection.php | 2 +- src/Illuminate/Foundation/Testing/RefreshDatabaseLazily.php | 2 -- tests/Database/DatabaseConnectionTest.php | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Database/Connection.php b/src/Illuminate/Database/Connection.php index 60db855384f4..55462c35c45d 100755 --- a/src/Illuminate/Database/Connection.php +++ b/src/Illuminate/Database/Connection.php @@ -822,7 +822,7 @@ public function disconnect() /** * Register a hook to be run just before a database query is executed. * - * @param Closure $callback + * @param Closure $callback * @return void */ public function beforeExecuting(Closure $callback) diff --git a/src/Illuminate/Foundation/Testing/RefreshDatabaseLazily.php b/src/Illuminate/Foundation/Testing/RefreshDatabaseLazily.php index 16775b1c8313..356e004be5b5 100644 --- a/src/Illuminate/Foundation/Testing/RefreshDatabaseLazily.php +++ b/src/Illuminate/Foundation/Testing/RefreshDatabaseLazily.php @@ -2,8 +2,6 @@ namespace Illuminate\Foundation\Testing; -use Illuminate\Database\Events\QueryExecuting; - trait RefreshDatabaseLazily { use RefreshDatabase { diff --git a/tests/Database/DatabaseConnectionTest.php b/tests/Database/DatabaseConnectionTest.php index 0c1deae102f0..27ace0b7fc5d 100755 --- a/tests/Database/DatabaseConnectionTest.php +++ b/tests/Database/DatabaseConnectionTest.php @@ -411,7 +411,7 @@ public function testBeforeExecutingHooksCanBeRegistered() $this->expectExceptionMessage('The callback was fired'); $connection = $this->getMockConnection(); - $connection->beforeExecuting(function() { + $connection->beforeExecuting(function () { throw new Exception('The callback was fired'); }); $connection->select('foo bar', ['baz']); From 728db1dfbec1f09cec5177ecfa6b423cd8e26de7 Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Sun, 19 Sep 2021 08:39:56 +0100 Subject: [PATCH 10/13] Refactor --- src/Illuminate/Database/Connection.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Illuminate/Database/Connection.php b/src/Illuminate/Database/Connection.php index 55462c35c45d..b558462e91f2 100755 --- a/src/Illuminate/Database/Connection.php +++ b/src/Illuminate/Database/Connection.php @@ -8,7 +8,6 @@ use Exception; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Events\QueryExecuted; -use Illuminate\Database\Events\QueryExecuting; use Illuminate\Database\Events\StatementPrepared; use Illuminate\Database\Events\TransactionBeginning; use Illuminate\Database\Events\TransactionCommitted; From b98f99f4534b64e7f04e8fd65002a0601df92d82 Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Sun, 19 Sep 2021 09:17:24 +0100 Subject: [PATCH 11/13] Refactor --- src/Illuminate/Foundation/Testing/TestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index 8caeb701df05..ee19a864b591 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -117,7 +117,7 @@ protected function setUpTraits() { $uses = array_flip(class_uses_recursive(static::class)); - if (isset($uses[RefreshDatabase::class]) || isset($uses[RefreshDatabaseLazily::class])) { + if (isset($uses[RefreshDatabase::class])) { $this->refreshDatabase(); } From 8b5a82996033c8c9e9d4149b1c6cd74fa2700dc9 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 21 Sep 2021 13:27:19 -0500 Subject: [PATCH 12/13] formatting --- src/Illuminate/Database/Connection.php | 6 ++++-- ...{RefreshDatabaseLazily.php => LazilyRefreshDatabase.php} | 6 +++--- src/Illuminate/Foundation/Testing/RefreshDatabaseState.php | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) rename src/Illuminate/Foundation/Testing/{RefreshDatabaseLazily.php => LazilyRefreshDatabase.php} (83%) diff --git a/src/Illuminate/Database/Connection.php b/src/Illuminate/Database/Connection.php index b558462e91f2..a5d334d0f31c 100755 --- a/src/Illuminate/Database/Connection.php +++ b/src/Illuminate/Database/Connection.php @@ -821,12 +821,14 @@ public function disconnect() /** * Register a hook to be run just before a database query is executed. * - * @param Closure $callback - * @return void + * @param \Closure $callback + * @return $this */ public function beforeExecuting(Closure $callback) { $this->beforeExecutingCallbacks[] = $callback; + + return $this; } /** diff --git a/src/Illuminate/Foundation/Testing/RefreshDatabaseLazily.php b/src/Illuminate/Foundation/Testing/LazilyRefreshDatabase.php similarity index 83% rename from src/Illuminate/Foundation/Testing/RefreshDatabaseLazily.php rename to src/Illuminate/Foundation/Testing/LazilyRefreshDatabase.php index 356e004be5b5..98204cceab48 100644 --- a/src/Illuminate/Foundation/Testing/RefreshDatabaseLazily.php +++ b/src/Illuminate/Foundation/Testing/LazilyRefreshDatabase.php @@ -2,10 +2,10 @@ namespace Illuminate\Foundation\Testing; -trait RefreshDatabaseLazily +trait LazilyRefreshDatabase { use RefreshDatabase { - refreshDatabase as standardRefreshDatabase; + refreshDatabase as baseRefreshDatabase; } /** @@ -24,7 +24,7 @@ public function refreshDatabase() RefreshDatabaseState::$lazilyRefreshed = true; - $this->standardRefreshDatabase(); + $this->baseRefreshDatabase(); }); $this->beforeApplicationDestroyed(function () { diff --git a/src/Illuminate/Foundation/Testing/RefreshDatabaseState.php b/src/Illuminate/Foundation/Testing/RefreshDatabaseState.php index 1cb0e996d3b0..a42d3d081bda 100644 --- a/src/Illuminate/Foundation/Testing/RefreshDatabaseState.php +++ b/src/Illuminate/Foundation/Testing/RefreshDatabaseState.php @@ -12,7 +12,7 @@ class RefreshDatabaseState public static $migrated = false; /** - * Indicates if a lazy hook has been fired. + * Indicates if a lazy refresh hook has been invoked. * * @var bool */ From e055e1ef07a9f3a583d79764c756404569a74895 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 21 Sep 2021 13:28:52 -0500 Subject: [PATCH 13/13] fix doc block --- src/Illuminate/Support/Facades/DB.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Facades/DB.php b/src/Illuminate/Support/Facades/DB.php index 66ceee50f15a..554dd22030ff 100755 --- a/src/Illuminate/Support/Facades/DB.php +++ b/src/Illuminate/Support/Facades/DB.php @@ -28,7 +28,7 @@ * @method static void enableQueryLog() * @method static void disableQueryLog() * @method static void flushQueryLog() - * @method static void beforeExecuting(\Closure $callback) + * @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)