function DatabaseSelectCloneTest::testNestedQueryCloning

Tests that nested SELECT queries are cloned properly.

File

modules/simpletest/tests/database_test.test, line 348

Class

DatabaseSelectCloneTest
Test cloning Select queries.

Code

public function testNestedQueryCloning() {
    $sub_query = db_select('test', 't');
    $sub_query->addField('t', 'id', 'id');
    $sub_query->condition('age', 28, '<');
    $query = db_select($sub_query, 't');
    $clone = clone $query;
    // Cloned query should have a different unique identifier.
    $this->assertNotEqual($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->assertEqual(3, $clone_result, 'The cloned query returns the expected number of rows');
    $this->assertEqual(2, $query_result, 'The query returns the expected number of rows');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.