You are here

public function SelectComplexTest::testCountQuery 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::testCountQuery()

Tests that we can generate a count query from a built query.

File

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

Class

SelectComplexTest
Tests the Select query builder with more complex queries.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testCountQuery() {
  $query = $this->connection
    ->select('test');
  $name_field = $query
    ->addField('test', 'name');
  $age_field = $query
    ->addField('test', 'age', 'age');
  $query
    ->orderBy('name');
  $count = $query
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 4, 'Counted the correct number of records.');

  // Now make sure we didn't break the original query!  We should still have
  // all of the fields we asked for.
  $record = $query
    ->execute()
    ->fetch();
  $this
    ->assertEqual($record->{$name_field}, 'George', 'Correct data retrieved.');
  $this
    ->assertEqual($record->{$age_field}, 27, 'Correct data retrieved.');
}