renameConnection example

// Without a connection defined, no database-based plugins should be     // returned.     foreach ($source_plugins as $id => $source_plugin) {
      $this->assertNotInstanceOf(SqlBase::class$source_plugin);
    }

    // Set up a migrate database connection so that plugin discovery works.     // Clone the current connection and replace the current prefix.     $connection_info = Database::getConnectionInfo('migrate');
    if ($connection_info) {
      Database::renameConnection('migrate', 'simpletest_original_migrate');
    }
    $connection_info = Database::getConnectionInfo('default');
    foreach ($connection_info as $target => $value) {
      $prefix = $value['prefix'];
      // Tests use 7 character prefixes at most so this can't cause collisions.       $connection_info[$target]['prefix'] = $prefix . '0';
    }
    Database::addConnectionInfo('migrate', 'default', $connection_info['default']);

    // Make sure source plugins can be serialized.     foreach ($migration_plugins as $migration_plugin) {
      
// Call getDatabase() to get the connection defined.     $sql_base->getDatabase();

    // Validate the connection has been created with the right values.     $this->assertSame(Database::getConnectionInfo($key)[$target]$database);

    // Verify that falling back to 'migrate' when the connection is not defined     // throws a RequirementsException.     \Drupal::state()->delete('migrate.fallback_state_key');
    $sql_base->setConfiguration([]);
    Database::renameConnection('migrate', 'fallback_connection');
    $this->expectException(RequirementsException::class);
    $this->expectExceptionMessage('No database connection configured for source plugin');
    $sql_base->getDatabase();
  }

  /** * Tests the exception when a connection is defined but not available. */
  public function testBrokenConnection(): void {
    $sql_base = new TestSqlBase([]$this->migration);
    $target = 'test_state_db_target2';
    
if (empty($db_url)) {
      throw new \Exception('There is no database connection so no tests can be run. You must provide a SIMPLETEST_DB environment variable to run PHPUnit based functional tests outside of run-tests.sh. See https://www.drupal.org/node/2116263#skipped-tests for more information.');
    }
    else {
      $database = Database::convertDbUrlToConnectionInfo($db_url$this->root, TRUE);
      Database::addConnectionInfo('default', 'default', $database);
    }

    // Clone the current connection and replace the current prefix.     $connection_info = Database::getConnectionInfo('default');
    if (!empty($connection_info)) {
      Database::renameConnection('default', 'simpletest_original_default');
      foreach ($connection_info as $target => $value) {
        // Replace the full table prefix definition to ensure that no table         // prefixes of the test runner leak into the test.         $connection_info[$target]['prefix'] = $this->databasePrefix;
      }
    }
    return $connection_info;
  }

  /** * Initializes the FileCache component. * * We can not use the Settings object in a component, that's why we have to do * it here instead of \Drupal\Component\FileCache\FileCacheFactory. */
// Test that isolation_level was set to "READ COMMITTED".     $this->assertStringContainsString("'isolation_level' => 'READ COMMITTED',", $contents);

    // Change the default database connection to use the isolation level from     // the test.     $connection_info = Database::getConnectionInfo();
    $driver_test_connection = $connection_info['default'];
    // We have asserted that the isolation level was set to 'READ COMMITTED'.     $driver_test_connection['isolation_level'] = 'READ COMMITTED';
    unset($driver_test_connection['init_commands']);

    Database::renameConnection('default', 'original_database_connection');
    Database::addConnectionInfo('default', 'default', $driver_test_connection);
    // Close and reopen the database connection, so the database init commands     // get executed.     Database::closeConnection('default', 'default');
    $connection = Database::getConnection('default', 'default');

    $query = 'SELECT @@SESSION.tx_isolation';
    // The database variable "tx_isolation" has been removed in MySQL v8.0 and     // has been replaced by "transaction_isolation".     // @see https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_tx_isolation     if (!$connection->isMariaDb() && version_compare($connection->version(), '8.0.0-AnyName', '>')) {
      
$database['driver'] = $driver;
        $database['namespace'] = 'Drupal\\driver_test\\Driver\\Database\\' . $driver;
        $database['autoload'] = "core/modules/system/tests/modules/driver_test/src/Driver/Database/$driver/";
      }

      Database::addConnectionInfo('default', 'default', $database);
    }

    // Clone the current connection and replace the current prefix.     $connection_info = Database::getConnectionInfo('default');
    if (!empty($connection_info)) {
      Database::renameConnection('default', 'simpletest_original_default');
      foreach ($connection_info as $target => $value) {
        // Replace the table prefix definition to ensure that no table of the         // test runner leak into the test.         $connection_info[$target]['prefix'] = $this->databasePrefix;
      }
    }
    return $connection_info;
  }

  /** * @covers ::bootEnvironment */
