Files
opensourcepos/app/Database/Seeds/TestDatabaseBootstrapSeeder.php
T
objecttothisandClaude Sonnet 5 0015b7575c Revert "Merge remote-tracking branch 'OpensourcePOS/master' into fix-unit-test-failures" and test-standardization commit (#4770)
These landed on master by accident via a local branch that was
mistakenly created tracking OpensourcePOS/master directly, rather than
pushed as its own branch. Reverting to restore master to its prior
state; the reverted work will come back through a proper PR instead.

This reverts commit 00bf4cb8af.
This reverts commit 37cc1b15e7.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-22 16:43:32 +04:00

55 lines
1.9 KiB
PHP

<?php
namespace App\Database\Seeds;
use CodeIgniter\Database\Seeder;
use Config\Database;
class TestDatabaseBootstrapSeeder extends Seeder
{
public static function reset(): void
{
if (ENVIRONMENT !== 'testing') {
throw new \RuntimeException('TestDatabaseBootstrapSeeder can only run in the testing environment.');
}
$config = config('Database');
$group = $config->tests;
$dbName = $group['database'];
if ($dbName === '' || !str_contains(strtolower($dbName), 'test')) {
throw new \RuntimeException("Refusing to reset non-test database: {$dbName}");
}
$serverConn = Database::connect([
'hostname' => $group['hostname'],
'username' => $group['username'],
'password' => $group['password'],
'DBDriver' => $group['DBDriver'],
'database' => null,
'charset' => $group['charset'] ?? 'utf8mb4',
'DBCollat' => $group['DBCollat'] ?? 'utf8mb4_general_ci',
], false);
$serverConn->query("DROP DATABASE IF EXISTS `{$dbName}`");
$serverConn->query("CREATE DATABASE IF NOT EXISTS `{$dbName}`");
$serverConn->close();
// The application's shared 'tests' connection caches listTables()
// results in dataCache['table_names'']. Because we dropped and
// recreated the schema on a SEPARATE server connection above, that
// cache is now stale. Reset it so subsequent listTables()/tableExists()
// calls re-query the server instead of trusting a dropped schema's table
// list (which previously caused "ospos_migrations doesn't exist" and
// "column ... doesn't exist" errors for every class that followed a
// bootstrap-reset class in the suite).
$shared = Database::connect('tests');
$shared->resetDataCache();
}
public function run(): void
{
self::reset();
}
}