You are here

function SelectComplexTest::testCountQueryGroupBy in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Database/SelectComplexTest.php \Drupal\system\Tests\Database\SelectComplexTest::testCountQueryGroupBy()

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

File

core/modules/system/src/Tests/Database/SelectComplexTest.php, line 272
Contains \Drupal\system\Tests\Database\SelectComplexTest.

Class

SelectComplexTest
Tests the Select query builder with more complex queries.

Namespace

Drupal\system\Tests\Database

Code

function testCountQueryGroupBy() {
  $query = db_select('test_task');
  $query
    ->addField('test_task', 'pid');
  $query
    ->groupBy('pid');
  $count = $query
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 3, 'Counted the correct number of records.');

  // Use a column alias as, without one, the query can succeed for the wrong
  // reason.
  $query = db_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
    ->assertEqual($count, 3, 'Counted the correct number of records.');
}