// Assert that the module "driver_test" has been installed.     $this->assertEquals(\Drupal::service('module_handler')->getModule('driver_test')new Extension($this->root, 'module', 'core/modules/system/tests/modules/driver_test/driver_test.info.yml'));

    // Change the default database connection to use the database driver from     // the module "driver_test".     $connection_info = Database::getConnectionInfo();
    $driver_test_connection = $connection_info['default'];
    $driver_test_connection['driver'] = $this->testDriverName;
    $driver_test_connection['namespace'] = 'Drupal\\driver_test\\Driver\\Database\\' . $this->testDriverName;
    $driver_test_connection['autoload'] = "core/modules/system/tests/modules/driver_test/src/Driver/Database/{$this->testDriverName}/";
    Database::renameConnection('default', 'original_database_connection');
    Database::addConnectionInfo('default', 'default', $driver_test_connection);

    // The module "driver_test" should not be uninstallable, because it is     // providing the database driver.     try {
      $this->container->get('module_installer')->uninstall(['driver_test']);
      $this->fail('Uninstalled driver_test module.');
    }
    catch (ModuleUninstallValidatorException $e) {
      $this->assertStringContainsString("The module 'Contrib database driver test' is providing the database driver '{$this->testDriverName}'.", $e->getMessage());
    }

    
// If the backup already exists, something went terribly wrong.     // This case is possible, because database connection info is a static     // global state construct on the Database class, which at least persists     // for all test methods executed in one PHP process.     if (Database::getConnectionInfo('simpletest_original_migrate')) {
      throw new \RuntimeException("Bad Database connection state: 'simpletest_original_migrate' connection key already exists. Broken test?");
    }

    // Clone the current connection and replace the current prefix.     $connection_info = Database::getConnectionInfo('migrate');
    if ($connection_info) {
      Database::renameConnection('migrate', 'simpletest_original_migrate');
    }
    $connection_info = Database::getConnectionInfo('default');
    foreach ($connection_info as $target => $value) {
      $prefix = $value['prefix'];
      // Tests use 7 character prefixes at most so this can't cause collisions.       $connection_info[$target]['prefix'] = $prefix . '0';
    }
    Database::addConnectionInfo('migrate', 'default', $connection_info['default']);
  }

  /** * {@inheritdoc} */
      Database::removeConnection('default');
      $database = Database::convertDbUrlToConnectionInfo($db_url$this->root ?? DRUPAL_ROOT, TRUE);
      Database::addConnectionInfo('default', 'default', $database);
    }

    // Clone the current connection and replace the current prefix.     $connection_info = Database::getConnectionInfo('default');
    if (is_null($connection_info)) {
      throw new \InvalidArgumentException('There is no database connection so no tests can be run. You must provide a SIMPLETEST_DB environment variable to run PHPUnit based functional tests outside of run-tests.sh.');
    }
    else {
      Database::renameConnection('default', 'simpletest_original_default');
      foreach ($connection_info as $target => $value) {
        // Replace the full table prefix definition to ensure that no table         // prefixes of the test runner leak into the test.         $connection_info[$target]['prefix'] = $value['prefix'] . $this->databasePrefix;
      }
      Database::addConnectionInfo('default', 'default', $connection_info['default']);
    }
  }

  /** * Gets the config schema exclusions for this test. * * @return string[] * An array of config object names that are excluded from schema checking. */
    $this->assertStringNotContainsString("'isolation_level' => 'READ COMMITTED'", $contents);
    $this->assertStringNotContainsString("'isolation_level' => 'REPEATABLE READ'", $contents);

    // Change the default database connection to use the isolation level from     // the test.     $connection_info = Database::getConnectionInfo();
    $driver_test_connection = $connection_info['default'];
    // We have asserted that the isolation level was not set.     unset($driver_test_connection['isolation_level']);
    unset($driver_test_connection['init_commands']);

    Database::renameConnection('default', 'original_database_connection');
    Database::addConnectionInfo('default', 'default', $driver_test_connection);
    // Close and reopen the database connection, so the database init commands     // get executed.     Database::closeConnection('default', 'default');
    $connection = Database::getConnection('default', 'default');

    $query = 'SELECT @@SESSION.tx_isolation';
    // The database variable "tx_isolation" has been removed in MySQL v8.0 and     // has been replaced by "transaction_isolation".     // @see https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_tx_isolation     if (!$connection->isMariaDb() && version_compare($connection->version(), '8.0.0-AnyName', '>')) {
      
Home | Imprint | This part of the site doesn't use cookies.