DatabaseExceptionWrapperTest.php

Same filename and directory in other branches
  1. 9 core/modules/sqlite/tests/src/Kernel/sqlite/DatabaseExceptionWrapperTest.php
  2. 9 core/modules/mysql/tests/src/Kernel/mysql/DatabaseExceptionWrapperTest.php
  3. 9 core/modules/pgsql/tests/src/Kernel/pgsql/DatabaseExceptionWrapperTest.php
  4. 9 core/tests/Drupal/KernelTests/Core/Database/DatabaseExceptionWrapperTest.php
  5. 10 core/modules/sqlite/tests/src/Kernel/sqlite/DatabaseExceptionWrapperTest.php
  6. 10 core/modules/mysql/tests/src/Kernel/mysql/DatabaseExceptionWrapperTest.php
  7. 10 core/modules/pgsql/tests/src/Kernel/pgsql/DatabaseExceptionWrapperTest.php
  8. 10 core/tests/Drupal/KernelTests/Core/Database/DatabaseExceptionWrapperTest.php
  9. 11.x core/modules/sqlite/tests/src/Kernel/sqlite/DatabaseExceptionWrapperTest.php
  10. 11.x core/modules/mysql/tests/src/Kernel/mysql/DatabaseExceptionWrapperTest.php
  11. 11.x core/modules/pgsql/tests/src/Kernel/pgsql/DatabaseExceptionWrapperTest.php
  12. 11.x core/tests/Drupal/KernelTests/Core/Database/DatabaseExceptionWrapperTest.php

Namespace

Drupal\KernelTests\Core\Database

File

core/tests/Drupal/KernelTests/Core/Database/DatabaseExceptionWrapperTest.php

View source
<?php

namespace Drupal\KernelTests\Core\Database;

use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Database\Database;
use Drupal\KernelTests\KernelTestBase;

/**
 * Tests exceptions thrown by queries.
 *
 * @group Database
 */
class DatabaseExceptionWrapperTest extends KernelTestBase {
    
    /**
     * Tests the expected database exception thrown for prepared statements.
     */
    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));
        }
    }
    
    /**
     * Tests the expected database exception thrown for inexistent tables.
     */
    public function testQueryThrowsDatabaseExceptionWrapperException() {
        $this->expectException(DatabaseExceptionWrapper::class);
        Database::getConnection()->query('SELECT * FROM {does_not_exist}');
    }

}

Classes

Title Deprecated Summary
DatabaseExceptionWrapperTest Tests exceptions thrown by queries.

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