function SelectComplexTest::testCountQueryFieldRemovals

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

Tests that countQuery properly removes fields and expressions.

File

core/tests/Drupal/KernelTests/Core/Database/SelectComplexTest.php, line 261

Class

SelectComplexTest
Tests the Select query builder with more complex queries.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testCountQueryFieldRemovals() : void {
    // countQuery should remove all fields and expressions, so this can be
    // tested by adding a non-existent field and expression: if it ends
    // up in the query, an error will be thrown. If not, it will return the
    // number of records, which in this case happens to be 4 (there are four
    // records in the {test} table).
    $query = $this->connection
        ->select('test');
    $query->fields('test', [
        'fail',
    ]);
    $this->assertEquals(4, $query->countQuery()
        ->execute()
        ->fetchField(), 'Count Query removed fields');
    $query = $this->connection
        ->select('test');
    $query->addExpression('[fail]');
    $this->assertEquals(4, $query->countQuery()
        ->execute()
        ->fetchField(), 'Count Query removed expressions');
}

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