You are here

public function NextIdTest::testDbNextIdClosedConnection in Drupal 10

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/NextIdTest.php \Drupal\KernelTests\Core\Database\NextIdTest::testDbNextIdClosedConnection()

Tests that sequences table clear up works when a connection is closed.

See also

\Drupal\mysql\Driver\Database\mysql\Connection::__destruct()

File

core/tests/Drupal/KernelTests/Core/Database/NextIdTest.php, line 48

Class

NextIdTest
Tests the sequences API.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testDbNextIdClosedConnection() {

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

  // Create an additional connection to test closing the connection.
  $connection_info = Database::getConnectionInfo();
  Database::addConnectionInfo('default', 'next_id', $connection_info['default']);

  // Get a few IDs to ensure there the clean up needs to run and there is more
  // than one row.
  Database::getConnection('next_id')
    ->nextId();
  Database::getConnection('next_id')
    ->nextId();

  // At this point the sequences table should contain unnecessary rows.
  $count = $this->connection
    ->select('sequences')
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertGreaterThan(1, $count);

  // Close the connection.
  Database::closeConnection('next_id');

  // Test that \Drupal\mysql\Driver\Database\mysql\Connection::__destruct()
  // successfully trims the sequences table if the connection is closed.
  $count = $this->connection
    ->select('sequences')
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEquals(1, $count);
}