function SelectTest::testUnionOrderLimit

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

Tests that we can UNION multiple Select queries together with and a LIMIT.

File

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

Class

SelectTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testUnionOrderLimit() : 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');
    $query_1->range(0, 2);
    $names = $query_1->execute()
        ->fetchCol();
    // Ensure we get all only 2 of the 3 records.
    $this->assertCount(2, $names, 'UNION with a limit 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.');
}

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