Files
opensourcepos/tests/Support/OsposConfigFixtureTrait.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 CodeIgniter\Config\Factories;
use Config\OSPOS;
/**
* Shared helper for injecting a mocked OSPOS config into a test.
*
* Replaces the old per-file `$ospos = new OSPOS(); $ospos->settings = [...];
* Factories::injectMock(...)` block that was duplicated with slightly
* different settings arrays across many test files. Each test supplies the
* exact settings it depends on - OSPOS::getDefaultSettings() is private and
* only has a handful of keys, so it is not a usable base to merge onto.
*
* Usage:
*
* use Tests\Support\OsposConfigFixtureTrait;
*
* class FooTest extends CIUnitTestCase
* {
* use OsposConfigFixtureTrait;
*
* protected function setUp(): void
* {
* parent::setUp();
*
* $this->injectOsposSettings([
* 'invoice_enable' => false,
* 'number_locale' => 'en_US',
* ]);
* }
* }
*/
trait OsposConfigFixtureTrait
{
/**
* @param array<string, mixed> $settings
*/
protected function injectOsposSettings(array $settings): OSPOS
{
$ospos = new OSPOS();
$ospos->settings = $settings;
Factories::injectMock('config', OSPOS::class, $ospos);
return $ospos;
}
}