Skip to content

Commit

Permalink
[10.x] Streamline DatabaseMigrations and RefreshDatabase events (#…
Browse files Browse the repository at this point in the history
…49153)

* [10.x] Test Improvements

Use available method from `RefreshDatabase::afterRefreshingDatabase()`

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored Nov 28, 2023
1 parent 77da0a7 commit 1e03a4a
Show file tree
Hide file tree
Showing 76 changed files with 110 additions and 78 deletions.
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

0 comments on commit 1e03a4a

Please sign in to comment.