Skip to content

Commit

Permalink
refactor: tests; remove messages for a certain environment
Browse files Browse the repository at this point in the history
  • Loading branch information
eleftrik committed Jun 8, 2024
1 parent a5d1b31 commit 4080d55
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 329 deletions.
3 changes: 1 addition & 2 deletions resources/lang/en/translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
3 changes: 1 addition & 2 deletions resources/lang/it/translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
5 changes: 2 additions & 3 deletions src/Health/Checks/EnvVars.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(','),
])
Expand Down Expand Up @@ -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('; '),
])
);
Expand Down
299 changes: 184 additions & 115 deletions tests/EnvVarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
Loading

0 comments on commit 4080d55

Please sign in to comment.