function SelectTest::testUnionOrder

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

Tests that we can UNION multiple Select queries together and set the ORDER.

File

core/tests/Drupal/KernelTests/Core/Database/SelectTest.php, line 376

Class

SelectTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testUnionOrder() : void {
    // This gives George and Ringo.
    $query_1 = $this->connection
        ->select('test', 't')
        ->fields('t', [
        'name',
    ])
        ->condition('age', [
        27,
        28,
    ], 'IN');
    // This gives Paul.
    $query_2 = $this->connection
        ->select('test', 't')
        ->fields('t', [
        'name',
    ])
        ->condition('age', 26);
    $query_1->union($query_2);
    $query_1->orderBy('name', 'DESC');
    $names = $query_1->execute()
        ->fetchCol();
    // Ensure we get all 3 records.
    $this->assertCount(3, $names, 'UNION returned rows from both queries.');
    // Ensure that the names are in the correct reverse alphabetical order,
    // regardless of which query they came from.
    $this->assertEquals('Ringo', $names[0], 'First query returned correct name.');
    $this->assertEquals('Paul', $names[1], 'Second query returned correct name.');
    $this->assertEquals('George', $names[2], 'Third query returned correct name.');
}

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