All Projects â†’ ergebnis â†’ environment-variables

ergebnis / environment-variables

Licence: MIT license
🌳 Provides an abstraction of environment variables.

Programming Languages

PHP
23972 projects - #3 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to environment-variables

environment
🌳 Environment variable configuration for Node.js made easy.
Stars: ✭ 12 (-63.64%)
Mutual labels:  environment, variables
envy
Use envy to manage environment variables with your OS keychain
Stars: ✭ 23 (-30.3%)
Mutual labels:  environment, variables
ngx-env
Easily inject environment variables into your Angular applications
Stars: ✭ 73 (+121.21%)
Mutual labels:  environment, variables
Cross Env
🔀 Cross platform setting of environment scripts
Stars: ✭ 5,623 (+16939.39%)
Mutual labels:  environment, variables
environmental-exposure-ontology
Modular environmental exposures ontology
Stars: ✭ 20 (-39.39%)
Mutual labels:  environment
py better exchook
nice Python exception hook replacement
Stars: ✭ 35 (+6.06%)
Mutual labels:  variables
DBEnvironmentConfiguration
Easily switch between iOS development environments/ configurations
Stars: ✭ 18 (-45.45%)
Mutual labels:  environment
sensor
Sensor by Metriful | Indoor environment monitoring | Documentation and code samples
Stars: ✭ 93 (+181.82%)
Mutual labels:  environment
micromamba-docker
Rapid builds of small Conda-based containers using micromamba.
Stars: ✭ 97 (+193.94%)
Mutual labels:  environment
pandoc-latex-environment
Pandoc filter for adding LaTeX environement on specific div
Stars: ✭ 27 (-18.18%)
Mutual labels:  environment
environmental-footprint-data
Boavizta.org Data repository
Stars: ✭ 60 (+81.82%)
Mutual labels:  environment
dart environment config
Environment specific config generator for Dart and Flutter applications during CI/CD builds
Stars: ✭ 87 (+163.64%)
Mutual labels:  environment
awesome-scripts
Set of scripts that can do awesome stuff for developers
Stars: ✭ 16 (-51.52%)
Mutual labels:  environment
rune
tool to query for tokens and passwords for use as environment variables
Stars: ✭ 13 (-60.61%)
Mutual labels:  environment
salak.rs
A multi layered configuration loader and zero-boilerplate configuration parser.
Stars: ✭ 27 (-18.18%)
Mutual labels:  environment
termux-docker
Termux environment packaged as Docker image.
Stars: ✭ 284 (+760.61%)
Mutual labels:  environment
react-native-envs-poc
Manage staging & production environments in React Native
Stars: ✭ 32 (-3.03%)
Mutual labels:  environment
bleeding-rez
Rez - Reproducible software environments for Windows, Linux and MacOS
Stars: ✭ 57 (+72.73%)
Mutual labels:  environment
ScoreboardStats
Bukkit plugin for customizing the sidebar of the scoreboard feature from minecraft
Stars: ✭ 29 (-12.12%)
Mutual labels:  variables
hass-variables
Home Assistant variables component
Stars: ✭ 35 (+6.06%)
Mutual labels:  variables

environment-variables

Integrate Prune Release Renew

Code Coverage Type Coverage

Latest Stable Version Total Downloads

Provides an abstraction of environment variables.

Installation

Run

$ composer require ergebnis/environment-variables

Usage

This package provides the interface Ergebnis\Environment\Variables along with the following production implementations:

This package also provides the following test implementations:

Production Implementation

Ergebnis\Environment\SystemVariables

If you want to read, set, and unset environment variables in an object-oriented way in a production environment, you can use Ergebnis\Environment\SystemVariables:

<?php

declare(strict_types=1);

use Ergebnis\Environment;

final class BuildEnvironment
{
    private $environmentVariables;

    public function __construct(Environment\Variables $environmentVariables)
    {
        $this->environmentVariables = $environmentVariables;
    }

    public function isGitHubActions(): bool
    {
        return $this->environmentVariables->has('GITHUB_ACTIONS')
            && 'true' === $this->environmentVariables->get('GITHUB_ACTIONS');
    }

    public function isTravisCi(): bool
    {
        return $this->environmentVariables->has('TRAVIS')
            && 'true' === $this->environmentVariables->get('TRAVIS');
    }
}

Test Implementation

Ergebnis\Environment\FakeVariables

If you want to read, set, and unset environment variables in an object-oriented way in a test environment, but do not actually want to modify system environment variables, you can use Ergebnis\Environment\FakeVariables as a test-double:

<?php

declare(strict_types=1);

use Ergebnis\Environment;
use PHPUnit\Framework;

final class BuildEnvironmentTest extends Framework\TestCase
{
    public function testIsGitHubActionsReturnsFalseWhenNoneOfTheExpectedEnvironmentVariablesAreAvailable(): void
    {
        $environmentVariables = Environment\FakeVariables::empty();

        $buildEnvironment = new BuildEnvironment($environmentVariables);

        self::assertFalse($buildEnvironment->isGitHubActions());
    }

