You are here

function DatabaseSelectComplexTestCase::testCountQuery in SimpleTest 7

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

File

tests/database_test.test, line 1818

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, t('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', t('Correct data retrieved.'));
  $this
    ->assertEqual($record->{$age_field}, 27, t('Correct data retrieved.'));
}