3b14283b3451691b110b18bb25922b40e23f2faf
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Database / SelectCloneTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Database;
4
5 /**
6  * Tests cloning Select queries.
7  *
8  * @group Database
9  */
10 class SelectCloneTest extends DatabaseTestBase {
11
12   /**
13    * Test that subqueries as value within conditions are cloned properly.
14    */
15   public function testSelectConditionSubQueryCloning() {
16     $subquery = db_select('test', 't');
17     $subquery->addField('t', 'id', 'id');
18     $subquery->condition('age', 28, '<');
19
20     $query = db_select('test', 't');
21     $query->addField('t', 'name', 'name');
22     $query->condition('id', $subquery, 'IN');
23
24     $clone = clone $query;
25
26     // Cloned query should have a different unique identifier.
27     $this->assertNotEquals($query->uniqueIdentifier(), $clone->uniqueIdentifier());
28
29     // Cloned query should not be altered by the following modification
30     // happening on original query.
31     $subquery->condition('age', 25, '>');
32
33     $clone_result = $clone->countQuery()->execute()->fetchField();
34     $query_result = $query->countQuery()->execute()->fetchField();
35
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');
39   }
40
41   /**
42    * Tests that nested SELECT queries are cloned properly.
43    */
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, '<');
48
49     $query = $this->connection->select($sub_query, 't');
50
51     $clone = clone $query;
52
53     // Cloned query should have a different unique identifier.
54     $this->assertNotEquals($query->uniqueIdentifier(), $clone->uniqueIdentifier());
55
56     // Cloned query should not be altered by the following modification
57     // happening on original query.
58     $sub_query->condition('age', 25, '>');
59
60     $clone_result = $clone->countQuery()->execute()->fetchField();
61     $query_result = $query->countQuery()->execute()->fetchField();
62
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');
66   }
67
68 }