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

[10.x] Streamline DatabaseMigrations and RefreshDatabase events #49153

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/Illuminate/Foundation/Testing/DatabaseMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,46 @@ trait DatabaseMigrations
*/
public function runDatabaseMigrations()
{
$this->artisan('migrate:fresh', $this->migrateFreshUsing());

$this->app[Kernel::class]->setArtisan(null);
$this->beforeRefreshingDatabase();
$this->refreshTestDatabase();
$this->afterRefreshingDatabase();

$this->beforeApplicationDestroyed(function () {
$this->artisan('migrate:rollback');

RefreshDatabaseState::$migrated = false;
});
}

/**
* Refresh a conventional test database.
*
* @return void
*/
protected function refreshTestDatabase()
{
$this->artisan('migrate:fresh', $this->migrateFreshUsing());

$this->app[Kernel::class]->setArtisan(null);
}

/**
* Perform any work that should take place before the database has started refreshing.
*
* @return void
*/
protected function beforeRefreshingDatabase()
{
// ...
}

/**
* Perform any work that should take place once the database has finished refreshing.
*
* @return void
*/
protected function afterRefreshingDatabase()
{
// ...
}
}
2 changes: 1 addition & 1 deletion tests/Integration/Database/DatabaseCustomCastsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class DatabaseCustomCastsTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('test_eloquent_model_with_custom_casts', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class DatabaseEloquentBroadcastingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('test_eloquent_broadcasting_users', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class DatabaseEloquentModelAttributeCastingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('test_eloquent_model_with_custom_casts', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class DatabaseEloquentModelCustomCastingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('test_eloquent_model_with_custom_casts', function (Blueprint $table) {
$table->increments('id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentAggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class EloquentAggregateTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function tearDown(): void
Carbon::setTestNow(null);
}

protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentBelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class EloquentBelongsToTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentCollectionFreshTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class EloquentCollectionFreshTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class EloquentCollectionLoadCountTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class EloquentCollectionLoadMissingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentCursorPaginateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class EloquentCursorPaginateTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('test_posts', function (Blueprint $table) {
$table->increments('id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentCustomPivotCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class EloquentCustomPivotCastTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class EloquentDeleteTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class EloquentHasManyTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('eloquent_has_many_test_users', function ($table) {
$table->id();
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentHasManyThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class EloquentHasManyThroughTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentHasOneIsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class EloquentHasOneIsTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentHasOneOfManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class EloquentHasOneOfManyTest extends DatabaseTestCase
{
public $retrievedLogins;

protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('users', function ($table) {
$table->id();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class EloquentLazyEagerLoadingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('one', function (Blueprint $table) {
$table->increments('id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentMassPrunableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function setUp(): void
$container->alias(Dispatcher::class, 'events');
}

protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
collect([
'mass_prunable_test_models',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected function setUp(): void
});
}

protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('test_model1', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class EloquentModelDateCastingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('test_model1', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class EloquentModelDecimalCastingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('test_model1', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function setUp(): void
Model::$encrypter = null;
}

protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('encrypted_casts', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class EloquentModelEnumCastingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('enum_casts', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class EloquentModelHashedCastingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('hashed_casts', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class EloquentModelImmutableDateCastingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('test_model_immutable', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class EloquentModelJsonCastingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('json_casts', function (Blueprint $table) {
$table->increments('id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentModelLoadCountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class EloquentModelLoadCountTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('base_models', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class EloquentModelLoadMissingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentModelRefreshTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class EloquentModelRefreshTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class EloquentModelStringCastingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('casting_table', function (Blueprint $table) {
$table->increments('id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class EloquentModelTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('test_model1', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class EloquentModelWithoutEventsTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('auto_filled_models', function (Blueprint $table) {
$table->increments('id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Database/EloquentMorphConstrainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class EloquentMorphConstrainTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class EloquentMorphCountEagerLoadingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('likes', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class EloquentMorphCountLazyEagerLoadingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('likes', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class EloquentMorphEagerLoadingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class EloquentMorphLazyEagerLoadingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
protected function afterRefreshingDatabase()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
Expand Down
Loading