You are here

function SelectTest::testUnion in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Database/SelectTest.php \Drupal\system\Tests\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/modules/system/src/Tests/Database/SelectTest.php, line 264
Contains \Drupal\system\Tests\Database\SelectTest.

Class

SelectTest
Tests the Select query builder.

Namespace

Drupal\system\Tests\Database

Code

function testUnion() {
  $query_1 = db_select('test', 't')
    ->fields('t', array(
    'name',
  ))
    ->condition('age', array(
    27,
    28,
  ), 'IN');
  $query_2 = db_select('test', 't')
    ->fields('t', array(
    'name',
  ))
    ->condition('age', 28);
  $query_1
    ->union($query_2);
  $names = $query_1
    ->execute()
    ->fetchCol();

  // Ensure we only get 2 records.
  $this
    ->assertEqual(count($names), 2, 'UNION correctly discarded duplicates.');
  $this
    ->assertEqual($names[0], 'George', 'First query returned correct name.');
  $this
    ->assertEqual($names[1], 'Ringo', 'Second query returned correct name.');
}