diff --git a/src/SAREhub/Commons/Misc/EnvironmentHelper.php b/src/SAREhub/Commons/Misc/EnvironmentHelper.php index db13bc7..5e490e5 100644 --- a/src/SAREhub/Commons/Misc/EnvironmentHelper.php +++ b/src/SAREhub/Commons/Misc/EnvironmentHelper.php @@ -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; } -} \ No newline at end of file +} diff --git a/tests/SAREhub/Commons/Misc/EnvironmentHelperTest.php b/tests/SAREhub/Commons/Misc/EnvironmentHelperTest.php index f15369f..da019ca 100644 --- a/tests/SAREhub/Commons/Misc/EnvironmentHelperTest.php +++ b/tests/SAREhub/Commons/Misc/EnvironmentHelperTest.php @@ -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"); } }