function ConnectionUnitTest::testOpenQueryClose

Same name and namespace in other branches
  1. 7.x modules/simpletest/tests/database_test.test \ConnectionUnitTest::testOpenQueryClose()

Tests Database::closeConnection() with a query.

File

core/tests/Drupal/KernelTests/Core/Database/ConnectionUnitTest.php, line 127

Class

ConnectionUnitTest
Tests management of database connections.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testOpenQueryClose() {
    // Only run this test for the 'mysql' driver.
    $driver = $this->connection
        ->driver();
    if ($driver !== 'mysql') {
        $this->markTestSkipped("MySql tests can not run for driver '{$driver}'.");
    }
    // Add and open a new connection.
    $this->addConnection();
    $id = $this->getConnectionId();
    Database::getConnection(static::TEST_TARGET_CONNECTION);
    // Verify that there is a new connection.
    $this->assertConnection($id);
    // Execute a query.
    Database::getConnection(static::TEST_TARGET_CONNECTION)->query('SHOW TABLES');
    // Close the connection.
    Database::closeConnection(static::TEST_TARGET_CONNECTION);
    // Wait 20ms to give the database engine sufficient time to react.
    usleep(20000);
    // Verify that we are back to the original connection count.
    $this->assertNoConnection($id);
}

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