function TransactionTest::testQueryFailureInTransaction
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php \Drupal\KernelTests\Core\Database\TransactionTest::testQueryFailureInTransaction()
- 8.9.x 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 803
Class
- TransactionTest
- Tests the transactions, using the explicit ::commitOrRelease method.
Namespace
Drupal\KernelTests\Core\DatabaseCode
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) {
// 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) {
// 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) {
// 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) {
// 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) {
// 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) {
// 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) {
// 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.
if ($this->connection
->supportsTransactionalDDL()) {
$transaction->commitOrRelease();
}
else {
set_error_handler(static function (int $errno, string $errstr) : bool {
throw new \ErrorException($errstr);
});
try {
$transaction->commitOrRelease();
} catch (\ErrorException $e) {
$this->assertSame('Transaction::commitOrRelease() was not processed because a prior execution of a DDL statement already committed the transaction.', $e->getMessage());
} finally {
restore_error_handler();
}
}
$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.