Refresh Database Using Specific Database Connection In Laravel Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Feature; | |
use Tests\RefreshesDatabase; | |
class ExampleTest | |
{ | |
use RefreshesDatabase; | |
public string $connectionToMigrate = 'mysql-elevated'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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