/**
  * Test index.
  *
  * @param Connection $connection
  *   The connection to perform tests on.
  *
  * @dataProvider connectionDataProvider
  */
 public function testIndex(Connection $connection)
 {
     $indexer = new Indexer($connection);
     $connection->startTransaction();
     $indexer->index($connection->addMetadata('value', 'value1'), 'test1');
     $check = $indexer->lookup('test1');
     $this->assertSame('value1', reset($check)->getMetadata('value'), 'Operations not found during lookup.');
     $operation = new Operation();
     $operation->onCommit(function () {
         return 'testresult';
     });
     $connection->addOperation($operation);
     $indexer->index($operation, 'test2');
     $check = $indexer->lookup('test2');
     $this->assertSame([$operation->idx($connection) => $operation], $check, 'Operations not found during lookup.');
 }
 /**
  * Test commit transaction.
  *
  * @param Connection $connection
  *   The connection to perform tests on.
  *
  * @dataProvider connectionDataProvider
  */
 public function testNestedTransaction(Connection $connection)
 {
     $accumulator = '';
     $operation = new Operation();
     $operation->onRollback(function ($operation, $connection) use(&$accumulator) {
         $accumulator .= 'rollback';
         if ($connection->getDepth() > 0) {
             $connection->addOperation($operation);
         }
     });
     $connection->startTransaction();
     $connection->startTransaction();
     $connection->startTransaction();
     $connection->addOperation($operation);
     $connection->rollbackTransaction();
     $connection->rollbackTransaction();
     $connection->rollbackTransaction();
     $this->assertEquals('rollbackrollbackrollback', $accumulator, 'Nested rollback was not performed.');
 }
 /**
  * Test id.
  *
  * @param Operation $operation
  *   The operation to perform tests on.
  *
  * @dataProvider operationDataProvider
  */
 public function testIdx(Operation $operation)
 {
     $connection = new Connection();
     $operation->setIdx($connection, 'testid');
     $check = $operation->idx($connection);
     $this->assertSame('testid', $check, 'Correct id was not set.');
 }
use Gielfeldt\TransactionalPHP\Operation;
$connection = new Connection();
$operation = new Operation();
$operation->onCommit(function () {
    print "THIS WILL BE PRINTED IMMEDIATELY, BECAUSE NO TRANSACTION HAS BEGUN\n";
})->onRollback(function () {
    print "THIS WILL NEVER BE PRINTED, BECAUSE NO TRANSACTION HAS BEGUN\n";
});
$connection->addOperation($operation);
// Start outer transaction.
$connection->startTransaction();
$operation = new Operation();
$operation->onCommit(function () {
    print "THIS WILL BE PRINTED, BECAUSE THIS WILL BE COMMITTED\n";
})->onRollback(function () {
    print "THIS WILL NEVER BE PRINTED, BECAUSE THIS WILL BE COMMITTED\n";
});
$connection->addOperation($operation);
// Start inner transaction.
$connection->startTransaction();
$operation = new Operation();
$operation->onCommit(function () {
    print "THIS WILL NOT BE PRINTED, BECAUSE THIS WILL BE ROLLED BACK\n";
})->onRollback(function () {
    print "THIS WILL BE PRINTED, BECAUSE THIS WILL BE ROLLED BACK\n";
});
$connection->addOperation($operation);
// Rollback inner transaction.
$connection->rollbackTransaction();
// Commit inner transaction.
$connection->commitTransaction();