class ConnectionUnitTest

Tests management of database connections.

Hierarchy

Expanded class hierarchy of ConnectionUnitTest

File

drupal/core/modules/system/lib/Drupal/system/Tests/Database/ConnectionUnitTest.php, line 16
Contains Drupal\system\Tests\Database\ConnectionUnitTest.

Namespace

Drupal\system\Tests\Database
View source
class ConnectionUnitTest extends UnitTestBase {
  protected $key;
  protected $target;
  protected $monitor;
  protected $originalCount;
  public static function getInfo() {
    return array(
      'name' => 'Connection unit tests',
      'description' => 'Tests management of database connections.',
      'group' => 'Database',
    );
  }
  function setUp() {
    parent::setUp();
    $this->key = 'default';
    $this->originalTarget = 'default';
    $this->target = 'DatabaseConnectionUnitTest';

    // Determine whether the database driver is MySQL. If it is not, the test
    // methods will not be executed.
    // @todo Make this test driver-agnostic, or find a proper way to skip it.
    // @see http://drupal.org/node/1273478
    $connection_info = Database::getConnectionInfo('default');
    $this->skipTest = (bool) $connection_info['default']['driver'] != 'mysql';
    if ($this->skipTest) {

      // Insert an assertion to prevent Simpletest from interpreting the test
      // as failure.
      $this
        ->pass('This test is only compatible with MySQL.');
    }

    // Create an additional connection to monitor the connections being opened
    // and closed in this test.
    // @see TestBase::changeDatabasePrefix()
    Database::addConnectionInfo('default', 'monitor', $connection_info['default']);
    global $databases;
    $databases['default']['monitor'] = $connection_info['default'];
    $this->monitor = Database::getConnection('monitor');
  }

  /**
   * Adds a new database connection info to Database.
   */
  protected function addConnection() {

    // Add a new target to the connection, by cloning the current connection.
    $connection_info = Database::getConnectionInfo($this->key);
    Database::addConnectionInfo($this->key, $this->target, $connection_info[$this->originalTarget]);

    // Verify that the new target exists.
    $info = Database::getConnectionInfo($this->key);

    // Note: Custom assertion message to not expose database credentials.
    $this
      ->assertIdentical($info[$this->target], $connection_info[$this->key], 'New connection info found.');
  }

  /**
   * Returns the connection ID of the current test connection.
   *
   * @return integer
   */
  protected function getConnectionID() {
    return (int) Database::getConnection($this->target, $this->key)
      ->query('SELECT CONNECTION_ID()')
      ->fetchField();
  }

  /**
   * Asserts that a connection ID exists.
   *
   * @param integer $id
   *   The connection ID to verify.
   */
  protected function assertConnection($id) {
    $list = $this->monitor
      ->query('SHOW PROCESSLIST')
      ->fetchAllKeyed(0, 0);
    return $this
      ->assertTrue(isset($list[$id]), format_string('Connection ID @id found.', array(
      '@id' => $id,
    )));
  }

  /**
   * Asserts that a connection ID does not exist.
   *
   * @param integer $id
   *   The connection ID to verify.
   */
  protected function assertNoConnection($id) {
    $list = $this->monitor
      ->query('SHOW PROCESSLIST')
      ->fetchAllKeyed(0, 0);
    return $this
      ->assertFalse(isset($list[$id]), format_string('Connection ID @id not found.', array(
      '@id' => $id,
    )));
  }

  /**
   * Tests Database::closeConnection() without query.
   *
   * @todo getConnectionID() executes a query.
   */
  function testOpenClose() {
    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);

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

  /**
   * Tests Database::closeConnection() with a query.
   */
  function testOpenQueryClose() {
    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);

    // Execute a query.
    Database::getConnection($this->target, $this->key)
      ->query('SHOW TABLES');

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

  /**
   * Tests Database::closeConnection() with a query and custom prefetch method.
   */
  function testOpenQueryPrefetchClose() {
    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);

    // Execute a query.
    Database::getConnection($this->target, $this->key)
      ->query('SHOW TABLES')
      ->fetchCol();

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

