From b51d98e40135cec87324b9d05f0003b887fdd1aa Mon Sep 17 00:00:00 2001 From: Piotr Kreft <49146985+piotrkreft@users.noreply.github.com> Date: Fri, 1 Mar 2024 13:17:52 +0100 Subject: [PATCH] Changed non-thread safe getenv to superglobals (#15) --- CHANGELOG.md | 4 ++ src/StorageAdapter/LocalEnv.php | 2 +- tests/StorageAdapter/LocalEnvTest.php | 85 +++++++++------------------ 3 files changed, 34 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 186cf73..84ad8c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.7.0] - 2024-03-01 +### Changed +- LocalEnv adapter getenv function to superglobal usage. + ## [0.6.0] - 2021-02-12 ### Added - Tested component against PHP8. diff --git a/src/StorageAdapter/LocalEnv.php b/src/StorageAdapter/LocalEnv.php index c7be2f2..5b3b028 100644 --- a/src/StorageAdapter/LocalEnv.php +++ b/src/StorageAdapter/LocalEnv.php @@ -15,7 +15,7 @@ class LocalEnv implements StorageAdapterInterface public function fetch(string $environment): array { $entries = []; - foreach (getenv() as $name => $value) { + foreach ($_SERVER + $_ENV as $name => $value) { $entries[] = new Entry($name, $value); } diff --git a/tests/StorageAdapter/LocalEnvTest.php b/tests/StorageAdapter/LocalEnvTest.php index 8e26e25..51247ed 100644 --- a/tests/StorageAdapter/LocalEnvTest.php +++ b/tests/StorageAdapter/LocalEnvTest.php @@ -2,69 +2,42 @@ declare(strict_types=1); -namespace PK\Config\StorageAdapter -{ - use PK\Tests\Config\StorageAdapter\LocalEnvTest; +namespace PK\Tests\Config\StorageAdapter; + +use PHPUnit\Framework\TestCase; +use PK\Config\StorageAdapter\LocalEnv; +class LocalEnvTest extends TestCase +{ /** - * @return string[] + * @var LocalEnv */ - function getenv(): array - { - if (LocalEnvTest::$mockGetenv) { - return [ - 'ENV_VAR_1' => 'env_var_value_1', - 'ENV_VAR_2' => 'env_var_value_2', - ]; - } + private $adapter; - return \getenv(); + protected function setUp(): void + { + $this->adapter = new LocalEnv(); + $_SERVER['ENV_VAR_1'] = 'env_var_value_1'; + $_ENV['ENV_VAR_2'] = 'env_var_value_2'; } -} -namespace PK\Tests\Config\StorageAdapter -{ - use PHPUnit\Framework\TestCase; - use PK\Config\Entry; - use PK\Config\StorageAdapter\LocalEnv; - class LocalEnvTest extends TestCase + public function testShouldFetch(): void { - /** - * @var bool - */ - public static $mockGetenv = false; - - /** - * @var LocalEnv - */ - private $adapter; - - protected function setUp(): void - { - $this->adapter = new LocalEnv(); - } - - protected function tearDown(): void - { - self::$mockGetenv = false; - } - - public function testShouldFetch(): void - { - // given - self::$mockGetenv = true; - - // when - $entries = $this->adapter->fetch('some'); - - // then - $this->assertEquals( - [ - new Entry('ENV_VAR_1', 'env_var_value_1'), - new Entry('ENV_VAR_2', 'env_var_value_2'), - ], - $entries - ); + // when + $entries = $this->adapter->fetch('some'); + + // then + $foundVar1 = $foundVar2 = false; + foreach ($entries as $entry) { + if ('ENV_VAR_1' === $entry->getName()) { + $foundVar1 = $entry->getValue(); + } + if ('ENV_VAR_2' !== $entry->getName()) { + continue; + } + $foundVar2 = $entry->getValue(); } + $this->assertEquals('env_var_value_1', $foundVar1); + $this->assertEquals('env_var_value_2', $foundVar2); } }