Skip to content

Commit

Permalink
added EnvironemntHelper for getting variables (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mararok authored May 29, 2017
1 parent ea96a4e commit 15c3c87
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/SAREhub/Commons/Misc/EnvironmentHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace SAREhub\Commons\Misc;


class EnvironmentHelper {

public static function getVar(string $name, $default = null) {
$value = getenv($name);
return $value !== false ? $value : $default;
}

public static function getVars(array $schema): array {
$env = [];
foreach ($schema as $name => $default) {
$env[$name] = self::getVar($name, $default);
}

return $env;
}
}
32 changes: 32 additions & 0 deletions tests/SAREhub/Commons/Misc/EnvironmentHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace SAREhub\Commons\Misc;

use PHPUnit\Framework\TestCase;

class EnvironmentHelperTest extends TestCase {

const VARIABLE = 'ENV_HELPER_TEST';

public function setUp() {
putenv(self::VARIABLE);
}

public function testGetVarWhenExists() {
putenv(self::VARIABLE.'=1');
$this->assertEquals(1, EnvironmentHelper::getVar(self::VARIABLE));
}

public function testGetVarWhenNotExists() {
$this->assertEquals(1, EnvironmentHelper::getVar(self::VARIABLE, 1));
}

public function testGetVarsWhenExists() {
putenv(self::VARIABLE.'=1');
$this->assertEquals([self::VARIABLE => 1], EnvironmentHelper::getVars([self::VARIABLE => 2]));
}

public function testGetVarsWhenNotExists() {
$this->assertEquals([self::VARIABLE => 2], EnvironmentHelper::getVars([self::VARIABLE => 2]));
}
}

0 comments on commit 15c3c87

Please sign in to comment.