DatabaseExceptionWrapperTest.php

Same filename in this branch
  1. 10 core/modules/sqlite/tests/src/Kernel/sqlite/DatabaseExceptionWrapperTest.php
  2. 10 core/modules/pgsql/tests/src/Kernel/pgsql/DatabaseExceptionWrapperTest.php
  3. 10 core/tests/Drupal/KernelTests/Core/Database/DatabaseExceptionWrapperTest.php
Same filename 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. 8.9.x core/tests/Drupal/KernelTests/Core/Database/DatabaseExceptionWrapperTest.php
  6. 11.x core/modules/sqlite/tests/src/Kernel/sqlite/DatabaseExceptionWrapperTest.php
  7. 11.x core/modules/mysql/tests/src/Kernel/mysql/DatabaseExceptionWrapperTest.php
  8. 11.x core/modules/pgsql/tests/src/Kernel/pgsql/DatabaseExceptionWrapperTest.php
  9. 11.x core/tests/Drupal/KernelTests/Core/Database/DatabaseExceptionWrapperTest.php

Namespace

Drupal\Tests\mysql\Kernel\mysql

File

core/modules/mysql/tests/src/Kernel/mysql/DatabaseExceptionWrapperTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\mysql\Kernel\mysql;

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

/**
 * Tests exceptions thrown by queries.
 *
 * @group Database
 */
class DatabaseExceptionWrapperTest extends DriverSpecificKernelTestBase {
    
    /**
     * 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.
     */
    public function testPrepareStatementFailOnPreparation() : void {
        $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', []);
    }
    
    /**
     * Tests Connection::prepareStatement exception on execution.
     */
    public function testPrepareStatementFailOnExecution() : void {
        $this->expectException(\PDOException::class);
        $stmt = $this->connection
            ->prepareStatement('bananas', []);
        $stmt->execute();
    }

}

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.