-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x] Adds new
RefreshDatabaseLazily
testing trait (#38861)
* 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 <[email protected]>
- Loading branch information
1 parent
f267d7c
commit 3d1ead4
Showing
5 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/Illuminate/Foundation/Testing/LazilyRefreshDatabase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters