function SelectComplexTest::testNestedConditions

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

Confirms that we can properly nest conditional clauses.

File

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

Class

SelectComplexTest
Tests the Select query builder with more complex queries.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testNestedConditions() : void {
    // This query should translate to:
    // "SELECT job FROM {test} WHERE name = 'Paul' AND (age = 26 OR age = 27)"
    // That should find only one record. Yes it's a non-optimal way of writing
    // that query but that's not the point!
    $query = $this->connection
        ->select('test');
    $query->addField('test', 'job');
    $query->condition('name', 'Paul');
    $query->condition($this->connection
        ->condition('OR')
        ->condition('age', 26)
        ->condition('age', 27));
    $job = $query->execute()
        ->fetchField();
    $this->assertEquals('Songwriter', $job, 'Correct data retrieved.');
}

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