diff --git a/src/Health/Checks/EnvVars.php b/src/Health/Checks/EnvVars.php index eed5661..ae52243 100755 --- a/src/Health/Checks/EnvVars.php +++ b/src/Health/Checks/EnvVars.php @@ -186,7 +186,8 @@ protected function missingVars(Collection $vars): Collection $vars->each(function (string $name) use ($missingVars) { $value = env($name); - if (! $value) { + + if ($value === null) { $missingVars->push($name); } }); diff --git a/tests/EnvVarsTest.php b/tests/EnvVarsTest.php index 2ebe5f9..89a609d 100644 --- a/tests/EnvVarsTest.php +++ b/tests/EnvVarsTest.php @@ -3,6 +3,10 @@ use Encodia\Health\Checks\EnvVars; use Spatie\Health\Enums\Status; +afterEach(function () { + unsetEnvVars(['VAR1', 'VAR2', 'VAR3', 'VAR4', 'ENV_VAR1', 'ENV_VAR2', 'ENV_PROD_VAR1']); +}); + it('returns ok when no variable names have been provided', function () { $result = EnvVars::new()->run(); @@ -40,16 +44,33 @@ ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); }); +it('returns ok when a boolean variable is initialized to false', function () { + initEnvVars([ + 'ENV_VAR1' => false, + ]); + + $result = EnvVars::new() + ->requireVars([ + 'ENV_VAR1', + ]) + ->run(); + + expect($result) + ->status->toBe(Status::ok()) + ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); +}); + it('returns an error when not every provided name matches a .env variable with a non-empty value', function () { $missingList = ['ENV_VAR1']; - // GIVEN some .env variables which has been initialized with a non-empty value and one variable which has an - // empty value + // GIVEN one .env variable which has been initialized with a non-empty value and one variable which has not + // been declared initEnvVars([ - 'ENV_VAR1' => '', 'ENV_VAR2' => 'bar', ]); + expect(env('ENV_VAR1'))->toBeNull(); + $result = EnvVars::new() ->requireVars([ 'ENV_VAR1', diff --git a/tests/MultipleEnvironmentTest.php b/tests/MultipleEnvironmentTest.php index 2f40c08..d5e2c6c 100644 --- a/tests/MultipleEnvironmentTest.php +++ b/tests/MultipleEnvironmentTest.php @@ -62,7 +62,7 @@ function () { // ensure code is running in an environment different from the one we're testing that a var has been set expect(currentEnvironment())->not->toEqual($specificEnvironment); // ensure in the current environment the given var has not been set - initEnvVars([$varName => null]); + expect(env($varName))->toBeNull(); // WHEN switching to the desired environment... mockCurrentEnvironment(ENVIRONMENT_PRODUCTION);