Remap Connections
If you'd like to alter the type of database used for your tests, you can change the "default" connection used, either by setting DB_CONNECTION
in your .env.testing
file, or by setting the $defaultConnection
property in your test-classes.
⚙️ See configuration
# .env.testing
# using Laravel's setting
DB_CONNECTION=sqlite
# or using Adapt's setting
ADAPT_DEFAULT_CONNECTION=sqlite
or
<?php
// config/code_distortion.adapt.php
return [
…
'default_connection' => env('ADAPT_DEFAULT_CONNECTION', 'sqlite'), // or null
…
];
or
<?php
// tests/Feature/MyFeatureTest.php
use CodeDistortion\Adapt\AdaptDatabase;
use Tests\TestCase;
class MyFeatureTest extends TestCase
{
use AdaptDatabase;
protected bool $defaultConnection = 'sqlite';
…
}
If you have a more complicated situation, you could look to remapping your database connections instead. This tells Adapt to override particular connections with the details from others.
Let's say for example you use database connections "primary" and "secondary", and these are normally MySQL databases. And let's also say your codebase refers to these connections by name. e.g.
// as a simple example…
// somewhere in your codebase, these
// connections are accessed by name
DB::connection('primary')->select(…);
DB::connection('secondary')->select(…);
You'd have a hard time getting these databases to use SQLite when running tests.
If you add some extra SQLite connections like below, you could then get Adapt to re-map them.
<?php
// configs/database.php
return [
…
'connections' => [
'primary' => [
'driver' => 'mysql',
'url' => env('PRIMARY_MYSQL_DATABASE_URL'),
'host' => env('PRIMARY_MYSQL_DB_HOST', '127.0.0.1'),
'port' => env('PRIMARY_MYSQL_DB_PORT', '3306'),
'database' => env('PRIMARY_MYSQL_DB_DATABASE', 'forge'),
'username' => env('PRIMARY_MYSQL_DB_USERNAME', 'forge'),
'password' => env('PRIMARY_MYSQL_DB_PASSWORD', ''),
'unix_socket' => env('PRIMARY_MYSQL_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'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'),
]) : [],
],
'secondary' => [
'driver' => 'mysql',
'url' => env('SECONDARY_MYSQL_DATABASE_URL'),
'host' => env('SECONDARY_MYSQL_DB_HOST', '127.0.0.1'),
'port' => env('SECONDARY_MYSQL_DB_PORT', '3306'),
'database' => env('SECONDARY_MYSQL_DB_DATABASE', 'forge'),
'username' => env('SECONDARY_MYSQL_DB_USERNAME', 'forge'),
'password' => env('SECONDARY_MYSQL_DB_PASSWORD', ''),
'unix_socket' => env('SECONDARY_MYSQL_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'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'),
]) : [],
],
'test-primary' => [
'driver' => 'sqlite',
'url' => env('PRIMARY_SQLITE_DATABASE_URL'),
'database' => env('PRIMARY_SQLITE_DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('PRIMARY_SQLITE_DB_FOREIGN_KEYS', true),
],
'test-secondary' => [
'driver' => 'sqlite',
'url' => env('SECONDARY_SQLITE_DATABASE_URL'),
'database' => env('SECONDARY_SQLITE_DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('SECONDARY_SQLITE_DB_FOREIGN_KEYS', true),
],
],
…
];
To re-map these connections, update Adapt's remap_connections
config setting. Or you can add the $remapConnections
property to your test-classes.
To override the "primary" and "secondary" connections from above with the "test-primary" and "test-secondary" details respectively, use the string "primary < test-primary, secondary < test-secondary"
.
⚙️ See configuration
# .env.testing
ADAPT_REMAP_CONNECTIONS="primary < test-primary, secondary < test-secondary"
or
<?php
// config/code_distortion.adapt.php
return [
…
'remap_connections' => env('ADAPT_REMAP_CONNECTIONS', 'primary < test-primary, secondary < test-secondary'), // or ''
…
];
or
<?php
// tests/Feature/MyFeatureTest.php
use CodeDistortion\Adapt\AdaptDatabase;
use Tests\TestCase;
class MyFeatureTest extends TestCase
{
use AdaptDatabase;
protected string $remapConnections = 'primary < test-primary, secondary < test-secondary'; // or ''
…
}
You can make connections defined in the config more important than values in your test-classes by adding "!". e.g. "!primary < test-primary"
.