function KernelTestBase::resetEnvironment

Same name and namespace in other branches
  1. main core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::resetEnvironment()

Resets the environment after a test method has run.

See also

\Drupal\TestTools\Attribute\ShareEnvironment

File

core/tests/Drupal/KernelTests/KernelTestBase.php, line 934

Class

KernelTestBase
Base class for functional integration tests.

Namespace

Drupal\KernelTests

Code

protected function resetEnvironment() : void {
  if (static::sharesEnvironment()) {
    return;
  }
  // Remove all prefixed tables.
  $original_connection_info = Database::getConnectionInfo('simpletest_original_default');
  $original_prefix = $original_connection_info['default']['prefix'] ?? NULL;
  $test_connection_info = Database::getConnectionInfo('default');
  $test_prefix = $test_connection_info['default']['prefix'] ?? NULL;
  if ($original_prefix != $test_prefix) {
    $tables = Database::getConnection()->schema()
      ->findTables('%');
    foreach ($tables as $table) {
      if (Database::getConnection()->schema()
        ->dropTable($table)) {
        unset($tables[$table]);
      }
    }
  }
}

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