Connection example

public function __construct(array $config = [])
    {
        if (!\extension_loaded('ldap')) {
            throw new LdapException('The LDAP PHP extension is not enabled.');
        }

        $this->config = $config;
    }

    public function getConnection(): ConnectionInterface
    {
        return $this->connection ??= new Connection($this->config);
    }

    public function getEntryManager(): EntryManagerInterface
    {
        return $this->entryManager ??= new EntryManager($this->getConnection());
    }

    public function createQuery(string $dn, string $query, array $options = []): QueryInterface
    {
        return new Query($this->getConnection()$dn$query$options);
    }

    
public function testItCannotBeConstructedWithAWrongDsn()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('The given AMQP DSN is invalid.');
        Connection::fromDsn('amqp://:');
    }

    public function testItCanBeConstructedWithDefaults()
    {
        $this->assertEquals(
            new Connection([
                'host' => 'localhost',
                'port' => 5672,
                'vhost' => '/',
            ][
                'name' => self::DEFAULT_EXCHANGE_NAME,
            ][
                self::DEFAULT_EXCHANGE_NAME => [],
            ]),
            Connection::fromDsn('amqp://')
        );
    }

    
/** * @covers ::createConnectionOptionsFromUrl * @dataProvider providerCreateConnectionOptionsFromUrl * * @param string $url * SQLite URL. * @param string $expected * Expected connection option. */
  public function testCreateConnectionOptionsFromUrl(string $url, string $expected) {
    $root = dirname(__DIR__, 8);
    $sqlite_connection = new Connection($this->createMock(StubPDO::class)[]);
    $database = $sqlite_connection->createConnectionOptionsFromUrl($url$root);
    $this->assertEquals('sqlite', $database['driver']);
    $this->assertEquals($expected$database['database']);
  }

  /** * Data provider for testCreateConnectionOptionsFromUrl. * * @return string[][] * Associative array of arrays with the following elements: * - SQLite database URL * - Expected database connection option */
