Laravel Dusk Migration Rollback Causing Schema Changed Exceptions

Viewed 457

I am attempting to run some browser tests on an sqlite, in file database separate from my application.

In one process I run

APP_ENV=local artisan serve --env=dusk.local

And in another I run

APP_ENV=local artisan dusk

What appears to happen is my test runs, the database migrates, runs my test which passes and then rolls back as expected.

However, following the successful test my Laravel log gets filled with loads of exceptions varying from those which would be expected when a request is made in the absence of a schema and data, and others such as

General error: 17 database schema has changed in [...]/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:91

I can't understand why it'd be making a request or doing something after the successful test.

class RegistrationTest extends DuskTestCase
{
    use DatabaseMigrations;

    public function testUserCanRegister()
    {
        // Some factories here

        $this->browse(function (Browser $browser) {
            $browser->visit('/register')
                ->assertSee('Sign up');
        });
    }

Which yields

PHPUnit 5.7.21 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 5.5 seconds, Memory: 22.00MB

OK (1 test, 1 assertion)

But then I see my log files full of errors if i tail them.

Anyone able to shed some light on what might be going on?

The dusk env file i'm using is below

APP_ENV=local
APP_KEY=...
APP_URL=http://localhost:8000

DB_DEFAULT=app_testing
DB_CONNECTION=app_testing
SECOND_DB_CONNECTION=other_testing #app uses 2 separate databases

SESSION_DRIVER=file

Update: some malformed asset urls were causing subsequent failing requests.

However, if i disable the rollback none of the aforementioned logged exceptions are created. Rolling back seems to cause issues if data exists.

If i allow rollback but don't run any factories during the test, it rolls back fine.

If I allow rollback after inserting data during the test, it rolls back but with loads of exceptions.

1 Answers

Even if you have zero un-migration migrations in your database/migrations/ folder, and you use DatabaseMigrations, dusk will run migrations before ruinning your tests, and run a rollback after running the tests.

If there are no outstanding migrations waiting when you start dusk, it will still run php artisan migrate, but nothing will be migrated.

After the tests are done, it will also run a php artisan migrate:rollback. This will tollback the most recent bactch of migrations.

If you did not have any migrations ready to run, it will rollback the prior batch of migrations. This could be any migration right back to the out of the box 2014_10_12_100000_create_password_resets_table.

So the rollback may have deleted tables when rolling back migrations you did not want to rollback at all, destroying your schema, filling your log files.

To find out if this is what is going on, look at your migrations table just beofore you start your tests, sorted by id descending (most recent migrations at the top) and see if dusk deletes rows.

To stop this behaviour take out use DatabaseMigrations; from the test class.

Related