You are here

public function SelectOrderedTest::testSimpleSelectOrderedDesc in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/SelectOrderedTest.php \Drupal\KernelTests\Core\Database\SelectOrderedTest::testSimpleSelectOrderedDesc()

Tests ORDER BY descending.

File

core/tests/Drupal/KernelTests/Core/Database/SelectOrderedTest.php, line 67

Class

SelectOrderedTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testSimpleSelectOrderedDesc() {
  $query = $this->connection
    ->select('test');
  $query
    ->addField('test', 'name');
  $age_field = $query
    ->addField('test', 'age', 'age');
  $query
    ->orderBy($age_field, 'DESC');
  $result = $query
    ->execute();
  $num_records = 0;
  $last_age = 100000000;
  foreach ($result as $record) {
    $num_records++;
    $this
      ->assertTrue($record->age <= $last_age, 'Results returned in correct order.');
    $last_age = $record->age;
  }
  $this
    ->assertEqual($num_records, 4, 'Returned the correct number of rows.');
}