From 4080d55e5288c2591d49fcd6cf4dca6ec7409401 Mon Sep 17 00:00:00 2001 From: Erik D'Ercole Date: Sat, 8 Jun 2024 18:35:43 +0200 Subject: [PATCH] refactor: tests; remove messages for a certain environment --- resources/lang/en/translations.php | 3 +- resources/lang/it/translations.php | 3 +- src/Health/Checks/EnvVars.php | 5 +- tests/EnvVarsTest.php | 299 +++++++++++++++---------- tests/MultipleEnvironmentTest.php | 348 ++++++++++++----------------- tests/Pest.php | 6 + 6 files changed, 335 insertions(+), 329 deletions(-) diff --git a/resources/lang/en/translations.php b/resources/lang/en/translations.php index d5d3d0f..fa1de96 100644 --- a/resources/lang/en/translations.php +++ b/resources/lang/en/translations.php @@ -3,9 +3,8 @@ return [ 'every_var_has_been_set' => 'Every required .env variable has been set', 'missing_vars_list' => 'Missing .env variables: :list', - 'missing_vars_list_in_environment' => "Missing .env variables in ':environment' environment: :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 value in ':environment' environment: :list", + 'vars_not_matching_values_list' => 'Some variables does not match their expected values: :list', ]; diff --git a/resources/lang/it/translations.php b/resources/lang/it/translations.php index 76127b1..2255151 100644 --- a/resources/lang/it/translations.php +++ b/resources/lang/it/translations.php @@ -3,9 +3,8 @@ return [ 'every_var_has_been_set' => 'Tutte le variabili .env richieste sono state valorizzate', 'missing_vars_list' => 'Variabili .env mancanti: :list', - 'missing_vars_list_in_environment' => "Variabili .env mancanti nell'ambiente ':environment': :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'", '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 nell'ambiente ':environment': :list", + '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 da3768f..bd0b361 100755 --- a/src/Health/Checks/EnvVars.php +++ b/src/Health/Checks/EnvVars.php @@ -39,7 +39,7 @@ public function run(): Result $result = Result::make(); - // Check variables matches their values + // Check variables match their values // Merge the non-environment-specific collection with the current-environment-specific one $requiredVarsWithValues = $this->requiredVarsWithValues->merge( @@ -76,7 +76,7 @@ public function run(): Result return $result->meta($missingVars->toArray()) ->shortSummary(trans('health-env-vars::translations.not_every_var_has_been_set')) ->failed( - trans('health-env-vars::translations.missing_vars_list_in_environment', [ + trans('health-env-vars::translations.missing_vars_list', [ 'environment' => App::environment(), 'list' => $missingVars->implode(','), ]) @@ -234,7 +234,6 @@ protected function checkRequiredVarsWithValues(Collection $requiredVarsWithValue meta: $failingVarNames->toArray(), summary: trans('health-env-vars::translations.vars_not_matching_values'), message: trans('health-env-vars::translations.vars_not_matching_values_list', [ - 'environment' => App::environment(), 'list' => $failingVarMessages->implode('; '), ]) ); diff --git a/tests/EnvVarsTest.php b/tests/EnvVarsTest.php index 9bb62c5..8f0f61d 100644 --- a/tests/EnvVarsTest.php +++ b/tests/EnvVarsTest.php @@ -11,142 +11,211 @@ ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); }); -it('returns ok when an empty set of variable names has been provided', function () { - $result = EnvVars::new() - ->requireVars([]) - ->run(); - - expect($result) - ->status->toBe(Status::ok()) - ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); -}); - -it('returns ok when every provided name matches a .env variable with a non-empty value', function () { - // GIVEN some .env variables which has been initialized with a non-empty value - initEnvVars([ - 'ENV_VAR1' => 'foo', - 'ENV_VAR2' => 'bar', - ]); - - $result = EnvVars::new() - ->requireVars([ - 'ENV_VAR1', - 'ENV_VAR2', - ]) - ->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 - initEnvVars([ - 'ENV_VAR1' => '', - 'ENV_VAR2' => 'bar', - ]); - - $result = EnvVars::new() - ->requireVars([ - 'ENV_VAR1', - 'ENV_VAR2', - ]) - ->run(); - - expect($result) - ->status->toBe(Status::failed()) - ->meta->toEqual($missingList) - ->shortSummary->toBe(trans('health-env-vars::translations.not_every_var_has_been_set')) - ->notificationMessage->toBe( - trans('health-env-vars::translations.missing_vars_list', ['list' => implode(', ', $missingList)]) - ); -}); +describe('when vars need to be set', function () { + it('returns ok when an empty set of variable names has been provided', function () { + $result = EnvVars::new() + ->requireVars([]) + ->run(); -it( - 'returns ok if environment specific vars have been set and the environment matches the current one', - function () { - $varName = 'ENV_PROD_VAR1'; - $specificEnvironment = 'production'; + expect($result) + ->status->toBe(Status::ok()) + ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); + }); - mockCurrentEnvironment($specificEnvironment); + it('returns ok when every provided name matches a .env variable with a non-empty value', function () { + // GIVEN some .env variables which has been initialized with a non-empty value initEnvVars([ - $varName => 'some_value', + 'ENV_VAR1' => 'foo', + 'ENV_VAR2' => 'bar', ]); - // ensure code is running in the same environment we're testing that a var has been set - expect(currentEnvironment())->toEqual($specificEnvironment); $result = EnvVars::new() - ->requireVarsForEnvironment($specificEnvironment, [ - $varName, + ->requireVars([ + 'ENV_VAR1', + 'ENV_VAR2', ]) ->run(); expect($result) ->status->toBe(Status::ok()) ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); - } -); + }); -test('environment specific vars are ignored if their environment does not match current one', function () { - $varName = 'ENV_PROD_VAR1'; - $specificEnvironment = 'production'; + it('returns an error when not every provided name matches a .env variable with a non-empty value', function () { + $missingList = ['ENV_VAR1']; - // 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]); + // GIVEN some .env variables which has been initialized with a non-empty value and one variable which has an + // empty value + initEnvVars([ + 'ENV_VAR1' => '', + 'ENV_VAR2' => 'bar', + ]); - $result = EnvVars::new() - ->requireVarsForEnvironment('production', [ - $varName, - ]) - ->run(); + $result = EnvVars::new() + ->requireVars([ + 'ENV_VAR1', + 'ENV_VAR2', + ]) + ->run(); - expect($result) - ->status->toBe(Status::ok()) - ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); + expect($result) + ->status->toBe(Status::failed()) + ->meta->toEqual($missingList) + ->shortSummary->toBe(trans('health-env-vars::translations.not_every_var_has_been_set')) + ->notificationMessage->toBe( + trans('health-env-vars::translations.missing_vars_list', ['list' => implode(', ', $missingList)]) + ); + }); }); -it('returns an error if environment specific vars are required and they are not set in that environment', function () { - $varName = 'ENV_PROD_VAR1'; - $missingList = [$varName]; - $specificEnvironment = '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]); +describe('when vars need to match values', function () { + it( + "returns an error if a var doesn't match the expected value in the current environment", + function (string $currentEnvironment) { + // ARRANGE + + $environments = ENVIRONMENTS; + $variableName = 'VAR1'; + $variableExpectedValue = 'expected value'; + $variableActualValue = 'another value'; + $missingList = trans('health-env-vars::translations.var_not_matching_value', [ + 'name' => $variableName, + 'actual' => $variableActualValue, + 'expected' => $variableExpectedValue, + ]); + + expect($currentEnvironment)->toBeIn($environments); + mockCurrentEnvironment($currentEnvironment); + + // init variable with a value different from the expected one + initEnvVars([ + $variableName => $variableActualValue, + ]); + expect(env($variableName))->not->toEqual($variableExpectedValue); + + // ACT & ASSERT + $result = EnvVars::new() + ->requireVarsMatchValues([ + $variableName => $variableExpectedValue, + ]) + ->run(); + + expect($result) + ->meta->toEqual([$variableName]) + ->status->toBe(Status::failed()) + ->shortSummary->toBe(trans('health-env-vars::translations.vars_not_matching_values')) + ->notificationMessage->toBe( + trans('health-env-vars::translations.vars_not_matching_values_list', [ + 'list' => $missingList, + ]) + ); + } + )->with([ENVIRONMENT_STAGING]); + + it( + 'returns OK if all vars match the expected values in the current environment', + function (string $currentEnvironment) { + // ARRANGE + + $environments = ENVIRONMENTS; + $varsWithValues = [ + 'VAR1' => 'Some value', + 'VAR2' => 42, + 'VAR3' => false, + ]; + + expect($currentEnvironment)->toBeIn($environments); + mockCurrentEnvironment($currentEnvironment); + + // init variable with a value different from the expected one + initEnvVars($varsWithValues); + + // ACT & ASSERT + $result = EnvVars::new() + ->requireVarsMatchValues($varsWithValues) + ->run(); + + expect($result) + ->status->toBe(Status::ok()) + ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); + } + )->with([ENVIRONMENT_STAGING]); +}); - // WHEN switching to the desired environment... - mockCurrentEnvironment('production'); +describe('when vars need to match values for a single environment', function () { + it( + "fails if at least one var doesn't match its value for the current environment ", + function (string $environment) { + // ARRANGE + mockCurrentEnvironment($environment); + + $variableName = 'VAR2'; + $variableActualValue = 'different'; + $variableExpectedValue = 'bar'; + $vars = [ + 'VAR1' => 'foo', + $variableName => $variableActualValue, + ]; + initEnvVars($vars); + + // ACT & ASSERT + $missingList = trans('health-env-vars::translations.var_not_matching_value', [ + 'name' => $variableName, + 'actual' => $variableActualValue, + 'expected' => $variableExpectedValue, + ]); + + $result = EnvVars::new() + ->requireVarsMatchValuesForEnvironment($environment, [ + 'VAR1' => 'foo', + 'VAR2' => $variableExpectedValue, + ]) + ->run(); + + expect($result) + ->status->toBe(Status::failed()) + ->shortSummary->toBe(trans('health-env-vars::translations.vars_not_matching_values')) + ->notificationMessage->toBe( + trans('health-env-vars::translations.vars_not_matching_values_list', [ + 'list' => $missingList, + ]) + ); + } + )->with([ENVIRONMENT_STAGING]); + + it('returns OK if vars match their values for the current environment ', function (string $environment) { + // ARRANGE + mockCurrentEnvironment($environment); + $vars = [ + 'VAR1' => 'foo', + ]; + initEnvVars($vars); + + // ACT & ASSERT + $result = EnvVars::new() + ->requireVarsMatchValuesForEnvironment($environment, $vars) + ->run(); - $result = EnvVars::new() - ->requireVarsForEnvironment('production', [ - $varName, - ]) - ->run(); + expect($result) + ->status->toBe(Status::ok()) + ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); + })->with([ENVIRONMENT_STAGING]); - expect($result) - ->status->toBe(Status::failed()) - ->shortSummary->toBe(trans('health-env-vars::translations.not_every_var_has_been_set')) - ->notificationMessage->toBe( - trans('health-env-vars::translations.missing_vars_list_in_environment', [ - 'environment' => currentEnvironment(), - 'list' => implode(', ', $missingList), - ]) - ); -}); + it('returns OK if vars does not match their values in another environment ', function (string $environment) { + // ARRANGE + mockCurrentEnvironment($environment); + $vars = [ + 'VAR1' => 'foo', + ]; + initEnvVars($vars); -test('several specific environment vars can be specified', function () { - $result = EnvVars::new() - ->requireVarsForEnvironment('staging', ['VAR1']) - ->requireVarsForEnvironment('production', ['VAR2']) - ->run(); + // ACT & ASSERT + $result = EnvVars::new() + ->requireVarsMatchValuesForEnvironment(ENVIRONMENT_PRODUCTION, $vars) + ->run(); - expect($result) - ->status->toBe(Status::ok()); + expect($result) + ->status->toBe(Status::ok()) + ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); + })->with([ENVIRONMENT_STAGING]); }); diff --git a/tests/MultipleEnvironmentTest.php b/tests/MultipleEnvironmentTest.php index 723a03f..b4e9794 100644 --- a/tests/MultipleEnvironmentTest.php +++ b/tests/MultipleEnvironmentTest.php @@ -3,82 +3,141 @@ use Encodia\Health\Checks\EnvVars; use Spatie\Health\Enums\Status; -const ENVIRONMENT_STAGING = 'staging'; -const ENVIRONMENT_QA = 'qa'; -const ENVIRONMENT_PRODUCTION = 'production'; - -const ENVIRONMENTS = [ENVIRONMENT_STAGING, ENVIRONMENT_QA, ENVIRONMENT_PRODUCTION]; - afterEach(function () { - unsetEnvVars(['VAR1', 'VAR2', 'VAR3', 'VAR4']); + unsetEnvVars(['VAR1', 'VAR2', 'VAR3', 'VAR4', 'ENV_PROD_VAR1']); }); -describe('when vars need to match values', function () { +describe('when vars need to be set', function () { it( - "returns an error if a var doesn't match the expected value in the current environment", - function (string $currentEnvironment) { - // ARRANGE + 'returns ok if environment specific vars have been set and the environment matches the current one', + function () { + $varName = 'ENV_PROD_VAR1'; + $specificEnvironment = 'production'; - $environments = ENVIRONMENTS; - $variableName = 'VAR1'; - $variableExpectedValue = 'expected value'; - $variableActualValue = 'another value'; - $missingList = trans('health-env-vars::translations.var_not_matching_value', [ - 'name' => $variableName, - 'actual' => $variableActualValue, - 'expected' => $variableExpectedValue, + mockCurrentEnvironment($specificEnvironment); + initEnvVars([ + $varName => 'some_value', ]); + // ensure code is running in the same environment we're testing that a var has been set + expect(currentEnvironment())->toEqual($specificEnvironment); + + $result = EnvVars::new() + ->requireVarsForEnvironment($specificEnvironment, [ + $varName, + ]) + ->run(); + + expect($result) + ->status->toBe(Status::ok()) + ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); + } + ); + + test('environment specific vars are ignored if their environment does not match current one', function () { + $varName = 'ENV_PROD_VAR1'; + $specificEnvironment = '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', [ + $varName, + ]) + ->run(); + + expect($result) + ->status->toBe(Status::ok()) + ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); + }); + + it( + 'returns an error if environment specific vars are required and they are not set in that environment', + function () { + $varName = 'ENV_PROD_VAR1'; + $missingList = [$varName]; + $specificEnvironment = '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'); + + $result = EnvVars::new() + ->requireVarsForEnvironment('production', [ + $varName, + ]) + ->run(); + + expect($result) + ->status->toBe(Status::failed()) + ->shortSummary->toBe(trans('health-env-vars::translations.not_every_var_has_been_set')) + ->notificationMessage->toBe( + trans('health-env-vars::translations.missing_vars_list', [ + 'list' => implode(', ', $missingList), + ]) + ); + } + ); + + it( + "returns an error if vars are required in multiple environments and at least one var hasn't been set in the ". + 'current environment', + function (string $currentEnvironment) { + $environments = ENVIRONMENTS; + $vars = ['VAR1', 'VAR2']; + $missingList = ['VAR1']; expect($currentEnvironment)->toBeIn($environments); mockCurrentEnvironment($currentEnvironment); - // init variable with a value different from the expected one + // init only VAR2 env var initEnvVars([ - $variableName => $variableActualValue, + 'VAR2' => 'somevalue', ]); - expect(env($variableName))->not->toEqual($variableExpectedValue); - // ACT & ASSERT + // ensure VAR1 hasn't been set + expect(env('VAR1'))->toBeNull(); + $result = EnvVars::new() - ->requireVarsMatchValues([ - $variableName => $variableExpectedValue, - ]) + ->requireVarsForEnvironments($environments, $vars) ->run(); expect($result) - ->meta->toEqual([$variableName]) ->status->toBe(Status::failed()) - ->shortSummary->toBe(trans('health-env-vars::translations.vars_not_matching_values')) + ->shortSummary->toBe(trans('health-env-vars::translations.not_every_var_has_been_set')) ->notificationMessage->toBe( - trans('health-env-vars::translations.vars_not_matching_values_list', [ + trans('health-env-vars::translations.missing_vars_list', [ 'environment' => currentEnvironment(), - 'list' => $missingList, + 'list' => implode(', ', $missingList), ]) ); } )->with(ENVIRONMENTS); it( - 'returns OK if all vars match the expected values in the current environment', + 'returns ok if vars are required in multiple environments and they have been set in the current one (one of them)', function (string $currentEnvironment) { - // ARRANGE - $environments = ENVIRONMENTS; - $varsWithValues = [ - 'VAR1' => 'Some value', - 'VAR2' => 42, - 'VAR3' => false, - ]; + $vars = ['VAR3', 'VAR4']; expect($currentEnvironment)->toBeIn($environments); mockCurrentEnvironment($currentEnvironment); - // init variable with a value different from the expected one - initEnvVars($varsWithValues); + // init env vars using the given names and assigning a value + initEnvVars( + collect($vars)->mapWithKeys(function ($name) { + return [$name => 'somevalue']; + })->toArray() + ); - // ACT & ASSERT $result = EnvVars::new() - ->requireVarsMatchValues($varsWithValues) + ->requireVarsForEnvironments($environments, $vars) ->run(); expect($result) @@ -86,122 +145,58 @@ function (string $currentEnvironment) { ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); } )->with(ENVIRONMENTS); -}); - -describe('when vars need to match values for a single environment', function () { - it("fails if at least one var doesn't match its value for the current environment ", function (string $environment) { - // ARRANGE - mockCurrentEnvironment($environment); - - $variableName = 'VAR2'; - $variableActualValue = 'different'; - $variableExpectedValue = 'bar'; - $vars = [ - 'VAR1' => 'foo', - $variableName => $variableActualValue, - ]; - initEnvVars($vars); - - // ACT & ASSERT - $missingList = trans('health-env-vars::translations.var_not_matching_value', [ - 'name' => $variableName, - 'actual' => $variableActualValue, - 'expected' => $variableExpectedValue, - ]); + test('several specific environment vars can be specified', function () { $result = EnvVars::new() - ->requireVarsMatchValuesForEnvironment($environment, [ - 'VAR1' => 'foo', - 'VAR2' => $variableExpectedValue, - ]) + ->requireVarsForEnvironment('staging', ['VAR1']) + ->requireVarsForEnvironment('production', ['VAR2']) ->run(); expect($result) - ->status->toBe(Status::failed()) - ->shortSummary->toBe(trans('health-env-vars::translations.vars_not_matching_values')) - ->notificationMessage->toBe( - trans('health-env-vars::translations.vars_not_matching_values_list', [ - 'environment' => currentEnvironment(), - 'list' => $missingList, - ]) - ); - })->with([ENVIRONMENT_STAGING]); - - it('returns OK if vars match their values for the current environment ', function (string $environment) { - // ARRANGE - mockCurrentEnvironment($environment); - $vars = [ - 'VAR1' => 'foo', - ]; - initEnvVars($vars); - - // ACT & ASSERT - $result = EnvVars::new() - ->requireVarsMatchValuesForEnvironment($environment, $vars) - ->run(); - - expect($result) - ->status->toBe(Status::ok()) - ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); - })->with([ENVIRONMENT_STAGING]); - - it('returns OK if vars does not match their values in another environment ', function (string $environment) { - // ARRANGE - mockCurrentEnvironment($environment); - $vars = [ - 'VAR1' => 'foo', - ]; - initEnvVars($vars); - - // ACT & ASSERT - $result = EnvVars::new() - ->requireVarsMatchValuesForEnvironment(ENVIRONMENT_PRODUCTION, $vars) - ->run(); - - expect($result) - ->status->toBe(Status::ok()) - ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); - })->with([ENVIRONMENT_STAGING]); + ->status->toBe(Status::ok()); + }); }); describe('when vars need to match values for multiple environments', function () { - it("fails if at least one var doesn't match its value for the current environment ", function (string $environment) { - // ARRANGE - mockCurrentEnvironment($environment); - - $variableName = 'VAR2'; - $variableActualValue = 'different'; - $variableExpectedValue = 'bar'; - $vars = [ - 'VAR1' => 'foo', - $variableName => $variableActualValue, - ]; - initEnvVars($vars); - - // ACT & ASSERT - $missingList = trans('health-env-vars::translations.var_not_matching_value', [ - 'name' => $variableName, - 'actual' => $variableActualValue, - 'expected' => $variableExpectedValue, - ]); + it( + "fails if at least one var doesn't match its value for the current environment ", + function (string $environment) { + // ARRANGE + mockCurrentEnvironment($environment); - $result = EnvVars::new() - ->requireVarsMatchValuesForEnvironments(ENVIRONMENTS, [ + $variableName = 'VAR2'; + $variableActualValue = 'different'; + $variableExpectedValue = 'bar'; + $vars = [ 'VAR1' => 'foo', - 'VAR2' => $variableExpectedValue, - ]) - ->run(); + $variableName => $variableActualValue, + ]; + initEnvVars($vars); - expect($result) - ->status->toBe(Status::failed()) - ->shortSummary->toBe(trans('health-env-vars::translations.vars_not_matching_values')) - ->notificationMessage->toBe( - trans('health-env-vars::translations.vars_not_matching_values_list', [ - 'environment' => currentEnvironment(), - 'list' => $missingList, + // ACT & ASSERT + $missingList = trans('health-env-vars::translations.var_not_matching_value', [ + 'name' => $variableName, + 'actual' => $variableActualValue, + 'expected' => $variableExpectedValue, + ]); + + $result = EnvVars::new() + ->requireVarsMatchValuesForEnvironments(ENVIRONMENTS, [ + 'VAR1' => 'foo', + 'VAR2' => $variableExpectedValue, ]) - ); - })->with([ENVIRONMENT_STAGING]); + ->run(); + + expect($result) + ->status->toBe(Status::failed()) + ->shortSummary->toBe(trans('health-env-vars::translations.vars_not_matching_values')) + ->notificationMessage->toBe( + trans('health-env-vars::translations.vars_not_matching_values_list', [ + 'list' => $missingList, + ]) + ); + } + )->with([ENVIRONMENT_STAGING]); it('returns OK if vars match their values for the current environment ', function (string $environment) { // ARRANGE @@ -247,64 +242,3 @@ function (string $currentEnvironment) { ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); })->with([ENVIRONMENT_STAGING]); }); - -it( - "returns an error if vars are required in multiple environments and at least one var hasn't been set in the ". - 'current environment', - function (string $currentEnvironment) { - $environments = ENVIRONMENTS; - $vars = ['VAR1', 'VAR2']; - $missingList = ['VAR1']; - - expect($currentEnvironment)->toBeIn($environments); - mockCurrentEnvironment($currentEnvironment); - - // init only VAR2 env var - initEnvVars([ - 'VAR2' => 'somevalue', - ]); - - // ensure VAR1 hasn't been set - expect(env('VAR1'))->toBeNull(); - - $result = EnvVars::new() - ->requireVarsForEnvironments($environments, $vars) - ->run(); - - expect($result) - ->status->toBe(Status::failed()) - ->shortSummary->toBe(trans('health-env-vars::translations.not_every_var_has_been_set')) - ->notificationMessage->toBe( - trans('health-env-vars::translations.missing_vars_list_in_environment', [ - 'environment' => currentEnvironment(), - 'list' => implode(', ', $missingList), - ]) - ); - } -)->with(ENVIRONMENTS); - -it( - 'returns ok if vars are required in multiple environments and they have been set in the current one (one of them)', - function (string $currentEnvironment) { - $environments = ENVIRONMENTS; - $vars = ['VAR3', 'VAR4']; - - expect($currentEnvironment)->toBeIn($environments); - mockCurrentEnvironment($currentEnvironment); - - // init env vars using the given names and assigning a value - initEnvVars( - collect($vars)->mapWithKeys(function ($name) { - return [$name => 'somevalue']; - })->toArray() - ); - - $result = EnvVars::new() - ->requireVarsForEnvironments($environments, $vars) - ->run(); - - expect($result) - ->status->toBe(Status::ok()) - ->shortSummary->toBe(trans('health-env-vars::translations.every_var_has_been_set')); - } -)->with(ENVIRONMENTS); diff --git a/tests/Pest.php b/tests/Pest.php index 00d3ef2..513b658 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -5,6 +5,12 @@ uses(TestCase::class)->in(__DIR__); +const ENVIRONMENT_STAGING = 'staging'; +const ENVIRONMENT_QA = 'qa'; +const ENVIRONMENT_PRODUCTION = 'production'; + +const ENVIRONMENTS = [ENVIRONMENT_STAGING, ENVIRONMENT_QA, ENVIRONMENT_PRODUCTION]; + /** * Initialize .env variables with the given names and values *