You are here

function DatabaseSelectComplexTestCase::testCountQuery in Drupal 7

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

File

modules/simpletest/tests/database_test.test, line 2175

Class

DatabaseSelectComplexTestCase
Test more complex select statements.

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