function SelectTest::testUnionAll

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

Tests that we can UNION ALL multiple SELECT queries together.

File

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

Class

SelectTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testUnionAll() {
    $query_1 = $this->connection
        ->select('test', 't')
        ->fields('t', [
        'name',
    ])
        ->condition('age', [
        27,
        28,
    ], 'IN');
    $query_2 = $this->connection
        ->select('test', 't')
        ->fields('t', [
        'name',
    ])
        ->condition('age', 28);
    $query_1->union($query_2, 'ALL');
    $names = $query_1->execute()
        ->fetchCol();
    // Ensure we get all 3 records.
    $this->assertCount(3, $names, 'UNION ALL correctly preserved duplicates.');
    $this->assertEquals('George', $names[0], 'First query returned correct first name.');
    $this->assertEquals('Ringo', $names[1], 'Second query returned correct second name.');
    $this->assertEquals('Ringo', $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.