DatabaseConnectionInformation example

if (empty($databaseParameters['user'])
        || empty($databaseParameters['host'])
        || empty($databaseParameters['port'])
        || empty($databaseParameters['database'])
    ) {
        $app->render('database-configuration.php', ['error' => 'Please fill in all fields']);

        return;
    }

    $connectionInfo = new DatabaseConnectionInformation();
    $connectionInfo->username = $databaseParameters['user'];
    $connectionInfo->hostname = $databaseParameters['host'];
    $connectionInfo->port = $databaseParameters['port'];
    $connectionInfo->databaseName = $databaseParameters['database'];
    $connectionInfo->password = $databaseParameters['password'];

    try {
        $databaseFactory = new DatabaseFactory();
        $databaseFactory->createPDOConnection($connectionInfo); // check connection     } catch (\Exception $e) {
        $app->render('database-configuration.php', ['error' => $e->getMessage()]);

        
/** * @internal * * @covers \Shopware\Core\Maintenance\System\Struct\DatabaseConnectionInformation */
class DatabaseConnectionInformationTest extends TestCase
{
    use EnvTestBehaviour;

    public function testValidInformation(): void
    {
        $info = new DatabaseConnectionInformation();
        $info->assign([
            'hostname' => 'localhost',
            'port' => 3306,
            'username' => 'root',
            'password' => 'root',
            'databaseName' => 'shopware',
        ]);

        static::assertSame('localhost', $info->getHostname());
        static::assertSame(3306, $info->getPort());
        static::assertSame('root', $info->getUsername());
        
private readonly SetupDatabaseAdapter $setupDatabaseAdapter,
        private readonly DatabaseConnectionFactory $connectionFactory,
        string $projectDir
    ) {
        $this->jwtDir = $projectDir . '/config/jwt';
    }

    #[Route(path: '/installer/database-configuration', name: 'installer.database-configuration', methods: ['POST', 'GET'])]     public function databaseConfiguration(Request $request): Response
    {
        $session = $request->getSession();
        $connectionInfo = $session->get(DatabaseConnectionInformation::class) ?? new DatabaseConnectionInformation();

        if ($request->isMethod('GET')) {
            return $this->renderInstaller('@Installer/installer/database-configuration.html.twig', [
                'connectionInfo' => $connectionInfo,
                'error' => null,
            ]);
        }

        $connectionInfo = (new DatabaseConnectionInformation())->assign($request->request->all());

        try {
            
$this->twig->expects(static::once())->method('render')
            ->with(
                '@Installer/installer/database-import.html.twig',
                array_merge($this->getDefaultViewParams()[
                    'supportedLanguages' => [],
                    'error' => null,
                ])
            )
            ->willReturn('import');

        $session = new Session(new MockArraySessionStorage());
        $session->set(DatabaseConnectionInformation::classnew DatabaseConnectionInformation());
        $request = Request::create('/installer/database-import');
        $request->setSession($session);

        $response = $this->controller->databaseImport($request);
        static::assertSame('import', $response->getContent());
    }

    public function testDatabaseMigrateReturnsErrorIfSessionExpired(): void
    {
        $this->databaseMigrator->expects(static::never())->method('migrate');

        
unlink(__DIR__ . '/_fixtures/public/.htaccess');
    }

    public function testWriteConfig(): void
    {
        $idGenerator = $this->createMock(UniqueIdGenerator::class);
        $idGenerator->expects(static::once())->method('getUniqueId')
            ->willReturn('1234567890');

        $writer = new EnvConfigWriter(__DIR__ . '/_fixtures', $idGenerator);

        $info = new DatabaseConnectionInformation();
        $info->assign([
            'hostname' => 'localhost',
            'port' => 3306,
            'username' => 'root',
            'password' => 'root',
            'databaseName' => 'shopware',
        ]);

        $writer->writeConfig($info[
            'name' => 'test',
            'locale' => 'de-DE',
            


        /** @var Container $container */
        $container = $this->container = $this->getApplication()->getContainer();

        $container->offsetGet('shopware.notify')->doTrackEvent('Installer started');

        if ($this->IOHelper->isInteractive()) {
            $this->printStartMessage();
        }

        $connectionInfo = new DatabaseConnectionInformation();
        $connectionInfo = $this->getConnectionInfoFromConfig(SW_PATH . '/config.php', $connectionInfo);
        $connectionInfo = $this->getConnectionInfoFromArgs($input$connectionInfo);
        $connectionInfo = $this->getConnectionInfoFromInteractiveShell(
            $this->IOHelper,
            $connectionInfo
        );

        /** @var ConfigWriter $configWriter */
        $configWriter = $this->container->offsetGet('config.writer');
        $configWriter->writeConfig($connectionInfo);

        
__DIR__
        );
        $this->controller->setContainer($this->getInstallerContainer($this->twig, ['router' => $this->router]));
    }

    public function testDatabaseGetConfigurationRoute(): void
    {
        $this->twig->expects(static::once())->method('render')
            ->with(
                '@Installer/installer/database-configuration.html.twig',
                array_merge($this->getDefaultViewParams()[
                    'connectionInfo' => new DatabaseConnectionInformation(),
                    'error' => null,
                ])
            )
            ->willReturn('config');

        $this->connectionFactory->expects(static::never())->method('getConnection');

        $request = Request::create('/installer/database-configuration');
        $session = new Session(new MockArraySessionStorage());
        $request->setSession($session);

        

    public function askDatabaseConnectionInformation(
        DatabaseConnectionInformation $connectionInformation
    ) {
        $databaseHost = $this->askForDatabaseHostname($connectionInformation->hostname);
        $databasePort = $this->askForDatabasePort($connectionInformation->port);
        $question = new Question('Please enter database socket: ', $connectionInformation->socket);
        $databaseSocket = $this->askQuestion($question);
        $databaseUser = $this->askForDatabaseUsername($connectionInformation->username);
        $databasePassword = $this->askForDatabasePassword($connectionInformation->password);

        $connectionInformation = new DatabaseConnectionInformation([
            'hostname' => $databaseHost,
            'port' => $databasePort,
            'socket' => $databaseSocket,
            'username' => $databaseUser,
            'password' => $databasePassword,
        ]);

        return $connectionInformation;
    }

    /** * @return string */
$this->translator,
            ['de' => 'de-DE', 'en' => 'en-GB'],
            ['EUR', 'USD']
        );
        $this->controller->setContainer($this->getInstallerContainer($this->twig, ['router' => $this->router]));
    }

    public function testGetConfigurationRoute(): void
    {
        $request = new Request();
        $session = new Session(new MockArraySessionStorage());
        $session->set(DatabaseConnectionInformation::classnew DatabaseConnectionInformation());
        $session->set(BlueGreenDeploymentService::ENV_NAME, true);
        $request->setMethod('GET');
        $request->setSession($session);
        $request->attributes->set('_locale', 'de');

        $this->connection->expects(static::once())
            ->method('fetchAllAssociative')
            ->willReturn([
                ['iso3' => 'DEU', 'iso' => 'DE'],
                ['iso3' => 'GBR', 'iso' => 'GB'],
            ]);

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