Hi guys, I have a project with tons of migration files on hand (lots of modifications to the original table schemas kind of projects) that needs unit-testing coverage.
So, after configuring the phpunit.xml file to use SQLite and running the test as expected SQLSTATE[HY000]: General error: 1 Cannot add a NOT NULL column with default value NULL (SQL: alter table "users" add column "role" varchar not null) error showed up.
So, I've decided to create clean optimized migration files to be used by unit-testing only, and to accomplish this I've used this package:
https://github.com/bennett-treptow/laravel-migration-generator
Then, I placed the generate files in Tests/database/migrations and overrode DatabaseMigrations & RefreshDatabase trait methods in the TestCase.php to point the migrations to the new path like so:
public function runDatabaseMigrations()
{
$this->artisan('migrate:fresh', [
'--path' => 'tests/database/migrations',
]);
$this->app[Kernel::class]->setArtisan(null);
$this->beforeApplicationDestroyed(function () {
$this->artisan('migrate:rollback');
RefreshDatabaseState::$migrated = false;
});
}
protected function refreshTestDatabase()
{
if ( ! RefreshDatabaseState::$migrated)
{
$this->artisan('migrate:fresh', [
'--path' => 'tests/database/migrations',
]);
$this->app[\Illuminate\Contracts\Console\Kernel::class]->setArtisan(null);
RefreshDatabaseState::$migrated = true;
}
$this->beginDatabaseTransaction();
}
But, this error was returned Cannot declare class X, because the name is already in use. Adding namespaces to those migration files will suppress this error but then it will start complaining that the class name is not the same as the migration file name.
Would appreciate it if someone can help out with an example or a workaround for this. Thanks!