From c6411edee703dedd2ad201b9383fdd8d2f615f46 Mon Sep 17 00:00:00 2001 From: Erik D'Ercole Date: Sun, 9 Jun 2024 17:51:29 +0200 Subject: [PATCH] refactor: improve messages --- resources/lang/en/translations.php | 6 +++--- resources/lang/it/translations.php | 2 +- src/Health/Checks/EnvVars.php | 21 +++++++++++++++++---- tests/EnvVarsTest.php | 10 +++++----- tests/MultipleEnvironmentTest.php | 20 ++++++++++---------- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/resources/lang/en/translations.php b/resources/lang/en/translations.php index fa1de96..32b9b0e 100644 --- a/resources/lang/en/translations.php +++ b/resources/lang/en/translations.php @@ -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', ]; diff --git a/resources/lang/it/translations.php b/resources/lang/it/translations.php index 2255151..840d52c 100644 --- a/resources/lang/it/translations.php +++ b/resources/lang/it/translations.php @@ -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', ]; diff --git a/src/Health/Checks/EnvVars.php b/src/Health/Checks/EnvVars.php index 776d6b9..e2cf128 100755 --- a/src/Health/Checks/EnvVars.php +++ b/src/Health/Checks/EnvVars.php @@ -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); } @@ -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), ])); } }); @@ -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; + } } diff --git a/tests/EnvVarsTest.php b/tests/EnvVarsTest.php index 8f0f61d..c3b7423 100644 --- a/tests/EnvVarsTest.php +++ b/tests/EnvVarsTest.php @@ -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); @@ -120,7 +120,7 @@ function (string $currentEnvironment) { $environments = ENVIRONMENTS; $varsWithValues = [ 'VAR1' => 'Some value', - 'VAR2' => 42, + 'VAR2' => '42', 'VAR3' => false, ]; @@ -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() diff --git a/tests/MultipleEnvironmentTest.php b/tests/MultipleEnvironmentTest.php index b4e9794..7caa9c0 100644 --- a/tests/MultipleEnvironmentTest.php +++ b/tests/MultipleEnvironmentTest.php @@ -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([ @@ -35,7 +35,7 @@ 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); @@ -43,7 +43,7 @@ function () { initEnvVars([$varName => null]); $result = EnvVars::new() - ->requireVarsForEnvironment('production', [ + ->requireVarsForEnvironment(ENVIRONMENT_PRODUCTION, [ $varName, ]) ->run(); @@ -58,7 +58,7 @@ 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); @@ -66,10 +66,10 @@ function () { initEnvVars([$varName => null]); // WHEN switching to the desired environment... - mockCurrentEnvironment('production'); + mockCurrentEnvironment(ENVIRONMENT_PRODUCTION); $result = EnvVars::new() - ->requireVarsForEnvironment('production', [ + ->requireVarsForEnvironment(ENVIRONMENT_PRODUCTION, [ $varName, ]) ->run(); @@ -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) @@ -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()