You are here

public function DatabaseExceptionWrapperTest::testPrepareStatementFailOnPreparation in Drupal 9

Same name and namespace in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Database/DatabaseExceptionWrapperTest.php \Drupal\KernelTests\Core\Database\DatabaseExceptionWrapperTest::testPrepareStatementFailOnPreparation()

Tests Connection::prepareStatement exceptions on preparation.

Core database drivers use PDO emulated statements or the StatementPrefetch class, which defer the statement check to the moment of the execution. In order to test a failure at preparation time, we have to force the connection not to emulate statement preparation. Still, this is only valid for the MySql driver.

File

core/tests/Drupal/KernelTests/Core/Database/DatabaseExceptionWrapperTest.php, line 86

Class

DatabaseExceptionWrapperTest
Tests exceptions thrown by queries.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testPrepareStatementFailOnPreparation() {
  $driver = Database::getConnection()
    ->driver();
  if ($driver !== 'mysql') {
    $this
      ->markTestSkipped("MySql tests can not run for driver '{$driver}'.");
  }
  $connection_info = Database::getConnectionInfo('default');
  $connection_info['default']['pdo'][\PDO::ATTR_EMULATE_PREPARES] = FALSE;
  Database::addConnectionInfo('default', 'foo', $connection_info['default']);
  $foo_connection = Database::getConnection('foo', 'default');
  $this
    ->expectException(DatabaseExceptionWrapper::class);
  $stmt = $foo_connection
    ->prepareStatement('bananas', []);
}