Skip to content

Commit

Permalink
OXDEV-8715 Environment loader without file
Browse files Browse the repository at this point in the history
  • Loading branch information
AshrafOxid committed Jan 21, 2025
1 parent eed3cf8 commit 31d60ec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
16 changes: 12 additions & 4 deletions source/Internal/Framework/Env/DotenvLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,32 @@
namespace OxidEsales\EshopCommunity\Internal\Framework\Env;

use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;

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);
}
}
12 changes: 12 additions & 0 deletions tests/Integration/Internal/Framework/Env/EnvLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 31d60ec

Please sign in to comment.