Files
alrahma_sunday_school/tests/_support/DBReset.php
T
root 504c3bc9f9
Tests / PHPUnit (push) Failing after 1m24s
fix db delete data issue
2026-07-13 01:43:32 -04:00

74 lines
2.3 KiB
PHP

<?php
namespace Tests\Support;
use Config\Database;
use Throwable;
class DBReset
{
private const DB_GROUP = 'tests';
protected static array $excludeTables = ['migrations', 'school_years'];
public static function resetDatabase(): void
{
$config = config('Database');
$dbConfig = $config->{self::DB_GROUP} ?? [];
self::assertSafeTestDatabase($config->default ?? [], $dbConfig);
try {
$db = Database::connect(self::DB_GROUP);
$db->initialize();
} catch (Throwable $exception) {
throw new \RuntimeException(
sprintf(
'Test database connection failed: group=%s host=%s port=%s database=%s',
self::DB_GROUP,
$dbConfig['hostname'] ?? '',
$dbConfig['port'] ?? '',
$dbConfig['database'] ?? ''
),
0,
$exception
);
}
$db->disableForeignKeyChecks();
$tables = $db->resetDataCache()->listTables();
foreach ($tables as $table) {
if (!in_array($table, self::$excludeTables, true) && $db->tableExists($table, false)) {
$db->query('TRUNCATE TABLE ' . $db->escapeIdentifiers($table));
}
}
$db->enableForeignKeyChecks();
}
private static function assertSafeTestDatabase(array $defaultConfig, array $testConfig): void
{
$defaultDatabase = (string) ($defaultConfig['database'] ?? '');
$testDatabase = (string) ($testConfig['database'] ?? '');
if ($testDatabase === '') {
throw new \RuntimeException('Refusing to reset database: tests database name is empty.');
}
if ($defaultDatabase !== '' && $testDatabase === $defaultDatabase) {
throw new \RuntimeException(sprintf(
'Refusing to reset database "%s": tests database matches the default application database.',
$testDatabase
));
}
if (! preg_match('/(^test_|_test$|^tests$|^alrahma_test$)/i', $testDatabase)) {
throw new \RuntimeException(sprintf(
'Refusing to reset database "%s": test database names must clearly identify a test database.',
$testDatabase
));
}
}
}