-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a2023e
commit e3af1f4
Showing
10 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
OXID_ENV=prod | ||
OXID_DEBUG=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © OXID eSales AG. All rights reserved. | ||
* See LICENSE file for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OxidEsales\EshopCommunity\Internal\Framework\Env; | ||
|
||
use Symfony\Component\Dotenv\Dotenv; | ||
use Symfony\Component\Filesystem\Path; | ||
|
||
class DotenvLoader | ||
{ | ||
private string $envKey = 'OXID_ENV'; | ||
private string $debugKey = 'OXID_DEBUG'; | ||
private string $envFile = '.env'; | ||
|
||
public function __construct(private readonly string $pathToEnvFiles) | ||
{ | ||
} | ||
|
||
public function loadEnvironmentVariables(): void | ||
{ | ||
$dotEnv = new Dotenv($this->envKey, $this->debugKey); | ||
$dotEnv | ||
->usePutenv() | ||
->loadEnv( | ||
Path::join($this->pathToEnvFiles, $this->envFile) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?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; | ||
|
||
/** | ||
* Run tests that modify env values in a separate process | ||
* to avoid discrepancy between getenv('value') and $_ENV['value'] | ||
*/ | ||
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); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
tests/Integration/Internal/Framework/Env/EnvLoaderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © OXID eSales AG. All rights reserved. | ||
* See LICENSE file for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Framework\Env; | ||
|
||
use OxidEsales\EshopCommunity\Tests\ContainerTrait; | ||
use OxidEsales\EshopCommunity\Tests\EnvTrait; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\Filesystem\Path; | ||
|
||
final class EnvLoaderTest extends TestCase | ||
{ | ||
use ContainerTrait; | ||
use EnvTrait; | ||
|
||
private string $dotEnvFixture = ''; | ||
private string $fixtures = __DIR__ . '/Fixtures/EnvLoader'; | ||
private string $appEnvKey = 'OXID_ENV'; | ||
private string $serializedEnvKey = 'SERIALIZED_VALUE_KEY'; | ||
private string $serializedParameterKey = 'serialized_value_key'; | ||
|
||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->dotEnvFixture = Path::join($this->fixtures, '.env'); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
(new Filesystem())->remove($this->dotEnvFixture); | ||
parent::tearDown(); | ||
} | ||
|
||
public function testApplicationEnvironmentIsDefined(): void | ||
{ | ||
$currentEnvironment = getenv($this->appEnvKey); | ||
|
||
$this->assertNotEmpty($currentEnvironment); | ||
} | ||
|
||
public function testApplicationEnvironmentCanBeRedefined(): void | ||
{ | ||
$someValue = uniqid('some-value', true); | ||
$this->loadEnvFixture($this->fixtures, ["$this->appEnvKey=$someValue"]); | ||
|
||
$currentEnvironment = getenv($this->appEnvKey); | ||
|
||
$this->assertEquals($someValue, $currentEnvironment); | ||
} | ||
|
||
public function testJsonDSNsWithSpecialCharactersWillBeParsedAsArray(): void | ||
{ | ||
$somePassword = '"\[(üÄ\\" *123,.;)::""'; | ||
$dsnString = "mysql://username:$somePassword@123.255.255.255:3306/db-name?charset=utf8&driverOptions[1002]=\"SET @@SESSION.sql_mode=\"\"\""; | ||
$serializedValue = json_encode( | ||
[$dsnString, $dsnString, $dsnString], | ||
JSON_THROW_ON_ERROR | ||
); | ||
$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]); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
tests/Integration/Internal/Framework/Env/Fixtures/EnvLoader/services.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
parameters: | ||
serialized_value_key: '%env(json:SERIALIZED_VALUE_KEY)%' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters