function SelectTest::testUnionCount

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

Tests that we can get a count query for a UNION Select query.

File

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

Class

SelectTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testUnionCount() : void {
    $query_1 = $this->connection
        ->select('test', 't')
        ->fields('t', [
        'name',
        'age',
    ])
        ->condition('age', [
        27,
        28,
    ], 'IN');
    $query_2 = $this->connection
        ->select('test', 't')
        ->fields('t', [
        'name',
        'age',
    ])
        ->condition('age', 28);
    $query_1->union($query_2, 'ALL');
    $names = $query_1->execute()
        ->fetchCol();
    $count = (int) $query_1->countQuery()
        ->execute()
        ->fetchField();
    // Ensure the counts match.
    $this->assertSame(count($names), $count, "The count query's result matched the number of rows in the UNION query.");
}

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