3 namespace Drupal\KernelTests\Core\Database;
6 * Tests cloning Select queries.
10 class SelectCloneTest extends DatabaseTestBase {
13 * Test that subqueries as value within conditions are cloned properly.
15 public function testSelectConditionSubQueryCloning() {
16 $subquery = db_select('test', 't');
17 $subquery->addField('t', 'id', 'id');
18 $subquery->condition('age', 28, '<');
20 $query = db_select('test', 't');
21 $query->addField('t', 'name', 'name');
22 $query->condition('id', $subquery, 'IN');
24 $clone = clone $query;
26 // Cloned query should have a different unique identifier.
27 $this->assertNotEquals($query->uniqueIdentifier(), $clone->uniqueIdentifier());
29 // Cloned query should not be altered by the following modification
30 // happening on original query.
31 $subquery->condition('age', 25, '>');
33 $clone_result = $clone->countQuery()->execute()->fetchField();
34 $query_result = $query->countQuery()->execute()->fetchField();
36 // Make sure the cloned query has not been modified
37 $this->assertEqual(3, $clone_result, 'The cloned query returns the expected number of rows');
38 $this->assertEqual(2, $query_result, 'The query returns the expected number of rows');
42 * Tests that nested SELECT queries are cloned properly.
44 public function testNestedQueryCloning() {
45 $sub_query = $this->connection->select('test', 't');
46 $sub_query->addField('t', 'id', 'id');
47 $sub_query->condition('age', 28, '<');
49 $query = $this->connection->select($sub_query, 't');
51 $clone = clone $query;
53 // Cloned query should have a different unique identifier.
54 $this->assertNotEquals($query->uniqueIdentifier(), $clone->uniqueIdentifier());
56 // Cloned query should not be altered by the following modification
57 // happening on original query.
58 $sub_query->condition('age', 25, '>');
60 $clone_result = $clone->countQuery()->execute()->fetchField();
61 $query_result = $query->countQuery()->execute()->fetchField();
63 // Make sure the cloned query has not been modified.
64 $this->assertEquals(3, $clone_result, 'The cloned query returns the expected number of rows');
65 $this->assertEquals(2, $query_result, 'The query returns the expected number of rows');