You are here

function DatabaseTransactionTestCase::testTransactionRollBackSupported in SimpleTest 7

Test transaction rollback on a database that supports transactions.

If the active connection does not support transactions, this test does nothing.

File

tests/database_test.test, line 2825

Class

DatabaseTransactionTestCase
Test transaction support, particularly nesting.

Code

function testTransactionRollBackSupported() {

  // This test won't work right if transactions are not supported.
  if (!Database::getConnection()
    ->supportsTransactions()) {
    return;
  }
  try {

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

    // Neither of the rows we inserted in the two transaction layers
    // should be present in the tables post-rollback.
    $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(
      ':name' => 'DavidB',
    ))
      ->fetchField();
    $this
      ->assertNotIdentical($saved_age, '24', t('Cannot retrieve DavidB row after commit.'));
    $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(
      ':name' => 'DanielB',
    ))
      ->fetchField();
    $this
      ->assertNotIdentical($saved_age, '19', t('Cannot retrieve DanielB row after commit.'));
  } catch (Exception $e) {
    $this
      ->fail($e
      ->getMessage());
  }
}