You are here

public function SelectComplexTest::testCountQueryFieldRemovals in Drupal 8

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

Tests that countQuery properly removes fields and expressions.

File

core/tests/Drupal/KernelTests/Core/Database/SelectComplexTest.php, line 252

Class

SelectComplexTest
Tests the Select query builder with more complex queries.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testCountQueryFieldRemovals() {

  // countQuery should remove all fields and expressions, so this can be
  // tested by adding a non-existent field and expression: if it ends
  // up in the query, an error will be thrown. If not, it will return the
  // number of records, which in this case happens to be 4 (there are four
  // records in the {test} table).
  $query = $this->connection
    ->select('test');
  $query
    ->fields('test', [
    'fail',
  ]);
  $this
    ->assertEqual(4, $query
    ->countQuery()
    ->execute()
    ->fetchField(), 'Count Query removed fields');
  $query = $this->connection
    ->select('test');
  $query
    ->addExpression('fail');
  $this
    ->assertEqual(4, $query
    ->countQuery()
    ->execute()
    ->fetchField(), 'Count Query removed expressions');
}