You are here

public function SelectComplexTest::testCountQuery in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/SelectComplexTest.php \Drupal\KernelTests\Core\Database\SelectComplexTest::testCountQuery()
  2. 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 192

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
    ->assertEquals(4, $count, '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
    ->assertEquals('George', $record->{$name_field}, 'Correct data retrieved.');
  $this
    ->assertEquals(27, $record->{$age_field}, 'Correct data retrieved.');
}