protected function TransactionTest::transactionInnerLayer

Creates an "inner layer" transaction.

This "inner layer" transaction is either used alone or nested inside of the "outer layer" transaction.

Parameters

$suffix: Suffix to add to field values to differentiate tests.

$rollback: Whether or not to try rolling back the transaction when we're done.

$ddl_statement: Whether to execute a DDL statement during the transaction.

1 call to TransactionTest::transactionInnerLayer()
TransactionTest::transactionOuterLayer in drupal/core/modules/system/lib/Drupal/system/Tests/Database/TransactionTest.php
Encapsulates a transaction's "inner layer" with an "outer layer".

File

drupal/core/modules/system/lib/Drupal/system/Tests/Database/TransactionTest.php, line 110
Definition of Drupal\system\Tests\Database\TransactionTest.

Class

TransactionTest
Tests transaction support, particularly nesting.

Namespace

Drupal\system\Tests\Database

Code

protected function transactionInnerLayer($suffix, $rollback = FALSE, $ddl_statement = FALSE) {
  $connection = Database::getConnection();
  $depth = $connection
    ->transactionDepth();

  // Start a transaction. If we're being called from ->transactionOuterLayer,
  // then we're already in a transaction. Normally, that would make starting
  // a transaction here dangerous, but the database API handles this problem
  // for us by tracking the nesting and avoiding the danger.
  $txn = db_transaction();
  $depth2 = $connection
    ->transactionDepth();
  $this
    ->assertTrue($depth < $depth2, 'Transaction depth is has increased with new transaction.');

  // Insert a single row into the testing table.
  db_insert('test')
    ->fields(array(
    'name' => 'Daniel' . $suffix,
    'age' => '19',
  ))
    ->execute();
  $this
    ->assertTrue($connection
    ->inTransaction(), 'In transaction inside nested transaction.');
  if ($ddl_statement) {
    $table = array(
      'fields' => array(
        'id' => array(
          'type' => 'serial',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
      ),
      'primary key' => array(
        'id',
      ),
    );
    db_create_table('database_test_1', $table);
    $this
      ->assertTrue($connection
      ->inTransaction(), 'In transaction inside nested transaction.');
  }
  if ($rollback) {

    // Roll back the transaction, if requested.
    // This rollback should propagate to the last savepoint.
    $txn
      ->rollback();
    $this
      ->assertTrue($connection
      ->transactionDepth() == $depth, 'Transaction has rolled back to the last savepoint after calling rollback().');
  }
}