fix(tests): isolate databases in memory and reject persistent connections
This commit is contained in:
31
tests/Support/InMemoryConnectionFactory.php
Normal file
31
tests/Support/InMemoryConnectionFactory.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Support;
|
||||
|
||||
use Illuminate\Database\Connectors\ConnectionFactory;
|
||||
use RuntimeException;
|
||||
|
||||
final class InMemoryConnectionFactory extends ConnectionFactory
|
||||
{
|
||||
public static function assertSafe(array $config): void
|
||||
{
|
||||
// Reject URLs and alternate endpoints rather than trusting a database name.
|
||||
if (($config['driver'] ?? null) !== 'sqlite'
|
||||
|| ($config['database'] ?? null) !== ':memory:'
|
||||
|| ! empty($config['url'])
|
||||
|| array_key_exists('read', $config)
|
||||
|| array_key_exists('write', $config)
|
||||
|| array_key_exists('direct', $config)) {
|
||||
throw new RuntimeException(
|
||||
'Unsafe test connection. Tests may only use SQLite :memory: without URLs or alternate endpoints.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function make(array $config, $name = null)
|
||||
{
|
||||
self::assertSafe($config);
|
||||
|
||||
return parent::make($config, $name);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user