Skip to content

Commit

Permalink
Changed non-thread safe getenv to superglobals (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrkreft authored Mar 1, 2024
1 parent 888abac commit b51d98e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 57 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/StorageAdapter/LocalEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
85 changes: 29 additions & 56 deletions tests/StorageAdapter/LocalEnvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit b51d98e

Please sign in to comment.