function SelectTest::testUnion

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

Tests that we can UNION multiple Select queries together.

This is semantically equal to UNION DISTINCT, so we don't explicitly test that.

File

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

Class

SelectTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testUnion() {
    $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);
    $names = $query_1->execute()
        ->fetchCol();
    // Ensure we only get 2 records.
    $this->assertCount(2, $names, 'UNION correctly discarded duplicates.');
    $this->assertEqualsCanonicalizing([
        'George',
        'Ringo',
    ], $names);
}

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