Is it possible to support Snipe Migrations on the DatabaseMigrations trait?
Closed this issue ยท 3 comments
I'm using the DatabaseMigrations
trait, instead of the RefreshDatabase
trait, as I'm running my tests through Dusk.
Is it possible for Snipe Migrations to work with DatabaseMigrations
as well?
I don't have a way to use it out of the box, but here's the code I've used in an app recently to get it working.
// In DuskTestCase.php
public function setUp(): void
{
$uses = array_flip(class_uses_recursive(static::class));
if (isset($uses[DatabaseMigrations::class])) {
throw new Exception(
'Please use DuskDatabaseMigrations instead of DatabaseMigrations'
);
}
parent::setUp();
if (isset($uses[DuskDatabaseMigrations::class])) {
$this->runDatabaseMigrations();
}
}
// DuskDatabaseMigrations.php
<?php
namespace Tests;
use Drfraker\SnipeMigrations\Snipe;
use Drfraker\SnipeMigrations\SnipeDatabaseState;
trait DuskDatabaseMigrations
{
/**
* Import a database snapshot to "refresh" the database before tests run.
*
* @return void
*/
public function runDatabaseMigrations()
{
(new Snipe())->importSnapshot();
// Set the value below to false which will force a new database to be imported before each Dusk test.
// We need to do this because database transactions do not work in dusk tests.
SnipeDatabaseState::$importedDatabase = false;
}
}
@drfraker Thank you! All I can say is wow... This is phenomenal stuff!
My Dusk tests were previously taking up to 20 minutes to run. After implementing your suggestion above, I'm down to just 7 minutes!
I was also able to save more time by pre-running database seeds too, and saving them to the snapshot. Each of my test cases previously seeded the database before running the test using $this->seed()
in the setUp()
function of the test case. I removed those calls, and edited newSnapshot()
to run the seed command after the DB was migrated, but before the snapshot was taken:
/**
* Generate a new snapshot of the MySql database.
*/
protected function newSnapshot()
{
Artisan::call('migrate:fresh');
Artisan::call('db:seed');
...
This saved time re-running all the DB seeds on every test too, and seems like it could be an interesting thing to build in to Snipe Migrations natively.
Is this something you're working on? Otherwise, I would be happy to help contribute ๐