function SelectComplexTest::testCountQuery

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Database/SelectComplexTest.php \Drupal\KernelTests\Core\Database\SelectComplexTest::testCountQuery()
  2. 10 core/tests/Drupal/KernelTests/Core/Database/SelectComplexTest.php \Drupal\KernelTests\Core\Database\SelectComplexTest::testCountQuery()
  3. 11.x 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 198

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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.