Files
opensourcepos/tests/Helpers/GetItemDataRowTest.php
T
objecttothis 09e2c945fb test: replace OsposConfigFixtureTrait with Appconfig model persistence
- Remove OsposConfigFixtureTrait usage across test suite; settings are
  now written through model(Appconfig::class)->batch_save() instead of
  mocking the OSPOS config object directly
- OSPOSRulesTest keeps direct Config\OSPOS injection via
  Factories::injectMock since it tests config-object behavior directly
- CashupControllerTest, ItemKitsControllerTest, GetItemDataRowTest,
  LocaleHelperTest, export_helperTest, tabular_helperTest switch to
  real DB-backed settings via Appconfig model
- GetItemDataRowTest, LocaleHelperTest, export_helperTest,
  tabular_helperTest add DatabaseTestTrait/DatabaseBootstrapTrait and
  migrate/seed config plus resetDatabaseOnce() in setUp()
- export_helperTest consolidates redundant parent::setUp() calls
  scattered across individual test methods into a single call

This aligns tests with how settings are actually read at runtime
(DB-backed Appconfig) rather than relying on a config-mocking fixture
trait, reducing drift between test setup and production behavior.

Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>
2026-10-23 15:36:11 +04:00

117 lines
3.2 KiB
PHP

<?php
namespace Tests\Helpers;
use App\Models\Appconfig;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use stdClass;
use Tests\Support\DatabaseBootstrapTrait;
class GetItemDataRowTest extends CIUnitTestCase
{
use DatabaseTestTrait;
use DatabaseBootstrapTrait;
protected $migrate = true;
protected $migrateOnce = true;
protected $seedOnce = true;
protected $refresh = false;
protected $namespace = null;
protected function setUp(): void
{
$this->resetDatabaseOnce();
parent::setUp();
helper('tabular');
$this->injectSettings();
}
protected function injectSettings(array $overrides = []): void
{
model(Appconfig::class)->batch_save(array_merge([
'multi_pack_enabled' => 0,
'use_destination_based_tax' => 0,
'number_locale' => 'en_US',
'currency_decimals' => 2,
'quantity_decimals' => 0,
'thousands_separator' => 1,
'currency_symbol' => '$',
], $overrides));
}
protected function makeItem(array $overrides = []): stdClass
{
$item = new stdClass();
$defaults = [
'item_id' => 1,
'item_number' => 'ITEM-001',
'name' => 'Test Item',
'category' => 'Test Category',
'company_name' => 'Test Supplier',
'cost_price' => 10.00,
'unit_price' => 20.00,
'quantity' => 5,
'pic_filename' => null,
'pack_name' => null,
'tax_category_id' => null,
];
foreach (array_merge($defaults, $overrides) as $key => $value) {
$item->$key = $value;
}
return $item;
}
public function testTaxPercentsPulledFromPassedMapNotQueried(): void
{
$item = $this->makeItem(['item_id' => 42]);
$columns = getItemDataRow($item, [], [42 => '20.00%']);
$this->assertEquals('20.00%', $columns['tax_percents']);
}
public function testTaxPercentsDefaultsToDashWhenItemIdMissingFromMap(): void
{
$item = $this->makeItem(['item_id' => 99]);
$columns = getItemDataRow($item, [], []);
$this->assertEquals('-', $columns['tax_percents']);
}
public function testDefinitionNamesPassedThroughToExpandAttributeValues(): void
{
$item = $this->makeItem(['item_id' => 7]);
$item->attribute_values = '3_Red';
$item->attribute_dtvalues = '';
$item->attribute_dvalues = '';
$definitionNames = [
3 => ['name' => 'Color', 'type' => TEXT],
];
$columns = getItemDataRow($item, $definitionNames, []);
$this->assertArrayHasKey(3, $columns);
$this->assertEquals('Red', $columns[3]);
}
public function testPackNameAppendedToItemName(): void
{
$this->injectSettings(['multi_pack_enabled' => 1]);
$item = $this->makeItem(['name' => 'Base Item', 'pack_name' => 'Pack of 6']);
$columns = getItemDataRow($item, [], []);
$this->assertEquals('Base Item' . NAME_SEPARATOR . 'Pack of 6', $columns['name']);
}
}