convertDbUrlToConnectionInfo example


  protected function getDatabaseConnection(InputInterface $input) {
    // Load connection from a URL.     if ($input->getOption('database-url')) {
      // @todo this could probably be refactored to not use a global connection.       // Ensure database connection isn't set.       if (Database::getConnectionInfo('db-tools')) {
        throw new \RuntimeException('Database "db-tools" is already defined. Cannot define database provided.');
      }
      $info = Database::convertDbUrlToConnectionInfo($input->getOption('database-url'), \Drupal::root());
      Database::addConnectionInfo('db-tools', 'default', $info);
      $key = 'db-tools';
    }
    else {
      $key = $input->getOption('database');
    }

    // If they supplied a prefix, replace it in the connection information.     $prefix = $input->getOption('prefix');
    if ($prefix) {
      $info = Database::getConnectionInfo($key)['default'];
      
/** * Adds the installed test site to the database connection info. * * @param string $db_prefix * The prefix of the installed test site. * * @return string * The database key of the added connection. */
  protected function addTestDatabase($db_prefix) {
    $database = Database::convertDbUrlToConnectionInfo(getenv('SIMPLETEST_DB')$this->root);
    $database['prefix'] = $db_prefix;
    $target = __CLASS__ . $db_prefix;
    Database::addConnectionInfo($target, 'default', $database);
    return $target;
  }

  /** * Gets the lock file path. * * @param string $db_prefix * The prefix of the installed test site. * * @return string * The lock file path. */
->with('site.path')
      ->willReturn('');
    \Drupal::setContainer($container);
  }

  /** * @covers ::convertDbUrlToConnectionInfo * * @dataProvider providerConvertDbUrlToConnectionInfo */
  public function testDbUrlToConnectionConversion($url$database_array$include_test_drivers) {
    $result = Database::convertDbUrlToConnectionInfo($url$this->root, $include_test_drivers);
    $this->assertEquals($database_array$result);
  }

  /** * Data provider for testDbUrlToConnectionConversion(). * * @return array * Array of arrays with the following elements: * - url: The full URL string to be tested. * - database_array: An array containing the expected results. */
  
/** * {@inheritdoc} */
  protected function getDatabaseConnectionInfo() {
    // If the test is run with argument SIMPLETEST_DB then use it.     $db_url = getenv('SIMPLETEST_DB');
    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);

      if (in_array($database['driver']['mysql', 'pgsql'])) {
        // Change the used database driver to the one provided by the module         // "driver_test".         $driver = 'Drivertest' . ucfirst($database['driver']);
        $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);
    }

  protected function getDatabaseConnectionInfo() {
    // If the test is run with argument dburl then use it.     $db_url = getenv('SIMPLETEST_DB');
    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;
      }

  protected function tearDown(TestDatabase $test_database$db_url): void {
    // Connect to the test database.     $root = dirname(__DIR__, 5);
    $database = Database::convertDbUrlToConnectionInfo($db_url$root);
    $database['prefix'] = $test_database->getDatabasePrefix();
    Database::addConnectionInfo(__CLASS__, 'default', $database);

    // Remove all the tables.     $schema = Database::getConnection('default', __CLASS__)->schema();
    $tables = $schema->findTables('%');
    array_walk($tables[$schema, 'dropTable']);

    // Delete test site directory.     $this->fileUnmanagedDeleteRecursive($root . DIRECTORY_SEPARATOR . $test_database->getTestSitePath()[BrowserTestBase::class, 'filePreDeleteCallback']);
  }

  
protected function changeDatabasePrefix() {
    if (empty($this->databasePrefix)) {
      $this->prepareDatabasePrefix();
    }

    // If the test is run with argument dburl then use it.     $db_url = getenv('SIMPLETEST_DB');
    if (!empty($db_url)) {
      // Ensure no existing database gets in the way. If a default database       // exists already it must be removed.       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) {
        

class TestSetupTraitTest extends UnitTestCase {

  /** * Tests the SIMPLETEST_DB environment variable is used. * * @covers ::changeDatabasePrefix */
  public function testChangeDatabasePrefix() {
    $root = dirname(__FILE__, 7);
    putenv('SIMPLETEST_DB=pgsql://user:pass@127.0.0.1/db');
    $connection_info = Database::convertDbUrlToConnectionInfo('mysql://user:pass@localhost/db', $root);
    Database::addConnectionInfo('default', 'default', $connection_info);
    $this->assertEquals('mysql', Database::getConnectionInfo()['default']['driver']);
    $this->assertEquals('localhost', Database::getConnectionInfo()['default']['host']);

    // Create a mock for testing the trait and set a few properties that are     // used to avoid unnecessary set up.     $test_setup = $this->getMockForTrait(TestSetupTrait::class);

    $reflection = new \ReflectionClass($test_setup);
    $reflection->getProperty('databasePrefix')->setValue($test_setup, 'testDbPrefix');
    $reflection->getProperty('root')->setValue($test_setup$root);

    
Home | Imprint | This part of the site doesn't use cookies.