You are here

public function SelectTest::testUnion in Drupal 9

Same name and namespace in other branches
  1. 8 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()

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);
}