You are here

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

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

File

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

Class

SelectComplexTest
Tests the Select query builder with more complex queries.

Namespace

Drupal\system\Tests\Database

Code

function testCountQuery() {
  $query = db_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.');
}