To resolve a similar problem, I used a trait on the Model classes.
In my phpunit.xml I have this code
<env name="DB_CONNECTION" value="sqlite_testing"/>
<env name="DB_DATABASE" value=":memory:"/>```
In my config/database.php file I have connections set up for each of the databases, and a sqlite_testing connection set up for testing
'sqlite_testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
'mysql_connection_a' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'mysql_connection_b' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE_B', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'mysql_connection_c' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE_C', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
I then create a trait for each of my connections to set the connection and include them in the relevant models. e.g. if the User model needed to use mysql_connection_a I would use ConnectionATrait in the model
use App\Traits\ConnectionATrait;
class User extends Authenticatable
{
use Notifiable, ConnectionATrait;
The trait would then look like this
trait ConnectionATrait
{
/**
* The database table used by the model.
*
* @var string
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
if (env('APP_ENV') != 'testing') {
$this->connection = 'mysql_connection_a';
}else{
$this->connection = 'sqlite_testing';
}
}
}
If you use migrations in your tests I also had to do a similar approach in the migration files and use a trait for each connection.
For the mysql_connection_a I create a trait that looks likes below that overrides the getConnection method:
trait ConnectionAConnectionTrait
{
/**
* Get the migration connection name.
*
* @return string
*/
public function getConnection()
{
if (env('APP_ENV') != 'testing') {
return 'mysql_connection_a';
}
return 'sqlite_testing';
}
}
Then in the migration it would look like this
use Database\migrations\traits\ConnectionAConnectionTrait;
class CreateUsersTable extends Migration {
use ConnectionAConnectionTrait;
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::connection($this->getConnection())
->create('users', function(Blueprint $table)
{