-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[9.x] Add DatabaseTruncation
trait for testing
#45726
Merged
taylorotwell
merged 7 commits into
laravel:9.x
from
patrickomeara:database-truncates-take-2
Jan 31, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
debe87d
Add DatabaseTruncates option for setting up DB
patrickomeara 42db879
Use a specific seeder if it's set, otherwise use the default
patrickomeara 2b9a1b6
use migrations table from config
patrickomeara b4b2102
Handle truncating multiple connections
patrickomeara 9d14265
Get the database ready at the start of the test
patrickomeara e0199be
useForeignKeyChecks
patrickomeara 8a75339
formatting
taylorotwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
src/Illuminate/Foundation/Testing/DatabaseTruncation.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,118 @@ | ||
<?php | ||
|
||
namespace Illuminate\Foundation\Testing; | ||
|
||
use Illuminate\Contracts\Console\Kernel; | ||
use Illuminate\Database\ConnectionInterface; | ||
use Illuminate\Foundation\Testing\Traits\CanConfigureMigrationCommands; | ||
|
||
trait DatabaseTruncation | ||
{ | ||
use CanConfigureMigrationCommands; | ||
|
||
/** | ||
* The cached names of the database tables for each connection. | ||
* | ||
* @var array | ||
*/ | ||
protected static array $allTables; | ||
|
||
/** | ||
* Truncate the database tables for all configured connections. | ||
* | ||
* @return void | ||
*/ | ||
protected function truncateDatabaseTables(): void | ||
{ | ||
// Migrate and seed the database on first run... | ||
if (! RefreshDatabaseState::$migrated) { | ||
$this->artisan('migrate:fresh', $this->migrateFreshUsing()); | ||
|
||
$this->app[Kernel::class]->setArtisan(null); | ||
|
||
RefreshDatabaseState::$migrated = true; | ||
|
||
return; | ||
} | ||
|
||
// Always clear any test data on subsequent runs... | ||
$this->truncateTablesForAllConnections(); | ||
|
||
if ($seeder = $this->seeder()) { | ||
// Use a specific seeder class... | ||
$this->artisan('db:seed', ['--class' => $seeder]); | ||
} elseif ($this->shouldSeed()) { | ||
// Use the default seeder class... | ||
$this->artisan('db:seed'); | ||
} | ||
} | ||
|
||
/** | ||
* Truncate the database tables for all configured connections. | ||
* | ||
* @return void | ||
*/ | ||
protected function truncateTablesForAllConnections(): void | ||
{ | ||
$database = $this->app->make('db'); | ||
|
||
collect($this->connectionsToTruncate()) | ||
->each(function ($name) use ($database) { | ||
$connection = $database->connection($name); | ||
|
||
$connection->getSchemaBuilder()->withoutForeignKeyConstraints( | ||
fn () => $this->truncateTablesForConnection($connection, $name) | ||
); | ||
}); | ||
} | ||
|
||
/** | ||
* Truncate the database tables for the given database connection. | ||
* | ||
* @param \Illuminate\Database\ConnectionInterface $connection | ||
* @param string|null $name | ||
* @return void | ||
*/ | ||
protected function truncateTablesForConnection(ConnectionInterface $connection, ?string $name): void | ||
{ | ||
$dispatcher = $connection->getEventDispatcher(); | ||
|
||
$connection->unsetEventDispatcher(); | ||
|
||
collect(static::$allTables[$name] ??= $connection->getDoctrineSchemaManager()->listTableNames()) | ||
->diff($this->exceptTables($name)) | ||
->filter(fn ($table) => $connection->table($table)->exists()) | ||
->each(fn ($table) => $connection->table($table)->truncate()); | ||
|
||
$connection->setEventDispatcher($dispatcher); | ||
} | ||
|
||
/** | ||
* The database connections that should have their tables truncated. | ||
* | ||
* @return array | ||
*/ | ||
protected function connectionsToTruncate(): array | ||
{ | ||
return property_exists($this, 'connectionsToTruncate') | ||
? $this->connectionsToTruncate : [null]; | ||
} | ||
|
||
/** | ||
* Get the tables that should not be truncated. | ||
* | ||
* @param string|null $connectionName | ||
* @return array | ||
*/ | ||
protected function exceptTables(?string $connectionName): array | ||
{ | ||
if (property_exists($this, 'exceptTables')) { | ||
return array_merge( | ||
$this->exceptTables[$connectionName] ?? [], | ||
[$this->app['config']->get('database.migrations')] | ||
); | ||
} | ||
|
||
return [$this->app['config']->get('database.migrations')]; | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it will be better to use FQCN instead of the command name?