You are here

public function SelectTest::testInvalidSelectCount in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/SelectTest.php \Drupal\KernelTests\Core\Database\SelectTest::testInvalidSelectCount()

Tests that an invalid count query throws an exception.

File

core/tests/Drupal/KernelTests/Core/Database/SelectTest.php, line 557

Class

SelectTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testInvalidSelectCount() {
  try {

    // This query will fail because the table does not exist.
    // Normally it would throw an exception but we are suppressing
    // it with the throw_exception option.
    $options['throw_exception'] = FALSE;
    $this->connection
      ->select('some_table_that_does_not_exist', 't', $options)
      ->fields('t')
      ->countQuery()
      ->execute();
  } catch (\Exception $e) {
    $this
      ->fail('$options[\'throw_exception\'] is FALSE, but Exception thrown for invalid query.');
  }
  try {

    // This query will fail because the table does not exist.
    $this->connection
      ->select('some_table_that_does_not_exist', 't')
      ->fields('t')
      ->countQuery()
      ->execute();
    $this
      ->fail('No Exception thrown.');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf(DatabaseExceptionWrapper::class, $e);
  }
}