function SelectCloneTest::testSelectConditionSubQueryCloning

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

Tests that subqueries as value within conditions are cloned properly.

File

core/tests/Drupal/KernelTests/Core/Database/SelectCloneTest.php, line 15

Class

SelectCloneTest
Tests cloning Select queries.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testSelectConditionSubQueryCloning() {
    $subquery = $this->connection
        ->select('test', 't');
    $subquery->addField('t', 'id', 'id');
    $subquery->condition('age', 28, '<');
    $query = $this->connection
        ->select('test', 't');
    $query->addField('t', 'name', 'name');
    $query->condition('id', $subquery, 'IN');
    $clone = clone $query;
    // Cloned query should have a different unique identifier.
    $this->assertNotEquals($query->uniqueIdentifier(), $clone->uniqueIdentifier());
    // Cloned query should not be altered by the following modification
    // happening on original query.
    $subquery->condition('age', 25, '>');
    $clone_result = $clone->countQuery()
        ->execute()
        ->fetchField();
    $query_result = $query->countQuery()
        ->execute()
        ->fetchField();
    // Make sure the cloned query has not been modified
    $this->assertEquals(3, $clone_result, 'The cloned query returns the expected number of rows');
    $this->assertEquals(2, $query_result, 'The query returns the expected number of rows');
}

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