Files
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

76 lines
2.2 KiB
PHP

<?php
namespace Tests\Models;
use App\Models\Appconfig;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Config\OSPOS;
use Tests\Support\DatabaseBootstrapTrait;
class AppconfigTest extends CIUnitTestCase
{
use DatabaseTestTrait;
use DatabaseBootstrapTrait;
protected $migrate = true;
protected $migrateOnce = true;
protected $seed = '';
protected $seedOnce = true;
protected $refresh = true;
protected $namespace = null;
protected function setUp(): void
{
$this->resetDatabaseOnce();
parent::setUp();
}
private function appconfig(): Appconfig
{
return model(Appconfig::class);
}
public function testBatchSave_PersistsAllKeysAndRefreshesCachedSettings(): void
{
$appconfig = $this->appconfig();
$prefix = uniqid('ospos_batch_');
$keyA = $prefix . 'alpha';
$keyB = $prefix . 'beta';
$keyC = $prefix . 'gamma';
$success = $appconfig->batch_save([
$keyA => 'value-a',
$keyB => 'value-b',
$keyC => 'value-c',
]);
$this->assertTrue($success);
// Every key was written to the database.
$this->assertSame('value-a', $appconfig->get_value($keyA));
$this->assertSame('value-b', $appconfig->get_value($keyB));
$this->assertSame('value-c', $appconfig->get_value($keyC));
// The cached settings were refreshed after the transaction, so a
// consumer reading config(OSPOS::class)->settings sees the new values.
$settings = config(OSPOS::class)->settings;
$this->assertSame('value-a', $settings[$keyA]);
$this->assertSame('value-b', $settings[$keyB]);
$this->assertSame('value-c', $settings[$keyC]);
}
public function testSave_RefreshesCachedSettings(): void
{
$appconfig = $this->appconfig();
$key = uniqid('ospos_single_');
$success = $appconfig->save([$key => 'single-value']);
$this->assertTrue($success);
$this->assertSame('single-value', $appconfig->get_value($key));
$this->assertSame('single-value', config(OSPOS::class)->settings[$key]);
}
}