<?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";
Exemplo n.º 2
0
 /**
  * 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.');
 }
Exemplo n.º 3
0
namespace Gielfeldt\TransactionalPHP\Example;

require 'vendor/autoload.php';
use Gielfeldt\TransactionalPHP\Connection;
use Gielfeldt\TransactionalPHP\Indexer;
use Gielfeldt\TransactionalPHP\Operation;
$connection = new Connection();
$indexer = new Indexer($connection);
$operation = (new 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";
})->onBuffer(function ($operation) use($indexer) {
    print "INDEXING test1\n";
    $indexer->index($operation, 'test1');
})->setMetadata('value', 'test1');
$connection->addOperation($operation);
// Start outer transaction.
$connection->startTransaction();
$operation = (new 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";
})->onBuffer(function ($operation) use($indexer) {
    print "INDEXING test2\n";
    $indexer->index($operation, 'test2');
})->setMetadata('value', 'test2');
$connection->addOperation($operation);
// Start inner transaction.
$connection->startTransaction();