  /**
   * Tests Database::closeConnection() with a select query.
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConnectionUnitTest::$key protected property
ConnectionUnitTest::$monitor protected property
ConnectionUnitTest::$originalCount protected property
ConnectionUnitTest::$target protected property
ConnectionUnitTest::addConnection protected function Adds a new database connection info to Database.
ConnectionUnitTest::assertConnection protected function Asserts that a connection ID exists.
ConnectionUnitTest::assertNoConnection protected function Asserts that a connection ID does not exist.
ConnectionUnitTest::getConnectionID protected function Returns the connection ID of the current test connection.
ConnectionUnitTest::getInfo public static function
ConnectionUnitTest::setUp function Sets up unit test environment. Overrides UnitTestBase::setUp
ConnectionUnitTest::testOpenClose function Tests Database::closeConnection() without query.
ConnectionUnitTest::testOpenQueryClose function Tests Database::closeConnection() with a query.
ConnectionUnitTest::testOpenQueryPrefetchClose function Tests Database::closeConnection() with a query and custom prefetch method.
ConnectionUnitTest::testOpenSelectQueryClose function Tests Database::closeConnection() with a select query.
TestBase::$assertions protected property Assertions thrown in that test case.
TestBase::$databasePrefix protected property The database prefix of this test run.
TestBase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
TestBase::$originalPrefix protected property The original database prefix when running inside Simpletest.
TestBase::$results public property Current results of this test case.
TestBase::$setup protected property Flag to indicate whether the test has been set up.
TestBase::$setupDatabasePrefix protected property
TestBase::$setupEnvironment protected property
TestBase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
TestBase::$testId protected property The test run ID.
TestBase::$timeLimit protected property Time limit for the test.
TestBase::$verbose protected property TRUE if verbose debugging is enabled.
TestBase::$verboseClassName protected property Safe class name for use in verbose output filenames.
TestBase::$verboseDirectory protected property Directory where verbose output files are put.
TestBase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
TestBase::$verboseId protected property Incrementing identifier for verbose output filenames.
TestBase::assert protected function Internal helper: stores the assert.
TestBase::assertEqual protected function Check to see if two values are equal.
TestBase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
TestBase::assertIdentical protected function Check to see if two values are identical.
TestBase::assertIdenticalObject protected function Checks to see if two objects are identical.
TestBase::assertNotEqual protected function Check to see if two values are not equal.
TestBase::assertNotIdentical protected function Check to see if two values are not identical.
TestBase::assertNotNull protected function Check to see if a value is not NULL.
TestBase::assertNull protected function Check to see if a value is NULL.
TestBase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
TestBase::changeDatabasePrefix protected function Changes the database connection to the prefixed one.
TestBase::checkRequirements protected function Checks the matching requirements for Test. 3
TestBase::deleteAssert public static function Delete an assertion record by message ID.
TestBase::error protected function Fire an error assertion. 1
TestBase::errorHandler public function Handle errors during test runs.
TestBase::exceptionHandler protected function Handle exceptions.
TestBase::fail protected function Fire an assertion that is always negative.
TestBase::filePreDeleteCallback public static function Ensures test files are deletable within file_unmanaged_delete_recursive().
TestBase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
TestBase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
TestBase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
TestBase::insertAssert public static function Store an assertion from outside the testing context.
TestBase::pass protected function Fire an assertion that is always positive.
TestBase::prepareDatabasePrefix protected function Generates a database prefix for running tests.
TestBase::prepareEnvironment protected function Prepares the current environment for running the test.
TestBase::randomName public static function Generates a random string containing letters and numbers.
TestBase::randomObject public static function Generates a random PHP object.
TestBase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
TestBase::rebuildContainer protected function Rebuild drupal_container().
TestBase::run public function Run all tests in this class.
TestBase::tearDown protected function Deletes created files, database tables, and reverts all environment changes. 10
TestBase::verbose protected function Logs verbose message in a text file.
UnitTestBase::$configDirectories protected property
UnitTestBase::__construct function Constructor for UnitTestBase. Overrides TestBase::__construct 6