Files
opensourcepos/tests/Support/DatabaseBootstrapTrait.php
objecttothisandClaude Sonnet 5 af48fa4357 fix(tests): standardize DB bootstrap/OSPOS mocking, fix stale-prefix bug (#4771)
Adds DatabaseBootstrapTrait and OsposConfigFixtureTrait to replace
duplicated per-file setUp()/tearDown() bootstrap and OSPOS config mock
boilerplate across test classes, bringing them in line with the
ItemsControllerTest gold-standard shape.

Fixes the real root cause of tabular_helperTest's suite-order-dependent
failure: dropAllForeignKeyConstraints()/foreignKeyExists()/
dropColumnIfExists() temporarily blank the shared connection's DBPrefix
via overridePrefix() but never restored it if the query between the two
calls threw (e.g. migration_helperTest's mocked-failure tests) -
permanently corrupting prefixed table lookups for the rest of the
process. Wrapped each in try/finally. Also widens executeScript() to
accept an explicit connection so migration SQL can't silently run
against the wrong DB group.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-23 10:10:37 +04:00

51 lines
1.3 KiB
PHP

<?php
namespace Tests\Support;
use App\Database\Seeds\TestDatabaseBootstrapSeeder;
use CodeIgniter\Database\Config;
/**
* Standard one-time-per-class database reset for DatabaseTestTrait tests.
*
* Call {@see self::resetDatabaseOnce()} as the first line of setUp(), before
* parent::setUp(). It drops and re-migrates the `tests` schema exactly once
* per test class (guarded by a per-class static, since PHP trait statics are
* independent per using-class) and closes the shared connection afterward so
* the migration run that follows reconnects against the freshly reset schema
* instead of reusing a connection whose state predates the reset.
*
* Usage:
*
* use Tests\Support\DatabaseBootstrapTrait;
*
* class FooTest extends CIUnitTestCase
* {
* use DatabaseTestTrait;
* use DatabaseBootstrapTrait;
*
* protected function setUp(): void
* {
* $this->resetDatabaseOnce();
*
* parent::setUp();
* }
* }
*/
trait DatabaseBootstrapTrait
{
private static bool $doneBootstrap = false;
protected function resetDatabaseOnce(): void
{
if (self::$doneBootstrap === true) {
return;
}
TestDatabaseBootstrapSeeder::reset();
Config::connect($this->DBGroup)->close();
self::$doneBootstrap = true;
}
}