function SelectComplexTest::testCountQueryGroupBy

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/SelectComplexTest.php \Drupal\KernelTests\Core\Database\SelectComplexTest::testCountQueryGroupBy()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Database/SelectComplexTest.php \Drupal\KernelTests\Core\Database\SelectComplexTest::testCountQueryGroupBy()
  3. 11.x core/tests/Drupal/KernelTests/Core/Database/SelectComplexTest.php \Drupal\KernelTests\Core\Database\SelectComplexTest::testCountQueryGroupBy()

Tests that we can generate a count query from a query with GROUP BY.

File

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

Class

SelectComplexTest
Tests the Select query builder with more complex queries.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testCountQueryGroupBy() : void {
    $query = $this->connection
        ->select('test_task');
    $query->addField('test_task', 'pid');
    $query->groupBy('pid');
    $count = $query->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEquals(3, $count, 'Counted the correct number of records.');
    // Use a column alias as, without one, the query can succeed for the wrong
    // reason.
    $query = $this->connection
        ->select('test_task');
    $query->addField('test_task', 'pid', 'pid_alias');
    $query->addExpression('COUNT([test_task].[task])', 'count');
    $query->groupBy('pid_alias');
    $query->orderBy('pid_alias', 'asc');
    $count = $query->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEquals(3, $count, 'Counted the correct number of records.');
}

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