function DriverSpecificTransactionTestBase::transactionInnerLayer

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

File

core/tests/Drupal/KernelTests/Core/Database/DriverSpecificTransactionTestBase.php, line 155

Class

DriverSpecificTransactionTestBase
Tests the transaction abstraction system.

Namespace

Drupal\KernelTests\Core\Database

Code

protected function transactionInnerLayer($suffix, $rollback = FALSE, $ddl_statement = FALSE) {
    $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->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
            ->transactionManager()
            ->stackDepth(), 'Transaction has rolled back to the last savepoint after calling rollBack().');
    }
}

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