You are here

public function SelectCloneTest::testNestedQueryCloning in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/SelectCloneTest.php \Drupal\KernelTests\Core\Database\SelectCloneTest::testNestedQueryCloning()
  2. 9 core/tests/Drupal/KernelTests/Core/Database/SelectCloneTest.php \Drupal\KernelTests\Core\Database\SelectCloneTest::testNestedQueryCloning()

Tests that nested SELECT queries are cloned properly.

File

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

Class

SelectCloneTest
Tests cloning Select queries.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testNestedQueryCloning() {
  $sub_query = $this->connection
    ->select('test', 't');
  $sub_query
    ->addField('t', 'id', 'id');
  $sub_query
    ->condition('age', 28, '<');
  $query = $this->connection
    ->select($sub_query, 't');
  $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.
  $sub_query
    ->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');
}