fix(tests): isolate databases in memory and reject persistent connections

This commit is contained in:
2026-09-14 12:35:34 -03:00
parent 28ab6c9ae4
commit 289ceba3af
8 changed files with 177 additions and 14 deletions

View 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);
}
}