function TransactionTest::testCommitWithActiveSavepoint

Tests committing a transaction while savepoints are active.

File

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

Class

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

Namespace

Drupal\KernelTests\Core\Database

Code

public function testCommitWithActiveSavepoint() : void {
    $transaction = $this->createRootTransaction();
    // phpcs:ignore DrupalPractice.CodeAnalysis.VariableAnalysis
    $savepoint1 = $this->createFirstSavepointTransaction('', FALSE);
    // Starts a savepoint transaction. Corresponds to 'SAVEPOINT savepoint_2'
    // on the database.
    $savepoint2 = $this->connection
        ->startTransaction();
    $this->assertSame(3, $this->connection
        ->transactionManager()
        ->stackDepth());
    $this->insertRow('row');
    // Commit the root transaction.
    $transaction->commitOrRelease();
    // Since we have committed the outer (root) Transaction object, the inner
    // (savepoint) ones have been dropped by the database already, and we are
    // no longer in an active transaction state.
    $this->assertSame(0, $this->connection
        ->transactionManager()
        ->stackDepth());
    $this->assertFalse($this->connection
        ->inTransaction());
    $this->assertRowPresent('row');
    // Trying to release the inner (savepoint) Transaction object, throws an
    // exception since it was dropped by the database already, and removed from
    // our transaction stack.
    $this->expectException(TransactionOutOfOrderException::class);
    $this->expectExceptionMessageMatches("/^Error attempting commit of .*\\\\savepoint_2\\. Active stack: .* empty/");
    $savepoint2->commitOrRelease();
}

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