Skip to content

Commit

Permalink
refactor: improve messages
Browse files Browse the repository at this point in the history
  • Loading branch information
eleftrik committed Jun 9, 2024
1 parent cdb03db commit c6411ed
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
6 changes: 3 additions & 3 deletions resources/lang/en/translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'every_var_has_been_set' => 'Every required .env variable has been set',
'missing_vars_list' => 'Missing .env variables: :list',
'not_every_var_has_been_set' => 'Not every required .env variable has been set!',
'var_not_matching_value' => ":name is set to ':actual' instead of ':expected'",
'vars_not_matching_values' => 'Some variables does not match their expected value',
'vars_not_matching_values_list' => 'Some variables does not match their expected values: :list',
'var_not_matching_value' => ':name is set to :actual instead of :expected',
'vars_not_matching_values' => 'Some variables do not match their expected value',
'vars_not_matching_values_list' => 'Some variables do not match their expected values: :list',
];
2 changes: 1 addition & 1 deletion resources/lang/it/translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'every_var_has_been_set' => 'Tutte le variabili .env richieste sono state valorizzate',
'missing_vars_list' => 'Variabili .env mancanti: :list',
'not_every_var_has_been_set' => 'Non tutte le variabili .env richieste sono state valorizzate!',
'var_not_matching_value' => ":name è valorizzata a ':actual' invece di ':expected'",
'var_not_matching_value' => ':name è valorizzata a :actual invece di :expected',
'vars_not_matching_values' => 'Alcune variabili non sono state valorizzate con i valori previsti',
'vars_not_matching_values_list' => 'Alcune variabili non sono state valorizzate come previsto: :list',
];
21 changes: 17 additions & 4 deletions src/Health/Checks/EnvVars.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ protected function missingVars(Collection $vars): Collection
$missingVars = Collection::empty();

$vars->each(function (string $name) use ($missingVars) {
$value = getenv($name);
$value = env($name);
if (! $value) {
$missingVars->push($name);
}
Expand All @@ -205,12 +205,12 @@ protected function checkRequiredVarsWithValues(Collection $requiredVarsWithValue
$requiredVarsWithValues->each(function ($expectedValue, $name) use ($failingVarNames, $failingVarMessages) {
$actualValue = env($name);

if ($expectedValue != $actualValue) {
if ($expectedValue !== $actualValue) {
$failingVarNames->push($name);
$failingVarMessages->push(trans('health-env-vars::translations.var_not_matching_value', [
'name' => $name,
'expected' => $expectedValue,
'actual' => $actualValue,
'expected' => self::displayableValueOf($expectedValue),
'actual' => self::displayableValueOf($actualValue),
]));
}
});
Expand All @@ -227,4 +227,17 @@ protected function checkRequiredVarsWithValues(Collection $requiredVarsWithValue
])
);
}

public static function displayableValueOf(mixed $var): mixed
{
if (is_bool($var)) {
return var_export($var, true);
}

if (is_string($var)) {
return '"'.$var.'"';
}

return $var;
}
}
10 changes: 5 additions & 5 deletions tests/EnvVarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ function (string $currentEnvironment) {
$variableActualValue = 'another value';
$missingList = trans('health-env-vars::translations.var_not_matching_value', [
'name' => $variableName,
'actual' => $variableActualValue,
'expected' => $variableExpectedValue,
'actual' => EnvVars::displayableValueOf($variableActualValue),
'expected' => EnvVars::displayableValueOf($variableExpectedValue),
]);

expect($currentEnvironment)->toBeIn($environments);
Expand Down Expand Up @@ -120,7 +120,7 @@ function (string $currentEnvironment) {
$environments = ENVIRONMENTS;
$varsWithValues = [
'VAR1' => 'Some value',
'VAR2' => 42,
'VAR2' => '42',
'VAR3' => false,
];

Expand Down Expand Up @@ -161,8 +161,8 @@ function (string $environment) {
// ACT & ASSERT
$missingList = trans('health-env-vars::translations.var_not_matching_value', [
'name' => $variableName,
'actual' => $variableActualValue,
'expected' => $variableExpectedValue,
'actual' => EnvVars::displayableValueOf($variableActualValue),
'expected' => EnvVars::displayableValueOf($variableExpectedValue),
]);

$result = EnvVars::new()
Expand Down
20 changes: 10 additions & 10 deletions tests/MultipleEnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'returns ok if environment specific vars have been set and the environment matches the current one',
function () {
$varName = 'ENV_PROD_VAR1';
$specificEnvironment = 'production';
$specificEnvironment = ENVIRONMENT_PRODUCTION;

mockCurrentEnvironment($specificEnvironment);
initEnvVars([
Expand All @@ -35,15 +35,15 @@ function () {

test('environment specific vars are ignored if their environment does not match current one', function () {
$varName = 'ENV_PROD_VAR1';
$specificEnvironment = 'production';
$specificEnvironment = ENVIRONMENT_PRODUCTION;

// ensure code is running in an environment different from the one we're testing a var has been set
expect(app()->environment())->not->toEqual($specificEnvironment);
// ensure in the current environment the given var has not been set
initEnvVars([$varName => null]);

$result = EnvVars::new()
->requireVarsForEnvironment('production', [
->requireVarsForEnvironment(ENVIRONMENT_PRODUCTION, [
$varName,
])
->run();
Expand All @@ -58,18 +58,18 @@ function () {
function () {
$varName = 'ENV_PROD_VAR1';
$missingList = [$varName];
$specificEnvironment = 'production';
$specificEnvironment = ENVIRONMENT_PRODUCTION;

// 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]);

// WHEN switching to the desired environment...
mockCurrentEnvironment('production');
mockCurrentEnvironment(ENVIRONMENT_PRODUCTION);

$result = EnvVars::new()
->requireVarsForEnvironment('production', [
->requireVarsForEnvironment(ENVIRONMENT_PRODUCTION, [
$varName,
])
->run();
Expand Down Expand Up @@ -148,8 +148,8 @@ function (string $currentEnvironment) {

test('several specific environment vars can be specified', function () {
$result = EnvVars::new()
->requireVarsForEnvironment('staging', ['VAR1'])
->requireVarsForEnvironment('production', ['VAR2'])
->requireVarsForEnvironment(ENVIRONMENT_STAGING, ['VAR1'])
->requireVarsForEnvironment(ENVIRONMENT_PRODUCTION, ['VAR2'])
->run();

expect($result)
Expand All @@ -176,8 +176,8 @@ function (string $environment) {
// ACT & ASSERT
$missingList = trans('health-env-vars::translations.var_not_matching_value', [
'name' => $variableName,
'actual' => $variableActualValue,
'expected' => $variableExpectedValue,
'actual' => EnvVars::displayableValueOf($variableActualValue),
'expected' => EnvVars::displayableValueOf($variableExpectedValue),
]);

$result = EnvVars::new()
Expand Down

0 comments on commit c6411ed

Please sign in to comment.