You are here

protected function TransactionTest::transactionInnerLayer in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php \Drupal\KernelTests\Core\Database\TransactionTest::transactionInnerLayer()
  2. 10 core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php \Drupal\KernelTests\Core\Database\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 core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php
Encapsulates a transaction's "inner layer" with an "outer layer".

File

core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php, line 96

Class

TransactionTest
Tests the transaction abstraction system.

Namespace

Drupal\KernelTests\Core\Database

Code

protected function transactionInnerLayer($suffix, $rollback = FALSE, $ddl_statement = FALSE) {
  $depth = $this->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 = $this->connection
    ->startTransaction();
  $depth2 = $this->connection
    ->transactionDepth();
  $this
    ->assertGreaterThan($depth, $depth2, 'Transaction depth has increased with new transaction.');

  // Insert a single row into the testing table.
  $this->connection
    ->insert('test')
    ->fields([
    'name' => 'Daniel' . $suffix,
    'age' => '19',
  ])
    ->execute();
  $this
    ->assertTrue($this->connection
    ->inTransaction(), 'In transaction inside nested transaction.');
  if ($ddl_statement) {
    $table = [
      'fields' => [
        'id' => [
          'type' => 'serial',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ],
      ],
      'primary key' => [
        'id',
      ],
    ];
    $this->connection
      ->schema()
      ->createTable('database_test_1', $table);
    $this
      ->assertTrue($this->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
      ->assertSame($depth, $this->connection
      ->transactionDepth(), 'Transaction has rolled back to the last savepoint after calling rollBack().');
  }
}