3 namespace Drupal\KernelTests\Core\Database;
5 use Drupal\Core\Database\DatabaseExceptionWrapper;
6 use Drupal\Core\Database\Database;
7 use Drupal\KernelTests\KernelTestBase;
10 * Tests exceptions thrown by queries.
14 class DatabaseExceptionWrapperTest extends KernelTestBase {
17 * Tests the expected database exception thrown for prepared statements.
19 public function testPreparedStatement() {
20 $connection = Database::getConnection();
22 // SQLite validates the syntax upon preparing a statement already.
23 // @throws \PDOException
24 $query = $connection->prepare('bananas');
26 // MySQL only validates the syntax upon trying to execute a query.
27 // @throws \Drupal\Core\Database\DatabaseExceptionWrapper
28 $connection->query($query);
30 $this->fail('Expected PDOException or DatabaseExceptionWrapper, none was thrown.');
32 catch (\PDOException $e) {
33 $this->pass('Expected PDOException was thrown.');
35 catch (DatabaseExceptionWrapper $e) {
36 $this->pass('Expected DatabaseExceptionWrapper was thrown.');
38 catch (\Exception $e) {
39 $this->fail("Thrown exception is not a PDOException:\n" . (string) $e);
44 * Tests the expected database exception thrown for inexistent tables.
46 public function testQueryThrowsDatabaseExceptionWrapperException() {
47 $connection = Database::getConnection();
49 $connection->query('SELECT * FROM {does_not_exist}');
50 $this->fail('Expected PDOException, none was thrown.');
52 catch (DatabaseExceptionWrapper $e) {
53 $this->pass('Expected DatabaseExceptionWrapper was thrown.');
55 catch (\Exception $e) {
56 $this->fail("Thrown exception is not a DatabaseExceptionWrapper:\n" . (string) $e);