Skip to content

Commit eaebe19

Browse files
committed
test(database): cover transaction callback retry behavior
- Add commit callback coverage when driver commit fails once - Add rollback callback coverage when driver rollback fails once - Verify callbacks run after retry and are cleared afterward Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 2381f51 commit eaebe19

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

tests/system/Database/BaseConnectionTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,72 @@ public function testGetSessionTimezoneWithoutTimezoneKey(): void
558558
$this->assertNull($result);
559559
}
560560

561+
public function testAfterCommitCallbacksRemainQueuedWhenDriverCommitFails(): void
562+
{
563+
$callbacks = [];
564+
565+
$db = new class ($this->options) extends MockConnection {
566+
public int $commitAttempts = 0;
567+
568+
protected function _transCommit(): bool
569+
{
570+
$this->commitAttempts++;
571+
572+
return $this->commitAttempts > 1;
573+
}
574+
};
575+
576+
$this->assertTrue($db->transBegin());
577+
$db->afterCommit(static function () use (&$callbacks): void {
578+
$callbacks[] = 'committed';
579+
});
580+
581+
$this->assertFalse($db->transCommit());
582+
$this->assertSame([], $callbacks);
583+
$this->assertSame(1, $db->transDepth);
584+
585+
$this->assertTrue($db->transCommit());
586+
$this->assertSame(['committed'], $callbacks);
587+
$this->assertSame(0, $db->transDepth);
588+
589+
$this->assertTrue($db->transBegin());
590+
$this->assertTrue($db->transCommit());
591+
$this->assertSame(['committed'], $callbacks);
592+
}
593+
594+
public function testAfterRollbackCallbacksRemainQueuedWhenDriverRollbackFails(): void
595+
{
596+
$callbacks = [];
597+
598+
$db = new class ($this->options) extends MockConnection {
599+
public int $rollbackAttempts = 0;
600+
601+
protected function _transRollback(): bool
602+
{
603+
$this->rollbackAttempts++;
604+
605+
return $this->rollbackAttempts > 1;
606+
}
607+
};
608+
609+
$this->assertTrue($db->transBegin());
610+
$db->afterRollback(static function () use (&$callbacks): void {
611+
$callbacks[] = 'rolled back';
612+
});
613+
614+
$this->assertFalse($db->transRollback());
615+
$this->assertSame([], $callbacks);
616+
$this->assertSame(1, $db->transDepth);
617+
618+
$this->assertTrue($db->transRollback());
619+
$this->assertSame(['rolled back'], $callbacks);
620+
$this->assertSame(0, $db->transDepth);
621+
622+
$this->assertTrue($db->transBegin());
623+
$this->assertTrue($db->transRollback());
624+
$this->assertSame(['rolled back'], $callbacks);
625+
}
626+
561627
public function testCallFunctionDoesNotDoublePrefixAlreadyPrefixedName(): void
562628
{
563629
$db = new class ($this->options) extends MockConnection {

0 commit comments

Comments
 (0)