function SelectTest::testUnionOrderLimit

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/SelectTest.php \Drupal\KernelTests\Core\Database\SelectTest::testUnionOrderLimit()
  2. 10 core/tests/Drupal/KernelTests/Core/Database/SelectTest.php \Drupal\KernelTests\Core\Database\SelectTest::testUnionOrderLimit()
  3. 11.x 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 404

Class

SelectTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testUnionOrderLimit() {
    // 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->assertEqual($names[0], 'Ringo', 'First query returned correct name.');
    $this->assertEqual($names[1], 'Paul', 'Second query returned correct name.');
}

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