getDatabaseConnection example

// Check that the deprecated theme link was rendered correctly.     $this->assertSession()->elementExists('xpath', "//a[contains(@href, 'http://example.com/deprecated_theme')]");

    // Uninstall a deprecated theme and confirm the warning is not displayed.     $theme_installer->uninstall(['test_deprecated_theme']);
    $this->drupalGet('admin/reports/status');
    $session->pageTextNotContains('Deprecated themes enabled');
    $session->pageTextNotContains('Deprecated themes found: Test deprecated theme.');
    $this->assertSession()->elementNotExists('xpath', "//a[contains(@href, 'http://example.com/deprecated_theme')]");

    // Check if pg_trgm extension is enabled on postgres.     if ($this->getDatabaseConnection()->databaseType() == 'pgsql') {
      $this->assertSession()->pageTextContains('PostgreSQL pg_trgm extension');
      $elements = $this->xpath('//details[@class="system-status-report__entry"]//div[contains(text(), :text)]', [
        ':text' => 'The pg_trgm PostgreSQL extension is present.',
      ]);
      $this->assertCount(1, $elements);
      $this->assertStringStartsWith('Available', $elements[0]->getParent()->getText());
    }
  }

  /** * Tests that the Error counter matches the displayed number of errors. */
$this->setName('dump-database-d8-mysql')
      ->setDescription('Dump the current database to a generation script')
      ->addOption('schema-only', NULL, InputOption::VALUE_OPTIONAL, 'A comma separated list of tables to only export the schema without data.', 'cache.*,sessions,watchdog')
      ->addOption('insert-count', NULL, InputOption::VALUE_OPTIONAL, ' The number of rows to insert in a single SQL statement.', 1000);
    parent::configure();
  }

  /** * {@inheritdoc} */
  protected function execute(InputInterface $input, OutputInterface $output): int {
    $connection = $this->getDatabaseConnection($input);

    // If not explicitly set, disable ANSI which will break generated php.     if ($input->hasParameterOption(['--ansi']) !== TRUE) {
      $output->setDecorated(FALSE);
    }

    $schema_tables = $input->getOption('schema-only');
    $schema_tables = explode(',', $schema_tables);
    $insert_count = (int) $input->getOption('insert-count');

    $output->writeln($this->generateScript($connection$schema_tables$insert_count), OutputInterface::OUTPUT_RAW);
    
unset($tags[$key]);
      }
      else {
        $this->invalidatedTags[$tag] = TRUE;
        unset($this->tagCache[$tag]);
      }
    }
    if (!$tags) {
      return;
    }

    $in_transaction = $this->getDatabaseConnection()->inTransaction();
    if ($in_transaction) {
      if (empty($this->delayedTags)) {
        $this->getDatabaseConnection()->addRootTransactionEndCallback([$this, 'rootTransactionEndCallback']);
      }
      $this->delayedTags = Cache::mergeTags($this->delayedTags, $tags);
    }
    else {
      $this->doInvalidateTags($tags);
    }
  }

  
/** * {@inheritdoc} */
  protected function execute(InputInterface $input, OutputInterface $output): int {
    $script = $input->getArgument('script');
    if (!is_file($script)) {
      $output->writeln('File must exist.');
      return 1;
    }

    $connection = $this->getDatabaseConnection($input);
    $this->runScript($connection$script);
    $output->writeln('Import completed successfully.');
    return 0;
  }

  /** * Run the database script. * * @param \Drupal\Core\Database\Connection $connection * Connection used by the script when included. * @param string $script * Path to dump script. */

  public function testSpecifyDatabaseKey() {
    $command = new DbCommandBaseTester();
    $command_tester = new CommandTester($command);

    Database::addConnectionInfo('magic_db', 'default', Database::getConnectionInfo('default')['default']);

    $command_tester->execute([
      '--database' => 'magic_db',
    ]);
    $this->assertEquals('magic_db', $command->getDatabaseConnection($command_tester->getInput())->getKey(),
       'Special db key is returned');
  }

  /** * Invalid database names will throw a useful exception. */
  public function testSpecifyDatabaseDoesNotExist() {
    $command = new DbCommandBaseTester();
    $command_tester = new CommandTester($command);
    $command_tester->execute([
      '--database' => 'dne',
    ]);

class TestSetupTraitTest extends KernelTestBase {

  use TestSetupTrait;

  /** * @covers ::getDatabaseConnection * @group legacy */
  public function testGetDatabaseConnection(): void {
    $this->expectDeprecation('Drupal\Core\Test\TestSetupTrait::getDatabaseConnection is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3176816');
    $this->assertNotNull($this->getDatabaseConnection());
  }

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