    public function testIsGitHubActionsReturnsFalseWhenValueOfGitHubActionsEnvironmentVariableIsNotTrue(): void
    {
        $environmentVariables = Environment\FakeVariables::fromArray([
            'GITHUB_ACTIONS' => 'false',
        ]);

        $buildEnvironment = new BuildEnvironment($environmentVariables);

        self::assertFalse($buildEnvironment->isGitHubActions());
    }

    public function testIsGitHubActionsReturnsTrueWhenValueOfGitHubActionsEnvironmentVariableIsTrue(): void
    {
        $environmentVariables = Environment\FakeVariables::fromArray([
            'GITHUB_ACTIONS' => 'true',
        ]);

        $buildEnvironment = new BuildEnvironment($environmentVariables);

        self::assertTrue($buildEnvironment->isGitHubActions());
    }
}

Ergebnis\Environment\ReadOnlyVariables

If you want to read environment variables in an object-oriented way in a test environment, but neither actually want to modify system environment variables, nor allow modification by the system under test, you can use Ergebnis\Environment\ReadOnlyVariables as a test-double:

<?php

declare(strict_types=1);

use Ergebnis\Environment;
use PHPUnit\Framework;

final class BuildEnvironmentTest extends Framework\TestCase
{
    public function testIsGitHubActionsReturnsFalseWhenNoneOfTheExpectedEnvironmentVariablesAreAvailable(): void
    {
        $environmentVariables = Environment\ReadOnlyVariables::empty();

        $buildEnvironment = new BuildEnvironment($environmentVariables);

        self::assertFalse($buildEnvironment->isGitHubActions());
    }

    public function testIsGitHubActionsReturnsFalseWhenValueOfGitHubActionsEnvironmentVariableIsNotTrue(): void
    {
        $environmentVariables = Environment\ReadOnlyVariables::fromArray([
            'GITHUB_ACTIONS' => 'false',
        ]);

        $buildEnvironment = new BuildEnvironment($environmentVariables);

        self::assertFalse($buildEnvironment->isGitHubActions());
    }

    public function testIsGitHubActionsReturnsTrueWhenValueOfGitHubActionsEnvironmentVariableIsTrue(): void
    {
        $environmentVariables = Environment\ReadOnlyVariables::fromArray([
            'GITHUB_ACTIONS' => 'true',
        ]);

        $buildEnvironment = new BuildEnvironment($environmentVariables);

        self::assertTrue($buildEnvironment->isGitHubActions());
    }
}

💡 The ReadOnlyVariables implementation will throw a ShouldNotBeUsed exception when the system under tests uses any of the following methods:

  • set()
  • unset()

Ergebnis\Environment\TestVariables

If your tests depend on environment variables, you have the following challenges:

  • when you modify environment variables in a test, you want to restore environment variables that have existed before the test run to their original values
  • when you modify environment variables in a test that has not been backed up before, and forget to restore it, it might affect other tests

To solve this problem, you can add the @backupGlobals annotation to your test cases when using phpunit/phpunit, or use Ergebnis\Environment\TestVariables:

<?php

declare(strict_types=1);

use Ergebnis\Environment;
use PHPUnit\Framework;

final class FooTest extends Framework\TestCase
{
    /**
     * @var Environment\TestVariables
     */
    private static $environmentVariables;

    protected function setUp() : void
    {
        // will back up environment variables FOO, BAR, and BAZ
        self::$environmentVariables = Environment\TestVariables::backup(
            'FOO',
            'BAR',
            'BAZ'
        );
    }

    protected function tearDown() : void
    {
        // will restore backed-up environment variables FOO, BAR, and BAZ to their initial state
        self::$environmentVariables->restore();
    }

    public function testSomethingThatDependsOnEnvironmentVariableFooToBeSet(): void
    {
        self::$environmentVariables->set(
            'FOO',
            '9000'
        );

        // ...
    }

    public function testSomethingThatDependsOnEnvironmentVariableFooToBeUnset(): void
    {
        self::$environmentVariables->unset('FOO');

        // ...
    }

    public function testSomethingThatDependsOnEnvironmentVariableQuxToBeSet(): void
    {
        // will throw exception because the environment variable QUX has not been backed up
        self::$environmentVariables->set(
            'QUX',
            '9000'
        );

        // ...
    }

    public function testSomethingThatDependsOnEnvironmentVariableQuxToBeUnset(): void
    {
        // will throw exception because the environment variable QUX has not been backed up
        self::$environmentVariables->unset('QUX');
    }
}

Changelog

Please have a look at CHANGELOG.md.

Contributing

Please have a look at CONTRIBUTING.md.

Code of Conduct

Please have a look at CODE_OF_CONDUCT.md.

License

This package is licensed using the MIT License.

Please have a look at LICENSE.md.

Curious what I am building?

📬 Subscribe to my list, and I will occasionally send you an email to let you know what I am working on.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].