Skip to content

Commit

Permalink
OXDEV-7248 Update env test for serialized values
Browse files Browse the repository at this point in the history
  • Loading branch information
liulka-oxid committed Apr 19, 2024
1 parent 642e8d0 commit 8537c33
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
72 changes: 57 additions & 15 deletions tests/Integration/Internal/Framework/Env/EnvLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,91 @@

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\RequestTrait;
use OxidEsales\Facts\Facts;
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;

private string $dotEnvFixture = '';
private string $fixtures = __DIR__ . '/Fixtures';
private string $appEnvKey = 'OXID_ENV';
private string $serializedEnvKey = 'SERIALIZED_VALUE_KEY';
private string $serializedParameterKey = 'serialized_value_key';

private string $envKey = 'OXID_ENV';

public function setUp(): void
{
parent::setUp();
$this->backupRequestData();
$this->dotEnvFixture = Path::join($this->fixtures, '.env');
}

public function tearDown(): void
{
(new Filesystem())->remove($this->dotEnvFixture);
$this->restoreRequestData();
parent::tearDown();
}

public function testApplicationEnvironmentIsDefined(): void
{
(new DotenvLoader(
(new Facts())->getShopRootPath()
))
->loadEnvironmentVariables();
$currentEnvironment = getenv($this->envKey);
(new BootstrapConfigurationFactory())->create();

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

$this->assertNotEmpty($currentEnvironment);
$this->assertIsString($currentEnvironment);
}

public function testLoaderUsesExpectedEnvironmentKey(): void
public function testApplicationEnvironmentCanBeRedefined(): void
{
$someValue = uniqid('some-value', true);
$this->loadEnvValue("$this->appEnvKey=$someValue");

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

$this->assertEquals($someValue, $currentEnvironment);
}

public function testJsonDSNsWithSpecialCharactersWillBeParsedAsArray(): void
{
(new DotenvLoader(
__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures'
))
->loadEnvironmentVariables();
$currentEnvironment = getenv($this->envKey);
$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->loadEnvValue("$this->serializedEnvKey='$serializedValue'");
$this->setContainerFixture();

$this->assertEquals('abcde', $currentEnvironment);
$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();
}
}
1 change: 0 additions & 1 deletion tests/Integration/Internal/Framework/Env/Fixtures/.env

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
serialized_value_key: '%env(json:SERIALIZED_VALUE_KEY)%'

0 comments on commit 8537c33

Please sign in to comment.