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

Is it possible to support Snipe Migrations on the DatabaseMigrations trait? #20

Closed
JackWH opened this issue Mar 11, 2019 · 3 comments
Closed

Comments

@JackWH
Copy link

@JackWH JackWH commented Mar 11, 2019

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?

@drfraker

This comment has been minimized.

Copy link
Owner

@drfraker drfraker commented Mar 12, 2019

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 drfraker closed this Mar 12, 2019
@JackWH

This comment has been minimized.

Copy link
Author

@JackWH JackWH commented Mar 14, 2019

@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 👍

@JackWH

This comment has been minimized.

Copy link
Author

@JackWH JackWH commented Mar 17, 2019

I've put in a pull request for this: #24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.