function SelectOrderedTest::testSimpleSelectMultiOrdered

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Database/SelectOrderedTest.php \Drupal\KernelTests\Core\Database\SelectOrderedTest::testSimpleSelectMultiOrdered()
  2. 10 core/tests/Drupal/KernelTests/Core/Database/SelectOrderedTest.php \Drupal\KernelTests\Core\Database\SelectOrderedTest::testSimpleSelectMultiOrdered()
  3. 11.x core/tests/Drupal/KernelTests/Core/Database/SelectOrderedTest.php \Drupal\KernelTests\Core\Database\SelectOrderedTest::testSimpleSelectMultiOrdered()

Tests multiple ORDER BY.

File

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

Class

SelectOrderedTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testSimpleSelectMultiOrdered() {
    $query = $this->connection
        ->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 = [
        [
            'Ringo',
            28,
            'Drummer',
        ],
        [
            'John',
            25,
            'Singer',
        ],
        [
            'George',
            27,
            'Singer',
        ],
        [
            '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->assertEquals(4, $num_records, 'Returned the correct number of rows.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.