Skip to content

Commit

Permalink
fix: an env() variable initialized to false was evaluated as missing
Browse files Browse the repository at this point in the history
  • Loading branch information
eleftrik committed Jun 10, 2024
1 parent 842f8f9 commit 4500858
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Health/Checks/EnvVars.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down
27 changes: 24 additions & 3 deletions tests/EnvVarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion tests/MultipleEnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4500858

Please sign in to comment.