DatabaseExceptionWrapperTest.php in Drupal 10
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;
class DatabaseExceptionWrapperTest extends KernelTestBase {
public function testPrepareStatementFailOnExecution() {
$connection = Database::getConnection();
$connection_reflection_class = new \ReflectionClass($connection);
$client_connection_property = $connection_reflection_class
->getProperty('connection');
$client_connection = $client_connection_property
->getValue($connection);
if (!$client_connection instanceof \PDO) {
$this
->markTestSkipped("This tests can only run for drivers wrapping \\PDO connections.");
}
$this
->expectException(\PDOException::class);
$stmt = $connection
->prepareStatement('bananas', []);
$stmt
->execute();
}
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', []);
}
public function testQueryThrowsDatabaseExceptionWrapperException() {
$this
->expectException(DatabaseExceptionWrapper::class);
Database::getConnection()
->query('SELECT * FROM {does_not_exist}');
}
}