assertwell/phpunit-global-state

Enable global and environment variables to be set in bulk

Opened this issue · 0 comments

It would be helpful if there were setEnvironmentVariables() and setGlobalVariables() methods that would accept an array of environment/global variables to set at once.

Current:

$this->setEnvironmentVariable('FIRST_VAR', 'some-value');
$this->setEnvironmentVariable('SECOND_VAR', 'some-other-value');

Proposed:

$this->setEnvironmentVariables([
  'FIRST_VAR'  => 'some-value',
  'SECOND_VAR' => 'some-other-value',
]);

A good use-case would be in data providers:

/**
 * @test
 * @dataProvider provideEnvironmentVariableCombinations
 */
public function it_checks_the_environment_to_ensure_all_values_are_present(array $env): void
{
  $this->setEnvironmentVariables($env);

  // ...
}

public function provideEnvironmentVariableCombinations(): array
{
  return [
    'Missing all vars'   => [['FIRST_VAR' => null, 'SECOND_VAR' => null]],
    'Missing first var'  => [['FIRST_VAR' => null, 'SECOND_VAR' => 'some-other-value']],
    'Missing second var' => [['FIRST_VAR' => 'some-value', 'SECOND_VAR' => null]],
  ];
}

Under the hood, these methods could just loop through the array, passing the key/value combinations to the singular forms of the methods.