function TransactionTest::testRollbackAfterCommitSameSavepoint

Tests savepoint transaction rollback after commit.

File

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

Class

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

Namespace

Drupal\KernelTests\Core\Database

Code

public function testRollbackAfterCommitSameSavepoint() : void {
  $transaction = $this->createRootTransaction();
  $savepoint = $this->createFirstSavepointTransaction();
  // Release savepoint. Corresponds to 'RELEASE savepoint_1' on the database.
  $savepoint->commitOrRelease();
  $this->assertRowPresent('David');
  $this->assertRowPresent('Roger');
  $this->assertTrue($this->connection
    ->inTransaction());
  $this->assertSame(1, $this->connection
    ->transactionManager()
    ->stackDepth());
  // Insert a row.
  $this->insertRow('Syd');
  // Try rolling back savepoint. Should fail since it was released already.
  try {
    $savepoint->rollback();
    $this->fail('Expected TransactionOutOfOrderException was not thrown');
  } catch (\Exception $e) {
    $this->assertInstanceOf(TransactionOutOfOrderException::class, $e);
    $this->assertMatchesRegularExpression("/^Error attempting rollback of .*\\\\savepoint_1\\. Active stack: .*\\\\drupal_transaction/", $e->getMessage());
  }
  $this->assertRowPresent('David');
  $this->assertRowPresent('Roger');
  $this->assertRowPresent('Syd');
  $this->assertTrue($this->connection
    ->inTransaction());
  $this->assertSame(1, $this->connection
    ->transactionManager()
    ->stackDepth());
  // Commit root.
  $transaction->commitOrRelease();
  $this->assertRowPresent('David');
  $this->assertRowPresent('Roger');
  $this->assertRowPresent('Syd');
  $this->assertFalse($this->connection
    ->inTransaction());
  $this->assertSame(0, $this->connection
    ->transactionManager()
    ->stackDepth());
}

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