Skip to content

Commit

Permalink
[TransactionManagerTest] Added tests of transactional method.
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Apr 12, 2016
1 parent 38db631 commit cbcdf53
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions tests/TransactionManagerTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lightools\Tests;

use Dibi\Connection;
use ErrorException;
use Lightools\TransactionNesting\TransactionManager;
use Tester\Assert;
use Tester\Environment;
Expand All @@ -18,13 +19,23 @@ Environment::setup();
*/
class TransactionManagerTest extends TestCase {

public function testNesting() {
/**
* @var Connection
*/
private $connection;

protected function setUp() {
parent::setUp();

$connection = new Connection(array(
$this->connection = new Connection([
'driver' => 'sqlite3',
'database' => sys_get_temp_dir() . '/transaction-manager-test.sq3',
));
$manager = new TransactionManager($connection);
'database' => sys_get_temp_dir() . '/transaction-manager-test-' . uniqid() . '.sq3',
]);
$this->connection->query('CREATE TABLE [test] ([id] INT)');
}

public function testNesting() {
$manager = new TransactionManager($this->connection);

Assert::noError(function () use ($manager) {
$manager->transactional(function () use ($manager) {
Expand All @@ -35,6 +46,29 @@ class TransactionManagerTest extends TestCase {
});
}

public function testCommit() {
$manager = new TransactionManager($this->connection);

$manager->transactional(function () {
$this->connection->insert('test', ['id' => 1])->execute();
});

Assert::truthy($this->connection->fetch('SELECT [id] FROM [test]'));
}

public function testRollback() {
$manager = new TransactionManager($this->connection);

Assert::exception(function () use ($manager) {
$manager->transactional(function () {
$this->connection->insert('test', ['id' => 1])->execute();
throw new ErrorException;
});
}, ErrorException::class);

Assert::falsey($this->connection->fetchSingle('SELECT [id] FROM [test]'));
}

}

(new TransactionManagerTest)->run();

0 comments on commit cbcdf53

Please sign in to comment.