Skip to content

Commit

Permalink
Add test for transaction nesting (issue #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
martok committed Dec 17, 2023
1 parent c6c6ce7 commit 9950192
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/RecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,58 @@ public function testGlobalTransactionRollback()
});
}

public function testTransactionNesting()
{
$this->assertFalse($this->db->isTransaction());
$this->assertEquals(0, $this->db->getTransactionDepth());
$user = Users::start([
'username' => 'testuser264',
'password' => 'password27',
'email' => '[email protected]',
'logins' => 1
]);
$this->assertEquals(1, $this->db->getTransactionDepth());

// Adapter transaction management
$this->db->beginTransaction();
$this->assertEquals(2, $this->db->getTransactionDepth());
$this->db->query("INSERT INTO users (username, password, email) values ('testuser266', 'password27', '[email protected]')");
$this->db->commit();
$this->assertTrue($this->db->isTransaction());
$this->assertEquals(1, $this->db->getTransactionDepth());

// Record transaction management
$admin = new Users([
'username' => 'testuser265',
'password' => 'password27',
'email' => '[email protected]',
'logins' => 1
]);
$admin->startTransaction();
$this->assertEquals(2, $this->db->getTransactionDepth());
$admin->save();
$this->assertEquals(1, $this->db->getTransactionDepth());

// Adapter transaction rollback, visibility
$this->db->beginTransaction();
$this->assertEquals(2, $this->db->getTransactionDepth());
$this->db->query("INSERT INTO users (username, password, email) values ('testuser267', 'password27', '[email protected]')");
$test = Users::findOne(['username' => 'testuser267']);
$this->assertNotNull($test->id);
$this->db->rollback();
$this->assertTrue($this->db->isTransaction());
$this->assertEquals(1, $this->db->getTransactionDepth());
$test = Users::findOne(['username' => 'testuser267']);
$this->assertNull($test->id);

// Commits outer transaction
$user->save();
$this->assertFalse($this->db->isTransaction());
$this->assertEquals(0, $this->db->getTransactionDepth());

$this->db->disconnect();
}

public function testFindWhere()
{
$user = new Users([
Expand Down

0 comments on commit 9950192

Please sign in to comment.