From 31d60ec8ea020aeba23598bb0cce34ac0899e736 Mon Sep 17 00:00:00 2001 From: ashraf Date: Tue, 21 Jan 2025 16:40:05 +0100 Subject: [PATCH] OXDEV-8715 Environment loader without file --- source/Internal/Framework/Env/DotenvLoader.php | 16 ++++++++++++---- .../Internal/Framework/Env/EnvLoaderTest.php | 12 ++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/source/Internal/Framework/Env/DotenvLoader.php b/source/Internal/Framework/Env/DotenvLoader.php index 6eb9e2dadf..571ed64c9a 100644 --- a/source/Internal/Framework/Env/DotenvLoader.php +++ b/source/Internal/Framework/Env/DotenvLoader.php @@ -10,6 +10,7 @@ namespace OxidEsales\EshopCommunity\Internal\Framework\Env; use Symfony\Component\Dotenv\Dotenv; +use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Path; class DotenvLoader @@ -17,17 +18,24 @@ class DotenvLoader private string $envKey = 'OXID_ENV'; private string $envFile = '.env'; - public function __construct(private readonly string $pathToEnvFiles) + public function __construct( + private readonly string $pathToEnvFiles + ) { } public function loadEnvironmentVariables(): void { + $filesystem = new Filesystem(); + $envFilePath = Path::join($this->pathToEnvFiles, $this->envFile); + + if (!$filesystem->exists($envFilePath)) { + return; + } + $dotEnv = new Dotenv($this->envKey); $dotEnv ->usePutenv() - ->loadEnv( - Path::join($this->pathToEnvFiles, $this->envFile) - ); + ->loadEnv($envFilePath); } } \ No newline at end of file diff --git a/tests/Integration/Internal/Framework/Env/EnvLoaderTest.php b/tests/Integration/Internal/Framework/Env/EnvLoaderTest.php index 2797bef788..dfd984180a 100644 --- a/tests/Integration/Internal/Framework/Env/EnvLoaderTest.php +++ b/tests/Integration/Internal/Framework/Env/EnvLoaderTest.php @@ -9,6 +9,7 @@ namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Framework\Env; +use OxidEsales\EshopCommunity\Internal\Framework\Env\DotenvLoader; use OxidEsales\EshopCommunity\Tests\ContainerTrait; use OxidEsales\EshopCommunity\Tests\EnvTrait; use PHPUnit\Framework\TestCase; @@ -41,11 +42,22 @@ public function tearDown(): void public function testApplicationEnvironmentIsDefined(): void { + $this->loadEnvFixture($this->fixtures, ["key=value"]); + $currentEnvironment = getenv($this->appEnvKey); $this->assertNotEmpty($currentEnvironment); } + public function testLoadNonExistentEnvironmentFile(): void + { + $dotenvLoader = new DotenvLoader('non-existent-path'); + + $this->expectNotToPerformAssertions(); + + $dotenvLoader->loadEnvironmentVariables(); + } + public function testApplicationEnvironmentCanBeRedefined(): void { $someValue = uniqid('some-value', true);