You are here

function ConnectionUnitTest::testOpenSelectQueryClose in Drupal 7

Tests Database::closeConnection() with a select query.

File

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

Class

ConnectionUnitTest
Tests management of database connections.

Code

function testOpenSelectQueryClose() {
  if ($this->skipTest) {
    return;
  }

  // Add and open a new connection.
  $this
    ->addConnection();
  $id = $this
    ->getConnectionID();
  Database::getConnection($this->target, $this->key);

  // Verify that there is a new connection.
  $this
    ->assertConnection($id);

  // Create a table.
  $name = 'foo';
  Database::getConnection($this->target, $this->key)
    ->schema()
    ->createTable($name, array(
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
      ),
    ),
  ));

  // Execute a query.
  Database::getConnection($this->target, $this->key)
    ->select('foo', 'f')
    ->fields('f', array(
    'name',
  ))
    ->execute()
    ->fetchAll();

  // Drop the table.
  Database::getConnection($this->target, $this->key)
    ->schema()
    ->dropTable($name);

  // Close the connection.
  Database::closeConnection($this->target, $this->key);

  // 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);
}