Files
opensourcepos/tests/Models/ItemTaxesMultipleTest.php
T
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

77 lines
2.3 KiB
PHP

<?php
namespace Tests\Models;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use App\Models\Item_taxes;
use Tests\Support\DatabaseBootstrapTrait;
use Tests\Support\ItemSearchFixtureTrait;
class ItemTaxesMultipleTest extends CIUnitTestCase
{
use DatabaseTestTrait;
use DatabaseBootstrapTrait;
use ItemSearchFixtureTrait;
protected $migrate = true;
protected $migrateOnce = true;
protected $seed = '';
protected $seedOnce = true;
protected $refresh = false;
protected $namespace = null;
protected $item_taxes;
protected function setUp(): void
{
$this->resetDatabaseOnce();
parent::setUp();
$this->item_taxes = model(Item_taxes::class);
}
public function testGetInfoMultipleReturnsTaxesGroupedByItemId(): void
{
$itemOneId = $this->createSearchableItem();
$itemTwoId = $this->createSearchableItem();
$itemOneTaxes = [['name' => 'VAT', 'percent' => 20]];
$itemTwoTaxes = [['name' => 'GST', 'percent' => 10]];
$this->item_taxes->save_value($itemOneTaxes, $itemOneId);
$this->item_taxes->save_value($itemTwoTaxes, $itemTwoId);
$result = $this->item_taxes->getInfoMultiple([$itemOneId, $itemTwoId]);
$this->assertArrayHasKey($itemOneId, $result);
$this->assertArrayHasKey($itemTwoId, $result);
$this->assertCount(1, $result[$itemOneId]);
$this->assertCount(1, $result[$itemTwoId]);
$this->assertEquals('VAT', $result[$itemOneId][0]['name']);
$this->assertEquals('GST', $result[$itemTwoId][0]['name']);
}
public function testGetInfoMultipleItemWithNoTaxesOmittedFromResult(): void
{
$itemWithTaxesId = $this->createSearchableItem();
$itemWithoutTaxesId = $this->createSearchableItem();
$taxes = [['name' => 'VAT', 'percent' => 20]];
$this->item_taxes->save_value($taxes, $itemWithTaxesId);
$result = $this->item_taxes->getInfoMultiple([$itemWithTaxesId, $itemWithoutTaxesId]);
$this->assertArrayHasKey($itemWithTaxesId, $result);
$this->assertArrayNotHasKey($itemWithoutTaxesId, $result);
}
public function testGetInfoMultipleEmptyItemIdsReturnsEmptyArray(): void
{
$result = $this->item_taxes->getInfoMultiple([]);
$this->assertSame([], $result);
}
}