function TransactionTest::transactionInnerLayer

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php \Drupal\KernelTests\Core\Database\TransactionTest::transactionInnerLayer()
  2. 8.9.x 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

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

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 155

Class

TransactionTest
Tests the transactions, using the explicit ::commitOrRelease method.

Namespace

Drupal\KernelTests\Core\Database

Code

protected function transactionInnerLayer(string $suffix) : void {
    $depth = $this->connection
        ->transactionManager()
        ->stackDepth();
    // 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
        ->transactionManager()
        ->stackDepth();
    $this->assertSame($depth + 1, $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.');
    $txn->commitOrRelease();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.