Skip to content

Commit

Permalink
Merge pull request #58 from superbiche/fix/env-check-nullable-values
Browse files Browse the repository at this point in the history
fix(check): env check false positive when value is defined but null
  • Loading branch information
Gman98ish authored Jun 18, 2021
2 parents 9545884 + f3db734 commit 43fc12e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
7 changes: 7 additions & 0 deletions config/healthcheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@
'minutes-between-checks' => 5,
],

/*
* Default value for env checks.
* For each key, the check will call `env(KEY, config('healthcheck.env-default-key'))`
* to avoid false positives when `env(KEY)` is defined but is null.
*/
'env-check-key' => 'HEALTH_CHECK_ENV_DEFAULT_VALUE',

/*
* Additional config can be put here. For example, a health check
* for your .env file needs to know which keys need to be present.
Expand Down
4 changes: 3 additions & 1 deletion src/Checks/EnvHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ class EnvHealthCheck extends HealthCheck

public function status()
{
$default = config('healthcheck.env-check-key', 'HEALTH_CHECK_ENV_DEFAULT_VALUE');

foreach (config('healthcheck.required-env') as $env) {
if (env($env) === null) {
if (env($env, $default) === $default) {
$missing[] = $env;
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Checks/EnvHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,19 @@ public function shows_okay_if_all_required_env_params_are_present()

$this->assertTrue($status->isOkay());
}

/**
* @test
*/
public function shows_okay_if_required_env_param_is_present_but_null()
{
putenv('REDIS_PASSWORD=null');

config(['healthcheck.required-env' => [
'REDIS_PASSWORD',
]]);
$status = (new EnvHealthCheck)->status();

$this->assertTrue($status->isOkay());
}
}

0 comments on commit 43fc12e

Please sign in to comment.