Skip to content
Advertisement

Running Laravel anonymous migrations in Orchestra Test Bench

When doing testing in Orchestra Test Bench I often need to interact with the DB. It used to be that in your TestCase you would run this:

protected function getEnvironmentSetUp($app)
{
        include_once __DIR__ . '/../database/migrations/2021_01_01_100000_create_processes_table.php';
        (new CreateProcessesTable())->up();
}

Since Laravel 9 (I believe), there are now anonymous migrations which as their name implies, don’t have class names. So the above method doesn’t work. Thankfully, this does allow migrations in a package to be run, however, I often want to run migrations in another package, and those are not picked up by the ‘RefreshDatabase’ trait.

I used to be able to manually call them as above, but now I’m not clear on how to do so.

Advertisement

Answer

So I finally found a bit of a fix for this that I thought I would share incase it helps anyone in the future. Rather than calling the migrations directly, I’ve instead booted the migrations from the other package (they are in the same repo) via a conditional in the ServiceProvider of the package under test:

public function boot() 
{
    if(config('app.env') == 'testing') {
        $this->loadMigrationsFrom(__DIR__ . '/../../EventBus/database/migrations');
    }
}

Not the cleanest solution, but actually much nicer than having to load every table manually in the TestCase.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement