tableName = 'DB2_TEST.DB2T_'. strtoupper(bin2hex(random_bytes(6))); $this->db()->statement(" CREATE TABLE {$this->tableName} ( ID INTEGER NOT NULL PRIMARY KEY, NAME VARCHAR(100) NOT NULL ) "); } protected function tearDown(): void { try { if ($this->tableName !== '') { $db = $this->db(); while ($db->transactionLevel() > 0) { $db->rollBack(); } $db->statement("DROP TABLE {$this->tableName}"); } } finally { parent::tearDown(); } } private function insertRow(int $id, string $name): void { $this->db()->table($this->tableName)->insert([ 'ID' => $id, 'NAME' => $name, ]); } private function rowCount(): int { return $this->db()->table($this->tableName)->count(); } public function test_transaction_commit(): void { $this->db()->beginTransaction(); $this->insertRow(1, 'Committed'); $this->assertSame(1, $this->db()->transactionLevel()); $this->db()->commit(); $this->assertSame(0, $this->db()->transactionLevel()); $this->assertSame(1, $this->rowCount()); } public function test_transaction_rollback(): void { $this->db()->beginTransaction(); $this->insertRow(1, 'Rolled back'); $this->db()->rollBack(); $this->assertSame(0, $this->db()->transactionLevel()); $this->assertSame(0, $this->rowCount()); } public function test_transaction_closure_commits(): void { $result = $this->db()->transaction(function () { $this->insertRow(1, 'Closure'); return 'success'; }); $this->assertSame('success', $result); $this->assertSame(1, $this->rowCount()); $this->assertSame(0, $this->db()->transactionLevel()); } public function test_transaction_closure_rolls_back_on_exception(): void { try { $this->db()->transaction(function () { $this->insertRow(1, 'Rollback'); throw new RuntimeException('Expected failure'); }); $this->fail('Expected exception was not thrown.'); } catch (RuntimeException $e) { $this->assertSame('Expected failure', $e->getMessage()); } $this->assertSame(0, $this->rowCount()); $this->assertSame(0, $this->db()->transactionLevel()); } public function test_nested_transaction_commit(): void { $db = $this->db(); $db->beginTransaction(); $this->insertRow(1, 'Outer'); $db->beginTransaction(); $this->insertRow(2, 'Inner'); $this->assertSame(2, $db->transactionLevel()); $db->commit(); $this->assertSame(1, $db->transactionLevel()); $db->commit(); $this->assertSame(0, $db->transactionLevel()); $this->assertSame(2, $this->rowCount()); } public function test_nested_rollback_preserves_outer_transaction(): void { $db = $this->db(); $db->beginTransaction(); $this->insertRow(1, 'Outer'); $db->beginTransaction(); $this->insertRow(2, 'Inner'); $db->rollBack(); $this->assertSame(1, $db->transactionLevel()); $this->assertSame(1, $this->rowCount()); $db->commit(); $this->assertSame(0, $db->transactionLevel()); $this->assertSame(1, $this->rowCount()); } public function test_nested_exception_preserves_outer_changes(): void { $db = $this->db(); $db->beginTransaction(); $this->insertRow(1, 'Outer'); try { $db->transaction(function () { $this->insertRow(2, 'Inner'); throw new RuntimeException('Inner failure'); }); } catch (RuntimeException $e) { $this->assertSame('Inner failure', $e->getMessage()); } $this->assertSame(1, $db->transactionLevel()); $db->commit(); $this->assertSame(1, $this->rowCount()); } public function test_sql_error_rolls_back_transaction(): void { $db = $this->db(); try { $db->transaction(function () { $this->insertRow(1, 'Valid'); $this->insertRow(1, 'Duplicate'); }); $this->fail('Expected duplicate-key exception.'); } catch (QueryException $e) { $this->assertSame('23505', $e->getCode()); } $this->assertSame(0, $db->transactionLevel()); $this->assertSame(0, $this->rowCount()); } public function test_transaction_level_is_restored(): void { $db = $this->db(); $this->assertSame(0, $db->transactionLevel()); $db->beginTransaction(); $this->assertSame(1, $db->transactionLevel()); $db->beginTransaction(); $this->assertSame(2, $db->transactionLevel()); $db->rollBack(); $this->assertSame(1, $db->transactionLevel()); $db->rollBack(); $this->assertSame(0, $db->transactionLevel()); } }