You are here

public function SelectTest::testUnionOrder in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/SelectTest.php \Drupal\KernelTests\Core\Database\SelectTest::testUnionOrder()

Tests that we can UNION multiple Select queries together and set the ORDER.

File

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

Class

SelectTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testUnionOrder() {

  // 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');
  $names = $query_1
    ->execute()
    ->fetchCol();

  // Ensure we get all 3 records.
  $this
    ->assertCount(3, $names, 'UNION 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.');
  $this
    ->assertEqual($names[2], 'George', 'Third query returned correct name.');
}