function DriverSpecificTransactionTestBase::testQueryFailureInTransaction

Same name in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Database/DriverSpecificTransactionTestBase.php \Drupal\KernelTests\Core\Database\DriverSpecificTransactionTestBase::testQueryFailureInTransaction()

Tests that transactions can continue to be used if a query fails.

File

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

Class

DriverSpecificTransactionTestBase
Tests the transaction abstraction system.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testQueryFailureInTransaction() : void {
    $transaction = $this->createRootTransaction('test_transaction', FALSE);
    $this->connection
        ->schema()
        ->dropTable('test');
    // Test a failed query using the query() method.
    try {
        $this->connection
            ->query('SELECT [age] FROM {test} WHERE [name] = :name', [
            ':name' => 'David',
        ])
            ->fetchField();
        $this->fail('Using the query method should have failed.');
    } catch (\Exception $e) {
        // Just continue testing.
    }
    // Test a failed select query.
    try {
        $this->connection
            ->select('test')
            ->fields('test', [
            'name',
        ])
            ->execute();
        $this->fail('Select query should have failed.');
    } catch (\Exception $e) {
        // Just continue testing.
    }
    // Test a failed insert query.
    try {
        $this->connection
            ->insert('test')
            ->fields([
            'name' => 'David',
            'age' => '24',
        ])
            ->execute();
        $this->fail('Insert query should have failed.');
    } catch (\Exception $e) {
        // Just continue testing.
    }
    // Test a failed update query.
    try {
        $this->connection
            ->update('test')
            ->fields([
            'name' => 'Tiffany',
        ])
            ->condition('id', 1)
            ->execute();
        $this->fail('Update query should have failed.');
    } catch (\Exception $e) {
        // Just continue testing.
    }
    // Test a failed delete query.
    try {
        $this->connection
            ->delete('test')
            ->condition('id', 1)
            ->execute();
        $this->fail('Delete query should have failed.');
    } catch (\Exception $e) {
        // Just continue testing.
    }
    // Test a failed merge query.
    try {
        $this->connection
            ->merge('test')
            ->key('job', 'Presenter')
            ->fields([
            'age' => '31',
            'name' => 'Tiffany',
        ])
            ->execute();
        $this->fail('Merge query should have failed.');
    } catch (\Exception $e) {
        // Just continue testing.
    }
    // Test a failed upsert query.
    try {
        $this->connection
            ->upsert('test')
            ->key('job')
            ->fields([
            'job',
            'age',
            'name',
        ])
            ->values([
            'job' => 'Presenter',
            'age' => 31,
            'name' => 'Tiffany',
        ])
            ->execute();
        $this->fail('Upsert query should have failed.');
    } catch (\Exception $e) {
        // Just continue testing.
    }
    // Create the missing schema and insert a row.
    $this->installSchema('database_test', [
        'test',
    ]);
    $this->connection
        ->insert('test')
        ->fields([
        'name' => 'David',
        'age' => '24',
    ])
        ->execute();
    // Commit the transaction.
    unset($transaction);
    $saved_age = $this->connection
        ->query('SELECT [age] FROM {test} WHERE [name] = :name', [
        ':name' => 'David',
    ])
        ->fetchField();
    $this->assertEquals('24', $saved_age);
}

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