Skip to content

Commit

Permalink
OXDEV-7248 Switch to connection parameter provider
Browse files Browse the repository at this point in the history
  • Loading branch information
liulka-oxid committed Apr 19, 2024
1 parent 8537c33 commit 713b31d
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 42 deletions.
5 changes: 2 additions & 3 deletions source/Internal/Framework/Database/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
use Doctrine\DBAL\Configuration as DbalConfiguration;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\DriverManager;
use OxidEsales\EshopCommunity\Internal\Framework\Configuration\Dao\SystemConfigurationDaoInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Database\Logger\DatabaseLoggerFactoryInterface;

class ConnectionFactory implements ConnectionFactoryInterface
{
public function __construct(
private readonly SystemConfigurationDaoInterface $systemConfigurationDao,
private readonly ConnectionParameterProviderInterface $connectionParameterProvider,
private readonly DatabaseLoggerFactoryInterface $databaseLoggerFactory,
) {
}
Expand All @@ -30,7 +29,7 @@ public function create(): Connection
$this->databaseLoggerFactory->getDatabaseLogger()
);
return DriverManager::getConnection(
['url' => $this->systemConfigurationDao->get()->getDatabaseUrl()],
$this->connectionParameterProvider->getParameters(),
$dbalConfiguration,
);
}
Expand Down
25 changes: 25 additions & 0 deletions source/Internal/Framework/Database/ConnectionParameterProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\EshopCommunity\Internal\Framework\Database;

use OxidEsales\EshopCommunity\Internal\Framework\Configuration\Dao\SystemConfigurationDaoInterface;