$driverConnection->method('getDatabasePlatform')->willReturn($platform);
        $registry = $this->createMock(ConnectionRegistry::class);

        $registry->expects($this->once())
            ->method('getConnection')
            ->willReturn($driverConnection);

        $factory = new DoctrineTransportFactory($registry);
        $serializer = $this->createMock(SerializerInterface::class);

        $this->assertEquals(
            new DoctrineTransport(new Connection(PostgreSqlConnection::buildConfiguration('doctrine://default')$driverConnection)$serializer),
            $factory->createTransport('doctrine://default', []$serializer)
        );
    }

    public function testCreateTransportNotifyWithPostgreSQLPlatform()
    {
        $driverConnection = $this->createMock(\Doctrine\DBAL\Connection::class);
        $schemaManager = $this->createMock(AbstractSchemaManager::class);
        $schemaConfig = $this->createMock(SchemaConfig::class);
        $platform = $this->createMock(PostgreSQLPlatform::class);
        $schemaManager->method('createSchemaConfig')->willReturn($schemaConfig);
        

    private Connection $connection;
    private ?DataDumperInterface $wrappedDumper;

    /** * @param string $host The server host * @param DataDumperInterface|null $wrappedDumper A wrapped instance used whenever we failed contacting the server * @param ContextProviderInterface[] $contextProviders Context providers indexed by context name */
    public function __construct(string $host, DataDumperInterface $wrappedDumper = null, array $contextProviders = [])
    {
        $this->connection = new Connection($host$contextProviders);
        $this->wrappedDumper = $wrappedDumper;
    }

    public function getContextProviders(): array
    {
        return $this->connection->getContextProviders();
    }

    /** * @return string|null */
    
public function __construct(
        DriverInterface $driver,
        private DebugDataHolder $debugDataHolder,
        private ?Stopwatch $stopwatch,
        private string $connectionName,
    ) {
        parent::__construct($driver);
    }

    public function connect(array $params): Connection
    {
        return new Connection(
            parent::connect($params),
            $this->debugDataHolder,
            $this->stopwatch,
            $this->connectionName,
        );
    }
}

  protected function getDatabase(array $source_data) {
    // Create an in-memory SQLite database. Plugins can interact with it like     // any other database, and it will cease to exist when the connection is     // closed.     $connection_options = ['database' => ':memory:'];
    $pdo = Connection::open($connection_options);
    $connection = new Connection($pdo$connection_options);

    // Create the tables and fill them with data.     foreach ($source_data as $table => $rows) {
      // Use the biggest row to build the table schema.       $counts = array_map('count', $rows);
      asort($counts);
      end($counts);
      $pilot = $rows[key($counts)];

      $connection->schema()
        ->createTable($table[
          
public function testFromInvalidDsn()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('The given Beanstalkd DSN is invalid.');

        Connection::fromDsn('beanstalkd://');
    }

    public function testFromDsn()
    {
        $this->assertEquals(
            $connection = new Connection([], Pheanstalk::create('127.0.0.1', 11300)),
            Connection::fromDsn('beanstalkd://127.0.0.1')
        );

        $configuration = $connection->getConfiguration();

        $this->assertSame('default', $configuration['tube_name']);
        $this->assertSame(0, $configuration['timeout']);
        $this->assertSame(90, $configuration['ttr']);

        $this->assertEquals(
            $connection = new Connection([], Pheanstalk::create('foobar', 15555)),
            

  protected function getDatabase(array $source_data) {
    // Create an in-memory SQLite database. Plugins can interact with it like     // any other database, and it will cease to exist when the connection is     // closed.     $connection_options = ['database' => ':memory:'];
    $pdo = Connection::open($connection_options);
    $connection = new Connection($pdo$connection_options);

    // Create the tables and fill them with data.     foreach ($source_data as $table => $rows) {
      // Use the biggest row to build the table schema.       $counts = array_map('count', $rows);
      asort($counts);
      end($counts);
      $pilot = $rows[key($counts)];

      $connection->schema()
        ->createTable($table[
          
public function testDeprecationInstallTasks() {
    $this->expectDeprecation('\Drupal\Core\Database\Driver\sqlite\Install\Tasks is deprecated in drupal:9.4.0 and is removed from drupal:11.0.0. The SQLite database driver has been moved to the sqlite module. See https://www.drupal.org/node/3129492');
    $tasks = new Tasks();
    $this->assertInstanceOf(Tasks::class$tasks);
  }

  /** * @covers Drupal\Core\Database\Driver\sqlite\Connection */
  public function testDeprecationConnection() {
    $this->expectDeprecation('\Drupal\Core\Database\Driver\sqlite\Connection is deprecated in drupal:9.4.0 and is removed from drupal:11.0.0. The SQLite database driver has been moved to the sqlite module. See https://www.drupal.org/node/3129492');
    $connection = new Connection($this->createMock(StubPDO::class)[]);
    $this->assertInstanceOf(Connection::class$connection);
  }

  /** * @covers Drupal\Core\Database\Driver\sqlite\Insert */
  public function testDeprecationInsert() {
    $this->expectDeprecation('\Drupal\Core\Database\Driver\sqlite\Insert is deprecated in drupal:9.4.0 and is removed from drupal:11.0.0. The SQLite database driver has been moved to the sqlite module. See https://www.drupal.org/node/3129492');
    $insert = new Insert($this->connection, 'test');
    $this->assertInstanceOf(Insert::class$insert);
  }

  


  /** * Tests the getQualifiedMapTable method with a prefixed database. */
  public function testGetQualifiedMapTablePrefix() {
    $connection_options = [
      'database' => ':memory:',
      'prefix' => 'prefix',
    ];
    $pdo = Connection::open($connection_options);
    $this->database = new Connection($pdo$connection_options);
    $qualified_map_table = $this->getIdMap()->getQualifiedMapTableName();
    // The SQLite driver is a special flower. It will prefix tables with     // PREFIX.TABLE, instead of the standard PREFIXTABLE.     // @see \Drupal\sqlite\Driver\Database\sqlite\Connection::__construct()     $this->assertEquals('prefix.migrate_map_sql_idmap_test', $qualified_map_table);
  }

  /** * Tests all the iterator methods in one swing. * * The iterator methods are: * - Sql::rewind() * - Sql::next() * - Sql::valid() * - Sql::key() * - Sql::current() */
public function testFromInvalidDsn()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('The given Redis DSN is invalid.');

        Connection::fromDsn('redis://');
    }

    public function testFromDsn()
    {
        $this->assertEquals(
            new Connection([
                'stream' => 'queue',
                'host' => 'localhost',
                'port' => 6379,
            ]$this->createMock(\Redis::class)),
            Connection::fromDsn('redis://localhost/queue?', []$this->createMock(\Redis::class))
        );
    }

    public function testFromDsnOnUnixSocket()
    {
        $this->assertEquals(
            
$this->expectException(\InvalidArgumentException::class);
        Connection::fromDsn('sqs://default/queue?extra_param=some_value');
    }

    public function testConfigureWithCredentials()
    {
        $awsKey = 'some_aws_access_key_value';
        $awsSecret = 'some_aws_secret_value';
        $region = 'eu-west-1';
        $httpClient = $this->createMock(HttpClientInterface::class);
        $this->assertEquals(
            new Connection(['queue_name' => 'queue']new SqsClient(['region' => $region, 'accessKeyId' => $awsKey, 'accessKeySecret' => $awsSecret], null, $httpClient)),
            Connection::fromDsn('sqs://default/queue', [
                'access_key' => $awsKey,
                'secret_key' => $awsSecret,
                'region' => $region,
            ]$httpClient)
        );
    }

    public function testConfigureWithTemporaryCredentials()
    {
        $awsKey = 'some_aws_access_key_value';
        
public function testDeprecationInstallTasks() {
    $this->expectDeprecation('\Drupal\Core\Database\Driver\pgsql\Install\Tasks is deprecated in drupal:9.4.0 and is removed from drupal:11.0.0. The PostgreSQL database driver has been moved to the pgsql module. See https://www.drupal.org/node/3129492');
    $tasks = new Tasks();
    $this->assertInstanceOf(Tasks::class$tasks);
  }

  /** * @covers Drupal\Core\Database\Driver\pgsql\Connection */
  public function testDeprecationConnection() {
    $this->expectDeprecation('\Drupal\Core\Database\Driver\pgsql\Connection is deprecated in drupal:9.4.0 and is removed from drupal:11.0.0. The PostgreSQL database driver has been moved to the pgsql module. See https://www.drupal.org/node/3129492');
    $connection = new Connection($this->createMock(StubPDO::class)[]);
    $this->assertInstanceOf(Connection::class$connection);
  }

  /** * @covers Drupal\Core\Database\Driver\pgsql\Delete */
  public function testDeprecationDelete() {
    $this->expectDeprecation('\Drupal\Core\Database\Driver\pgsql\Delete is deprecated in drupal:9.4.0 and is removed from drupal:11.0.0. The PostgreSQL database driver has been moved to the pgsql module. See https://www.drupal.org/node/3129492');
    $delete = new Delete($this->connection, 'test');
    $this->assertInstanceOf(Delete::class$delete);
  }

  
/** * Tests the deprecations of the MySQL database driver classes in Core. * * @group Database */
class MysqlDriverTest extends DriverSpecificKernelTestBase {

  /** * @covers \Drupal\mysql\Driver\Database\mysql\Connection */
  public function testConnection() {
    $connection = new Connection($this->createMock(StubPDO::class)[]);
    $this->assertInstanceOf(Connection::class$connection);
  }

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