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

144 lines
4.6 KiB
PHP

<?php
namespace Tests\Models;
use App\Models\Giftcard;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Tests\Support\ConcurrentDbRaceTrait;
use Tests\Support\DatabaseBootstrapTrait;
/**
* Regression tests: decrementGiftcardValue() must
* apply its balance check and its write in a single atomic UPDATE, so that
* two concurrent decrements against the same gift card can never both read
* the same stale balance and double-spend it.
*/
class GiftcardTest extends CIUnitTestCase
{
use DatabaseTestTrait;
use DatabaseBootstrapTrait;
use ConcurrentDbRaceTrait;
protected $migrate = true;
protected $migrateOnce = true;
protected $refresh = false;
protected $namespace = null;
protected function setUp(): void
{
$this->resetDatabaseOnce();
parent::setUp();
}
protected function createGiftcard(float $value): int
{
$giftcardNumber = random_int(1000000, 9999999);
\Config\Database::connect()->table('giftcards')->insert([
'giftcard_number' => $giftcardNumber,
'value' => $value,
'deleted' => 0,
'person_id' => null,
]);
return $giftcardNumber;
}
protected function getGiftcardValue(int $giftcardNumber): float
{
$row = \Config\Database::connect()->table('giftcards')
->where('giftcard_number', $giftcardNumber)
->get()
->getRow();
return (float) $row->value;
}
public function testDecrementGiftcardValueSucceedsWithSufficientBalance(): void
{
$giftcardNumber = $this->createGiftcard(100.00);
$giftcardModel = model(Giftcard::class);
$result = $giftcardModel->decrementGiftcardValue((string) $giftcardNumber, 60.00);
$this->assertTrue($result);
$this->assertEqualsWithDelta(40.00, $this->getGiftcardValue($giftcardNumber), 0.001);
}
public function testDecrementGiftcardValueFailsWithInsufficientBalance(): void
{
$giftcardNumber = $this->createGiftcard(50.00);
$giftcardModel = model(Giftcard::class);
$result = $giftcardModel->decrementGiftcardValue((string) $giftcardNumber, 60.00);
$this->assertFalse($result);
$this->assertEqualsWithDelta(50.00, $this->getGiftcardValue($giftcardNumber), 0.001);
}
public function testDecrementGiftcardValueRejectsNegativeAmount(): void
{
$giftcardNumber = $this->createGiftcard(100.00);
$giftcardModel = model(Giftcard::class);
$result = $giftcardModel->decrementGiftcardValue((string) $giftcardNumber, -500.00);
$this->assertFalse($result);
$this->assertEqualsWithDelta(100.00, $this->getGiftcardValue($giftcardNumber), 0.001);
}
public function testDecrementGiftcardValueRejectsZeroAmount(): void
{
$giftcardNumber = $this->createGiftcard(100.00);
$giftcardModel = model(Giftcard::class);
$result = $giftcardModel->decrementGiftcardValue((string) $giftcardNumber, 0.00);
$this->assertFalse($result);
$this->assertEqualsWithDelta(100.00, $this->getGiftcardValue($giftcardNumber), 0.001);
}
public function testDecrementGiftcardValueExactBalanceSucceeds(): void
{
$giftcardNumber = $this->createGiftcard(60.00);
$giftcardModel = model(Giftcard::class);
$result = $giftcardModel->decrementGiftcardValue((string) $giftcardNumber, 60.00);
$this->assertTrue($result);
$this->assertEqualsWithDelta(0.00, $this->getGiftcardValue($giftcardNumber), 0.001);
}
public function testDecrementGiftcardValueFailsWhenDeleted(): void
{
$giftcardNumber = $this->createGiftcard(100.00);
\Config\Database::connect()->table('giftcards')
->where('giftcard_number', $giftcardNumber)
->update(['deleted' => 1]);
$giftcardModel = model(Giftcard::class);
$result = $giftcardModel->decrementGiftcardValue((string) $giftcardNumber, 60.00);
$this->assertFalse($result);
$this->assertEqualsWithDelta(100.00, $this->getGiftcardValue($giftcardNumber), 0.001);
}
public function testDecrementGiftcardValueConcurrentDecrementsOneWins(): void
{
$giftcardNumber = $this->createGiftcard(100.00);
[$result1, $result2] = $this->raceTwoProcesses(
'giftcard',
[$giftcardNumber, 60.00],
[$giftcardNumber, 60.00]
);
$this->assertSame(1, (int) $result1 + (int) $result2);
$this->assertEqualsWithDelta(40.00, $this->getGiftcardValue($giftcardNumber), 0.001);
}
}