You are here

function SelectOrderedTest::testSimpleSelectMultiOrdered in Zircon Profile 8

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

Tests multiple ORDER BY.

File

core/modules/system/src/Tests/Database/SelectOrderedTest.php, line 41
Contains \Drupal\system\Tests\Database\SelectOrderedTest.

Class

SelectOrderedTest
Tests the Select query builder.

Namespace

Drupal\system\Tests\Database

Code

function testSimpleSelectMultiOrdered() {
  $query = db_select('test');
  $query
    ->addField('test', 'name');
  $age_field = $query
    ->addField('test', 'age', 'age');
  $job_field = $query
    ->addField('test', 'job');
  $query
    ->orderBy($job_field);
  $query
    ->orderBy($age_field);
  $result = $query
    ->execute();
  $num_records = 0;
  $expected = array(
    array(
      'Ringo',
      28,
      'Drummer',
    ),
    array(
      'John',
      25,
      'Singer',
    ),
    array(
      'George',
      27,
      'Singer',
    ),
    array(
      'Paul',
      26,
      'Songwriter',
    ),
  );
  $results = $result
    ->fetchAll(\PDO::FETCH_NUM);
  foreach ($expected as $k => $record) {
    $num_records++;
    foreach ($record as $kk => $col) {
      if ($expected[$k][$kk] != $results[$k][$kk]) {
        $this
          ->assertTrue(FALSE, 'Results returned in correct order.');
      }
    }
  }
  $this
    ->assertEqual($num_records, 4, 'Returned the correct number of rows.');
}