diff --git a/source/Internal/Framework/Database/ConnectionFactory.php b/source/Internal/Framework/Database/ConnectionFactory.php index 8e2300add4..c3f39076ee 100644 --- a/source/Internal/Framework/Database/ConnectionFactory.php +++ b/source/Internal/Framework/Database/ConnectionFactory.php @@ -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, ) { } @@ -30,7 +29,7 @@ public function create(): Connection $this->databaseLoggerFactory->getDatabaseLogger() ); return DriverManager::getConnection( - ['url' => $this->systemConfigurationDao->get()->getDatabaseUrl()], + $this->connectionParameterProvider->getParameters(), $dbalConfiguration, ); } diff --git a/source/Internal/Framework/Database/ConnectionParameterProvider.php b/source/Internal/Framework/Database/ConnectionParameterProvider.php new file mode 100644 index 0000000000..5f0b70716a --- /dev/null +++ b/source/Internal/Framework/Database/ConnectionParameterProvider.php @@ -0,0 +1,25 @@ + $this->systemConfigurationDao->get()->getDatabaseUrl()]; + } +} diff --git a/source/Internal/Framework/Database/ConnectionParameterProviderInterface.php b/source/Internal/Framework/Database/ConnectionParameterProviderInterface.php new file mode 100644 index 0000000000..00d2fc4f60 --- /dev/null +++ b/source/Internal/Framework/Database/ConnectionParameterProviderInterface.php @@ -0,0 +1,13 @@ +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); + } } diff --git a/tests/EnvTrait.php b/tests/EnvTrait.php new file mode 100644 index 0000000000..4249769985 --- /dev/null +++ b/tests/EnvTrait.php @@ -0,0 +1,26 @@ +dumpFile($fixtureFile, implode("\n", $envFileLines),); + (new DotenvLoader($fixtureDir))->loadEnvironmentVariables(); + $filesystem->remove($fixtureFile); + } +} diff --git a/tests/Integration/Internal/Framework/Database/ConnectionFactoryTest.php b/tests/Integration/Internal/Framework/Database/ConnectionFactoryTest.php index 99fe7b11e8..6663fdb353 100644 --- a/tests/Integration/Internal/Framework/Database/ConnectionFactoryTest.php +++ b/tests/Integration/Internal/Framework/Database/ConnectionFactoryTest.php @@ -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 @@ -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(); } } diff --git a/tests/Integration/Internal/Framework/Env/EnvLoaderTest.php b/tests/Integration/Internal/Framework/Env/EnvLoaderTest.php index 452e2e030d..3f074bd59e 100644 --- a/tests/Integration/Internal/Framework/Env/EnvLoaderTest.php +++ b/tests/Integration/Internal/Framework/Env/EnvLoaderTest.php @@ -10,14 +10,10 @@ 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; @@ -25,6 +21,7 @@ final class EnvLoaderTest extends TestCase { use RequestTrait; use ContainerTrait; + use EnvTrait; private string $dotEnvFixture = ''; private string $fixtures = __DIR__ . '/Fixtures'; @@ -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); @@ -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(); - } }