SelectCloneTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace Drupal\KernelTests\Core\Database;
  3. /**
  4. * Tests cloning Select queries.
  5. *
  6. * @group Database
  7. */
  8. class SelectCloneTest extends DatabaseTestBase {
  9. /**
  10. * Test that subqueries as value within conditions are cloned properly.
  11. */
  12. public function testSelectConditionSubQueryCloning() {
  13. $subquery = $this->connection->select('test', 't');
  14. $subquery->addField('t', 'id', 'id');
  15. $subquery->condition('age', 28, '<');
  16. $query = $this->connection->select('test', 't');
  17. $query->addField('t', 'name', 'name');
  18. $query->condition('id', $subquery, 'IN');
  19. $clone = clone $query;
  20. // Cloned query should have a different unique identifier.
  21. $this->assertNotEquals($query->uniqueIdentifier(), $clone->uniqueIdentifier());
  22. // Cloned query should not be altered by the following modification
  23. // happening on original query.
  24. $subquery->condition('age', 25, '>');
  25. $clone_result = $clone->countQuery()->execute()->fetchField();
  26. $query_result = $query->countQuery()->execute()->fetchField();
  27. // Make sure the cloned query has not been modified
  28. $this->assertEqual(3, $clone_result, 'The cloned query returns the expected number of rows');
  29. $this->assertEqual(2, $query_result, 'The query returns the expected number of rows');
  30. }
  31. /**
  32. * Tests that nested SELECT queries are cloned properly.
  33. */
  34. public function testNestedQueryCloning() {
  35. $sub_query = $this->connection->select('test', 't');
  36. $sub_query->addField('t', 'id', 'id');
  37. $sub_query->condition('age', 28, '<');
  38. $query = $this->connection->select($sub_query, 't');
  39. $clone = clone $query;
  40. // Cloned query should have a different unique identifier.
  41. $this->assertNotEquals($query->uniqueIdentifier(), $clone->uniqueIdentifier());
  42. // Cloned query should not be altered by the following modification
  43. // happening on original query.
  44. $sub_query->condition('age', 25, '>');
  45. $clone_result = $clone->countQuery()->execute()->fetchField();
  46. $query_result = $query->countQuery()->execute()->fetchField();
  47. // Make sure the cloned query has not been modified.
  48. $this->assertEquals(3, $clone_result, 'The cloned query returns the expected number of rows');
  49. $this->assertEquals(2, $query_result, 'The query returns the expected number of rows');
  50. }
  51. }