View source
<?php
namespace Drupal\KernelTests\Core\Database;
class SelectCloneTest extends DatabaseTestBase {
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;
$this
->assertNotEquals($query
->uniqueIdentifier(), $clone
->uniqueIdentifier());
$subquery
->condition('age', 25, '>');
$clone_result = $clone
->countQuery()
->execute()
->fetchField();
$query_result = $query
->countQuery()
->execute()
->fetchField();
$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');
}
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;
$this
->assertNotEquals($query
->uniqueIdentifier(), $clone
->uniqueIdentifier());
$sub_query
->condition('age', 25, '>');
$clone_result = $clone
->countQuery()
->execute()
->fetchField();
$query_result = $query
->countQuery()
->execute()
->fetchField();
$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');
}
}