Skip to content

Commit

Permalink
EnvironmentHelper::getVars added prefix parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrzej Wasiak committed Jan 27, 2018
1 parent bf8346f commit a47791d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
23 changes: 17 additions & 6 deletions src/SAREhub/Commons/Misc/EnvironmentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,31 @@

class EnvironmentHelper
{

public static function getVar(string $name, $default = null)
/**
* Returns value of environment variable or default value
* @param string $name
* @param null $defaultValue
* @return mixed value from env or defaultValue
*/
public static function getVar(string $name, $defaultValue = null)
{
$value = getenv($name);
return $value !== false ? $value : $default;
return $value !== false ? $value : $defaultValue;
}

public static function getVars(array $schema): array
/**
* Returns values of environment variables defined in schema parameter
* @param array $schema Format: ["variableName" => defaultValue]
* @param string $prefix Its added to variableName when getting value from env
* @return array ["variableName" => valueFromEnv|defaultValue]
*/
public static function getVars(array $schema, string $prefix = ""): array
{
$env = [];
foreach ($schema as $name => $default) {
$env[$name] = self::getVar($name, $default);
$env[$name] = self::getVar($prefix . $name, $default);
}

return $env;
}
}
}
40 changes: 26 additions & 14 deletions tests/SAREhub/Commons/Misc/EnvironmentHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,45 @@

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));
$variableName = $this->getName();
$this->putenv($variableName, 1);
$this->assertEquals(1, EnvironmentHelper::getVar($variableName));
}

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

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

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

public function testGetVarsWithCustomPrefix()
{
$prefix = "prefix_";
$variableName = $this->getName();
$this->putenv($prefix . $variableName, 1);
$this->assertEquals([$variableName => 1], EnvironmentHelper::getVars([$variableName => null], $prefix));
}

private function putenv(string $name, $value)
{
putenv("$name=$value");
}
}

0 comments on commit a47791d

Please sign in to comment.