Skip to content

Instantly share code, notes, and snippets.

@ziadoz
Last active Oct 18, 2022
Embed
What would you like to do?
Refresh Database Using Specific Database Connection In Laravel Tests
<?php
namespace Tests\Feature;
use Tests\RefreshesDatabase;
class ExampleTest
{
use RefreshesDatabase;
public string $connectionToMigrate = 'mysql-elevated';
}
<?php
namespace Tests;
use Illuminate\Foundation\Testing\RefreshDatabase as LaravelRefreshDatabase;
// Use a specific database connection for test migrations.
// @see: https://github.com/laravel/framework/pull/44630
// @see: https://github.com/laravel/framework/issues/36294
trait RefreshDatabase
{
use LaravelRefreshDatabase {
migrateFreshUsing as protected migrateFreshUsingDefaults;
}
protected function migrateFreshUsing()
{
$database = $this->connectionToMigrate();
return array_merge(
$this->migrateFreshUsingDefaults(),
$database ? ['--database' => $database] : [],
);
}
protected function connectionToMigrate(): string|bool
{
return property_exists($this, 'connectionToMigrate') ? $this->connectionToMigrate : false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment