/**
  * Test get operations
  *
  * @param Connection $connection
  *   The connection to perform tests on.
  *
  * @dataProvider connectionDataProvider
  */
 public function testGetOperations(Connection $connection)
 {
     $indexer = new Indexer($connection);
     $connection->startTransaction();
     $operation1 = $connection->addMetadata('value', 'value1');
     $operation2 = $connection->addMetadata('value', 'value2');
     $operation3 = $connection->addMetadata('value', 'value3');
     $indexer->index($operation1, 'test1');
     $indexer->index($operation2, 'test1');
     $indexer->index($operation3, 'test3');
     $expected = [$operation1->idx($connection) => $operation1, $operation2->idx($connection) => $operation2, $operation3->idx($connection) => $operation3];
     $check = $indexer->getOperations();
     $this->assertSame($expected, $check, 'Correct operations not found in indexer.');
     $expected = ['test1' => [$operation1->idx($connection) => $operation1, $operation2->idx($connection) => $operation2], 'test3' => [$operation3->idx($connection) => $operation3]];
     $check = $indexer->getIndex();
     $this->assertSame($expected, $check, 'Correct operations not found in indexer.');
 }
Example #2
0
<?php

namespace Gielfeldt\TransactionalPHP\Example;

require 'vendor/autoload.php';
use Gielfeldt\TransactionalPHP\Connection;
$connection = new Connection();
// Start outer transaction.
$connection->startTransaction();
$connection->onCommit(function () {
    print "THIS WILL BE PRINTED, BECAUSE THIS WILL BE COMMITTED\n";
});
// Start inner transaction.
$connection->startTransaction();
$connection->onCommit(function () {
    print "THIS WILL NOT BE PRINTED, BECAUSE THIS WILL BE ROLLED BACK\n";
});
// Rollback inner transaction.
$connection->rollbackTransaction();
// Commit inner transaction.
$connection->commitTransaction();
<?php

namespace Gielfeldt\TransactionalPHP\Example;

require 'vendor/autoload.php';
use Gielfeldt\TransactionalPHP\Connection;
use Gielfeldt\TransactionalPHP\Indexer;
$connection = new Connection();
$indexer = new Indexer($connection);
// Start outer transaction.
$connection->startTransaction();
print "Started outer transaction\n";
$indexer->index($connection->addMetadata('value', 'value1'), 'test1');
$indexer->index($connection->addMetadata('value', 'value2'), 'test1');
$indexer->index($connection->addMetadata('value', 'value1'), 'test2');
$indexer->index($connection->addMetadata('value', 'value2'), 'test2');
print "Added data to indexer\n";
foreach ($indexer->lookup('test1') as $operation) {
    print "Looked up test1 - found: " . $operation->getMetadata('value') . "\n";
}
foreach ($indexer->lookup('test2') as $operation) {
    print "Looked up test2 - found: " . $operation->getMetadata('value') . "\n";
}
// Start inner transaction.
$connection->startTransaction();
print "Started inner transaction\n";
$indexer->index($connection->addMetadata('value', 'value3'), 'test1');
$indexer->index($connection->addMetadata('value', 'value3'), 'test2');
print "Added data to indexer\n";
foreach ($indexer->lookup('test1') as $operation) {
    print "Looked up test1 - found: " . $operation->getMetadata('value') . "\n";
 /**
  * Test transaction depth.
  *
  * @param Connection $connection
  *   The connection to perform tests on.
  *
  * @dataProvider connectionDataProvider
  */
 public function testDepth(Connection $connection)
 {
     $this->assertEquals(0, $connection->getDepth(), 'Depth was not correct');
     $connection->startTransaction();
     $this->assertEquals(1, $connection->getDepth(), 'Depth was not correct');
     $connection->startTransaction();
     $this->assertEquals(2, $connection->getDepth(), 'Depth was not correct');
     $connection->rollbackTransaction();
     $this->assertEquals(1, $connection->getDepth(), 'Depth was not correct');
     $connection->commitTransaction();
     $this->assertEquals(0, $connection->getDepth(), 'Depth was not correct');
 }
<?php

namespace Gielfeldt\TransactionalPHP\Example;

require 'vendor/autoload.php';
use Gielfeldt\TransactionalPHP\Connection;
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";