You are here

public function TransactionTest::testTransactionRollBackNotSupported in Drupal 8

Tests transaction rollback on a database that doesn't support transactions.

If the active driver supports transactions, this test does nothing.

File

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

Class

TransactionTest
Tests the transaction abstraction system.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testTransactionRollBackNotSupported() {

  // This test won't work right if transactions are supported.
  if ($this->connection
    ->supportsTransactions()) {
    $this
      ->markTestSkipped("The '{$this->connection->driver()}' database driver supports transactions.");
  }
  try {

    // Create two nested transactions. Attempt to roll back from the inner one.
    $this
      ->transactionOuterLayer('B', TRUE);

    // Because our current database claims to not support transactions,
    // the inserted rows should be present despite the attempt to roll back.
    $saved_age = $this->connection
      ->query('SELECT age FROM {test} WHERE name = :name', [
      ':name' => 'DavidB',
    ])
      ->fetchField();
    $this
      ->assertIdentical($saved_age, '24', 'DavidB not rolled back, since transactions are not supported.');
    $saved_age = $this->connection
      ->query('SELECT age FROM {test} WHERE name = :name', [
      ':name' => 'DanielB',
    ])
      ->fetchField();
    $this
      ->assertIdentical($saved_age, '19', 'DanielB not rolled back, since transactions are not supported.');
  } catch (\Exception $e) {
    $this
      ->fail($e
      ->getMessage());
  }
}