Skip to content

Commit

Permalink
Add NestedTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
emonkak committed Aug 30, 2018
1 parent 367cb67 commit 1e3a0b7
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 0 deletions.
99 changes: 99 additions & 0 deletions src/NestedTransaction.php
@@ -0,0 +1,99 @@
<?php

namespace Emonkak\Database;

class NestedTransaction implements PDOTransactionInterface
{
/**
* @var PDOInterface
*/
private $pdo;

/**
* @var integer
*/
private $level = 0;

/**
* @param PDOInterface $pdo
*/
public function __construct(PDOInterface $pdo)
{
$this->pdo = $pdo;
}

/**
* {@inheritDoc}
*/
public function beginTransaction()
{
if ($this->level > 0) {
$this->pdo->exec('SAVEPOINT ' . $this->getSavepoint());
$result = true;
} else {
$result = $this->pdo->beginTransaction();
}

$this->level++;

return $result;
}

/**
* {@inheritDoc}
*/
public function commit()
{
if ($this->level > 0) {
$this->level--;
}

if ($this->level > 0) {
$this->pdo->exec('RELEASE SAVEPOINT ' . $this->getSavepoint());
return true;
} else {
return $this->pdo->commit();
}
}

/**
* {@inheritDoc}
*/
public function inTransaction()
{
return $this->level > 0;
}

/**
* {@inheritDoc}
*/
public function rollback()
{
if ($this->level > 0) {
$this->level--;
}

if ($this->level > 0) {
$this->pdo->exec('ROLLBACK TO SAVEPOINT ' . $this->getSavepoint());
return true;
} else {
return $this->pdo->rollback();
}
}

/**
* @return integer
*/
public function getTransactionLevel()
{
return $this->level;
}

/**
* @return string
*/
private function getSavepoint()
{
return 'level_' . $this->level;
}
}
116 changes: 116 additions & 0 deletions tests/NestedTransactionTest.php
@@ -0,0 +1,116 @@
<?php

namespace Emonkak\Database\Tests;

use Emonkak\Database\NestedTransaction;
use Emonkak\Database\PDOInterface;

/**
* @covers Emonkak\Database\NestedTransaction
*/
class NestedTransactionTest extends \PHPUnit_Framework_TestCase
{
private $pdo;

private $nestedTransaction;

public function setUp()
{
$this->pdo = $this->createMock(PDOInterface::class);
$this->nestedTransaction = new NestedTransaction($this->pdo);
}

public function testCommit()
{
$this->pdo
->expects($this->at(0))
->method('beginTransaction')
->willReturn(true);
$this->pdo
->expects($this->at(1))
->method('exec')
->with($this->identicalTo('SAVEPOINT level_1'));
$this->pdo
->expects($this->at(2))
->method('exec')
->with($this->identicalTo('RELEASE SAVEPOINT level_1'));
$this->pdo
->expects($this->at(3))
->method('commit')
->willReturn(true);
$this->pdo
->expects($this->at(4))
->method('commit')
->willReturn(true);

$this->assertSame(0, $this->nestedTransaction->getTransactionLevel());
$this->assertFalse($this->nestedTransaction->inTransaction());

$this->assertTrue($this->nestedTransaction->beginTransaction());
$this->assertSame(1, $this->nestedTransaction->getTransactionLevel());
$this->assertTrue($this->nestedTransaction->inTransaction());

$this->assertTrue($this->nestedTransaction->beginTransaction());
$this->assertSame(2, $this->nestedTransaction->getTransactionLevel());
$this->assertTrue($this->nestedTransaction->inTransaction());

$this->assertTrue($this->nestedTransaction->commit());
$this->assertSame(1, $this->nestedTransaction->getTransactionLevel());
$this->assertTrue($this->nestedTransaction->inTransaction());

$this->assertTrue($this->nestedTransaction->commit());
$this->assertSame(0, $this->nestedTransaction->getTransactionLevel());
$this->assertFalse($this->nestedTransaction->inTransaction());

$this->assertTrue($this->nestedTransaction->commit());
$this->assertSame(0, $this->nestedTransaction->getTransactionLevel());
$this->assertFalse($this->nestedTransaction->inTransaction());
}

public function testRollback()
{
$this->pdo
->expects($this->at(0))
->method('beginTransaction')
->willReturn(true);
$this->pdo
->expects($this->at(1))
->method('exec')
->with($this->identicalTo('SAVEPOINT level_1'));
$this->pdo
->expects($this->at(2))
->method('exec')
->with($this->identicalTo('ROLLBACK TO SAVEPOINT level_1'));
$this->pdo
->expects($this->at(3))
->method('rollback')
->willReturn(true);
$this->pdo
->expects($this->at(4))
->method('rollback')
->willReturn(true);

$this->assertSame(0, $this->nestedTransaction->getTransactionLevel());
$this->assertFalse($this->nestedTransaction->inTransaction());

$this->assertTrue($this->nestedTransaction->beginTransaction());
$this->assertSame(1, $this->nestedTransaction->getTransactionLevel());
$this->assertTrue($this->nestedTransaction->inTransaction());

$this->assertTrue($this->nestedTransaction->beginTransaction());
$this->assertSame(2, $this->nestedTransaction->getTransactionLevel());
$this->assertTrue($this->nestedTransaction->inTransaction());

$this->assertTrue($this->nestedTransaction->rollback());
$this->assertSame(1, $this->nestedTransaction->getTransactionLevel());
$this->assertTrue($this->nestedTransaction->inTransaction());

$this->assertTrue($this->nestedTransaction->rollback());
$this->assertSame(0, $this->nestedTransaction->getTransactionLevel());
$this->assertFalse($this->nestedTransaction->inTransaction());

$this->assertTrue($this->nestedTransaction->rollback());
$this->assertSame(0, $this->nestedTransaction->getTransactionLevel());
$this->assertFalse($this->nestedTransaction->inTransaction());
}
}

0 comments on commit 1e3a0b7

Please sign in to comment.