Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |