You are here

public function DatabaseExceptionWrapperTest::testPreparedStatement in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Database/DatabaseExceptionWrapperTest.php \Drupal\system\Tests\Database\DatabaseExceptionWrapperTest::testPreparedStatement()

Tests the expected database exception thrown for prepared statements.

File

core/modules/system/src/Tests/Database/DatabaseExceptionWrapperTest.php, line 24
Contains \Drupal\system\Tests\Database\DatabaseExceptionWrapperTest.

Class

DatabaseExceptionWrapperTest
Tests exceptions thrown by queries.

Namespace

Drupal\system\Tests\Database

Code

public function testPreparedStatement() {
  $connection = Database::getConnection();
  try {

    // SQLite validates the syntax upon preparing a statement already.
    // @throws \PDOException
    $query = $connection
      ->prepare('bananas');

    // MySQL only validates the syntax upon trying to execute a query.
    // @throws \Drupal\Core\Database\DatabaseExceptionWrapper
    $connection
      ->query($query);
    $this
      ->fail('Expected PDOException or DatabaseExceptionWrapper, none was thrown.');
  } catch (\PDOException $e) {
    $this
      ->pass('Expected PDOException was thrown.');
  } catch (DatabaseExceptionWrapper $e) {
    $this
      ->pass('Expected DatabaseExceptionWrapper was thrown.');
  } catch (\Exception $e) {
    $this
      ->fail("Thrown exception is not a PDOException:\n" . (string) $e);
  }
}