我喜欢在laravel应用中使用两(2)个数据库连接来设置测试套件。同样对于Gitlab的问题,我需要一个配置让我向gitlab表示,由于难以在mysql中创建一个具有多个数据库的docker,因此在测试时使用的是单个数据库而不是两个数据库。 / p>
配置/数据库
<?php
use Illuminate\Support\Str;
return [
'default' => env('DB_CONNECTION', 'mysql'),
'connection_vmail' => env('DB_CONNECTION_VMAIL', 'vmail'),
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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' => env('DB_PREFIX', ''),
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'mysql_vmail' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE_VMAIL', 'forge'),
'username' => env('DB_USERNAME_VMAIL', 'forge'),
'password' => env('DB_PASSWORD_VMAIL', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => env('DB_PREFIX_VMAIL', ''),
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
// PHPunit testing main connection
'testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => 'albarid_',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
// PHPunit testing audit connection
'testing_vmail' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => 'vmail_',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
.env
APP_ENV=local
DB_CONNECTION=mysql
DB_HOST=IP_MYSQL
DB_PORT=3306
DB_DATABASE=albarid
DB_USERNAME=albarid
DB_PASSWORD=MyPass>Word
DB_PREFIX=
DB_CONNECTION_VMAIL=mysql_vmail
DB_HOST_VMAIL=IP_VMAIL
DB_PORT_VMAIL=3306
DB_DATABASE_VMAIL=vmail
DB_USERNAME_VMAIL=albarid
DB_PASSWORD_VMAIL=MyPass>Word
DB_PREFIX_VMAIL=
phpunit.xml
<php>
<server name="APP_ENV" value="testing"/>
<server name="DB_CONNECTION" value="testing"/>
<server name="DB_CONNECTION_VMAIL" value="testing_vmail" />
...
</php>
模型MyTable
protected $table = 'mytable';
protected $connection = 'mysql_vmail';
好吧,如果使用DB :: table('mytable')可以正常工作,并且所有测试都在内存中进行写,但是如果尝试使用工厂,则以正常连接进行写。