class ConnectionParameterProvider implements ConnectionParameterProviderInterface
{
public function __construct(
private readonly SystemConfigurationDaoInterface $systemConfigurationDao,
) {
}

public function getParameters(): array
{
return ['url' => $this->systemConfigurationDao->get()->getDatabaseUrl()];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

namespace OxidEsales\EshopCommunity\Internal\Framework\Database;

interface ConnectionParameterProviderInterface
{
public function getParameters(): array;
}
3 changes: 3 additions & 0 deletions source/Internal/Framework/Database/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ services:
Doctrine\DBAL\Driver\Connection:
public: true
factory: [ '@OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionFactoryInterface', 'create' ]

OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionParameterProviderInterface:
class: OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionParameterProvider
31 changes: 28 additions & 3 deletions tests/ContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

namespace OxidEsales\EshopCommunity\Tests;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Filesystem\Path;

/**
* @internal
Expand All @@ -34,9 +37,31 @@ private function getParameter(string $name)
private function prepareContainer(): void
{
if ($this->container === null) {
$this->container = (new TestContainerFactory())->create();
$this->container->compile();
$this->get('oxid_esales.module.install.service.launched_shop_project_configuration_generator')->generate();
$this->createContainer();
$this->compileContainer();
}
}

private function createContainer(): void
{
$this->container = (new TestContainerFactory())->create();
}

private function compileContainer(): void
{
$this->container->compile(true);
$this->get('oxid_esales.module.install.service.launched_shop_project_configuration_generator')->generate();
}

private function loadYamlFixture(string $fixtureDir): void
{
$loader = new YamlFileLoader($this->container, new FileLocator(__DIR__));
$loader->load(Path::join($fixtureDir, 'services.yaml'));
}

private function replaceService(string $id, ?object $service): void
{
$this->container->set($id, $service);
$this->container->autowire($id, $id);
}
}
26 changes: 26 additions & 0 deletions tests/EnvTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\EshopCommunity\Tests;

use OxidEsales\EshopCommunity\Internal\Framework\Env\DotenvLoader;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;

trait EnvTrait
{
private function loadEnvFixture(string $fixtureDir, array $envFileLines): void
{
$filesystem = new Filesystem();
$fixtureFile = Path::join($fixtureDir, '.env');
$filesystem->dumpFile($fixtureFile, implode("\n", $envFileLines),);
(new DotenvLoader($fixtureDir))->loadEnvironmentVariables();
$filesystem->remove($fixtureFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@

use Doctrine\DBAL\Driver\Connection;
use OxidEsales\EshopCommunity\Core\Registry;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\Context;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
use OxidEsales\EshopCommunity\Tests\ContainerTrait;
use OxidEsales\EshopCommunity\Tests\TestContainerFactory;
use PHPUnit\Framework\TestCase;

final class ConnectionFactoryTest extends TestCase
Expand Down Expand Up @@ -54,17 +52,17 @@ public function testSqlLoggerWritesExpectedValueToTheLog(): void

private function injectContextMock(): void
{
$this->container = (new TestContainerFactory())->create();
$contextMock = $this->createConfiguredMock(
$this->createContainer();
$this->replaceService(
ContextInterface::class,
[
'isAdmin' => true,
'isEnabledAdminQueryLog' => true,
'getAdminLogFilePath' => $this->logFile
]
);
$this->container->set(ContextInterface::class, $contextMock);
$this->container->autowire(ContextInterface::class, Context::class);
$this->container->compile();
$this->createConfiguredMock(
ContextInterface::class,
[
'isAdmin' => true,
'isEnabledAdminQueryLog' => true,
'getAdminLogFilePath' => $this->logFile
]
));
$this->compileContainer();
}
}
30 changes: 7 additions & 23 deletions tests/Integration/Internal/Framework/Env/EnvLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@
namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Framework\Env;

use OxidEsales\EshopCommunity\Internal\Framework\Configuration\BootstrapConfigurationFactory;
use OxidEsales\EshopCommunity\Internal\Framework\DIContainer\ContainerBuilder;
use OxidEsales\EshopCommunity\Internal\Framework\Env\DotenvLoader;
use OxidEsales\EshopCommunity\Tests\ContainerTrait;
use OxidEsales\EshopCommunity\Tests\EnvTrait;
use OxidEsales\EshopCommunity\Tests\RequestTrait;
use OxidEsales\EshopCommunity\Tests\Unit\Internal\ContextStub;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;

final class EnvLoaderTest extends TestCase
{
use RequestTrait;
use ContainerTrait;
use EnvTrait;

private string $dotEnvFixture = '';
private string $fixtures = __DIR__ . '/Fixtures';
Expand Down Expand Up @@ -59,7 +56,7 @@ public function testApplicationEnvironmentIsDefined(): void
public function testApplicationEnvironmentCanBeRedefined(): void
{
$someValue = uniqid('some-value', true);
$this->loadEnvValue("$this->appEnvKey=$someValue");
$this->loadEnvFixture($this->fixtures, ["$this->appEnvKey=$someValue"]);

$currentEnvironment = getenv($this->appEnvKey);

Expand All @@ -74,26 +71,13 @@ public function testJsonDSNsWithSpecialCharactersWillBeParsedAsArray(): void
[$dsnString, $dsnString, $dsnString],
JSON_THROW_ON_ERROR
);
$this->loadEnvValue("$this->serializedEnvKey='$serializedValue'");
$this->setContainerFixture();
$this->loadEnvFixture($this->fixtures, ["$this->serializedEnvKey='$serializedValue'"]);
$this->createContainer();
$this->loadYamlFixture($this->fixtures);
$this->compileContainer();

$containerParameter = $this->getParameter($this->serializedParameterKey);

$this->assertEquals($dsnString, $containerParameter[2]);
}

private function setContainerFixture(): void
{
$containerBuilder = new ContainerBuilder(new ContextStub());
$this->container = $containerBuilder->getContainer();
$loader = new YamlFileLoader($this->container, new FileLocator(__DIR__));
$loader->load(Path::join($this->fixtures, 'services.yaml'));
$this->container->compile(true);
}

private function loadEnvValue(string $content): void
{
(new Filesystem())->dumpFile($this->dotEnvFixture, $content);
(new DotenvLoader($this->fixtures))->loadEnvironmentVariables();
}
}

0 comments on commit 713b31d

Please sign in to comment.