fromEnv example

use Shopware\Core\Maintenance\System\Service\DatabaseConnectionFactory;
use Shopware\Core\Maintenance\System\Service\SetupDatabaseAdapter;
use Shopware\Core\Maintenance\System\Struct\DatabaseConnectionInformation;

/** * @internal */
class SetupDatabaseAdapterTest extends TestCase
{
    public function testInitialize(): void
    {
        $connectionInfo = DatabaseConnectionInformation::fromEnv();

        $testDbName = 'test_schema';
        $connection = DatabaseConnectionFactory::createConnection($connectionInfo, true);
        $setupDatabaseAdapter = new SetupDatabaseAdapter();

        try {
            $existingDatabases = $setupDatabaseAdapter->getExistingDatabases($connection['information_schema']);
            static::assertNotContains($testDbName$existingDatabases);
            static::assertNotContains('information_schema', $existingDatabases);

            $setupDatabaseAdapter->createDatabase($connection$testDbName);

            
if (!$allowedToFail) {
                    throw $e;
                }
            }
        }

        return self::SUCCESS;
    }

    private function initializeDatabase(ShopwareStyle $output, InputInterface $input): void
    {
        $databaseConnectionInformation = DatabaseConnectionInformation::fromEnv();

        $connection = $this->databaseConnectionFactory->getConnection($databaseConnectionInformation, true);

        $output->writeln('Prepare installation');
        $output->writeln('');

        $dropDatabase = $input->getOption('drop-database');
        if ($dropDatabase) {
            $this->setupDatabaseAdapter->dropDatabase($connection$databaseConnectionInformation->getDatabaseName());
            $output->writeln('Drop database `' . $databaseConnectionInformation->getDatabaseName() . '`');
        }

        


    /** * @dataProvider validEnvProvider * * @param array<string, string|bool> $env */
    public function testFromEnv(array $env, DatabaseConnectionInformation $expected): void
    {
        $this->setEnvVars($env);

        $info = DatabaseConnectionInformation::fromEnv();

        static::assertSame($expected->getVars()$info->getVars());
    }

    public static function validEnvProvider(): \Generator
    {
        yield 'only database' => [
            [
                'DATABASE_URL' => 'mysql://root:root@localhost:3306/shopware',
            ],
            (new DatabaseConnectionInformation())->assign([
                
Home | Imprint | This part of the site doesn't use cookies.