function DatabaseExceptionWrapperTest::testPreparedStatement

Tests the expected database exception thrown for prepared statements.

File

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

Class

DatabaseExceptionWrapperTest
Tests exceptions thrown by queries.

Namespace

Drupal\KernelTests\Core\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 (\Exception $e) {
        $this->assertTrue($e instanceof \PDOException || $e instanceof DatabaseExceptionWrapper, 'Exception should be an instance of \\PDOException or DatabaseExceptionWrapper, thrown ' . get_class($e));
    }
}

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