public function TransactionTest::testQueryFailureInTransaction in Drupal 8
Same name and namespace in other branches
- 9 core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php \Drupal\KernelTests\Core\Database\TransactionTest::testQueryFailureInTransaction()
- 10 core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php \Drupal\KernelTests\Core\Database\TransactionTest::testQueryFailureInTransaction()
Tests that transactions can continue to be used if a query fails.
File
- core/
tests/ Drupal/ KernelTests/ Core/ Database/ TransactionTest.php, line 492
Class
- TransactionTest
- Tests the transaction abstraction system.
Namespace
Drupal\KernelTests\Core\DatabaseCode
public function testQueryFailureInTransaction() {
$transaction = $this->connection
->startTransaction('test_transaction');
$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 sould 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
->assertEqual('24', $saved_age);
}