{"id":20180608,"url":"https://github.com/ergebnis/environment-variables","last_synced_at":"2025-04-10T05:05:36.744Z","repository":{"id":37859182,"uuid":"242388417","full_name":"ergebnis/environment-variables","owner":"ergebnis","description":"🌳 Provides a composer package with an abstraction of environment variables.","archived":false,"fork":false,"pushed_at":"2024-04-24T13:46:24.000Z","size":3025,"stargazers_count":38,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-05-01T11:41:17.717Z","etag":null,"topics":["environment","variables"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ergebnis.png","metadata":{"funding":{"github":["ergebnis","localheinz"]},"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-22T18:09:29.000Z","updated_at":"2024-05-02T14:27:32.048Z","dependencies_parsed_at":"2024-04-15T14:46:43.869Z","dependency_job_id":"6659430c-7d01-4ccd-b64f-797b02370b43","html_url":"https://github.com/ergebnis/environment-variables","commit_stats":{"total_commits":681,"total_committers":4,"mean_commits":170.25,"dds":"0.25844346549192365","last_synced_commit":"c4fd9872b6ee680d6af06afcb72bc63540a37727"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":"ergebnis/php-package-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ergebnis%2Fenvironment-variables","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ergebnis%2Fenvironment-variables/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ergebnis%2Fenvironment-variables/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ergebnis%2Fenvironment-variables/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ergebnis","download_url":"https://codeload.github.com/ergebnis/environment-variables/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248161280,"owners_count":21057554,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["environment","variables"],"created_at":"2024-11-14T02:31:59.896Z","updated_at":"2025-04-10T05:05:36.720Z","avatar_url":"https://github.com/ergebnis.png","language":"PHP","funding_links":["https://github.com/sponsors/ergebnis","https://github.com/sponsors/localheinz"],"categories":[],"sub_categories":[],"readme":"# environment-variables\n\n[![Integrate](https://github.com/ergebnis/environment-variables/workflows/Integrate/badge.svg)](https://github.com/ergebnis/environment-variables/actions)\n[![Merge](https://github.com/ergebnis/environment-variables/workflows/Merge/badge.svg)](https://github.com/ergebnis/environment-variables/actions)\n[![Release](https://github.com/ergebnis/environment-variables/workflows/Release/badge.svg)](https://github.com/ergebnis/environment-variables/actions)\n[![Renew](https://github.com/ergebnis/environment-variables/workflows/Renew/badge.svg)](https://github.com/ergebnis/environment-variables/actions)\n\n[![Code Coverage](https://codecov.io/gh/ergebnis/environment-variables/branch/main/graph/badge.svg)](https://codecov.io/gh/ergebnis/environment-variables)\n\n[![Latest Stable Version](https://poser.pugx.org/ergebnis/environment-variables/v/stable)](https://packagist.org/packages/ergebnis/environment-variables)\n[![Total Downloads](https://poser.pugx.org/ergebnis/environment-variables/downloads)](https://packagist.org/packages/ergebnis/environment-variables)\n[![Monthly Downloads](http://poser.pugx.org/ergebnis/environment-variables/d/monthly)](https://packagist.org/packages/ergebnis/environment-variables)\n\nThis project provides a [`composer`](https://getcomposer.org) package with an abstraction of environment variables.\n\n## Installation\n\nRun\n\n```sh\ncomposer require ergebnis/environment-variables\n```\n\n## Usage\n\nThis package provides the interface [`Ergebnis\\Environment\\Variables`](src/Variables.php) along with the following production implementations:\n\n- [`Ergebnis\\Environment\\SystemVariables`](#ergebnisenvironmentsystemvariables)\n\nThis package also provides the following test implementations:\n\n- [`Ergebnis\\Environment\\FakeVariables`](#ergebnisenvironmentfakevariables)\n- [`Ergebnis\\Environment\\ReadOnlyVariables`](#ergebnisenvironmentreadonlyvariables)\n- [`Ergebnis\\Environment\\TestVariables`](#ergebnisenvironmenttestvariables)\n\n### Production Implementation\n\n#### `Ergebnis\\Environment\\SystemVariables`\n\nIf you want to read, set, and unset environment variables in an object-oriented way in a production environment, you can use [`Ergebnis\\Environment\\SystemVariables`](src/SystemVariables.php):\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nuse Ergebnis\\Environment;\n\nfinal class BuildEnvironment\n{\n    private Environment\\Variables $environmentVariables;\n\n    public function __construct(Environment\\Variables $environmentVariables)\n    {\n        $this-\u003eenvironmentVariables = $environmentVariables;\n    }\n\n    public function isGitHubActions(): bool\n    {\n        return $this-\u003eenvironmentVariables-\u003ehas('GITHUB_ACTIONS')\n            \u0026\u0026 'true' === $this-\u003eenvironmentVariables-\u003eget('GITHUB_ACTIONS');\n    }\n\n    public function isTravisCi(): bool\n    {\n        return $this-\u003eenvironmentVariables-\u003ehas('TRAVIS')\n            \u0026\u0026 'true' === $this-\u003eenvironmentVariables-\u003eget('TRAVIS');\n    }\n}\n```\n\n### Test Implementation\n\n#### `Ergebnis\\Environment\\FakeVariables`\n\nIf 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`](src/FakeVariables.php) as a test-double:\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nuse Ergebnis\\Environment;\nuse PHPUnit\\Framework;\n\nfinal class BuildEnvironmentTest extends Framework\\TestCase\n{\n    public function testIsGitHubActionsReturnsFalseWhenNoneOfTheExpectedEnvironmentVariablesAreAvailable(): void\n    {\n        $environmentVariables = Environment\\FakeVariables::empty();\n\n        $buildEnvironment = new BuildEnvironment($environmentVariables);\n\n        self::assertFalse($buildEnvironment-\u003eisGitHubActions());\n    }\n\n    public function testIsGitHubActionsReturnsFalseWhenValueOfGitHubActionsEnvironmentVariableIsNotTrue(): void\n    {\n        $environmentVariables = Environment\\FakeVariables::fromArray([\n            'GITHUB_ACTIONS' =\u003e 'false',\n        ]);\n\n        $buildEnvironment = new BuildEnvironment($environmentVariables);\n\n        self::assertFalse($buildEnvironment-\u003eisGitHubActions());\n    }\n\n    public function testIsGitHubActionsReturnsTrueWhenValueOfGitHubActionsEnvironmentVariableIsTrue(): void\n    {\n        $environmentVariables = Environment\\FakeVariables::fromArray([\n            'GITHUB_ACTIONS' =\u003e 'true',\n        ]);\n\n        $buildEnvironment = new BuildEnvironment($environmentVariables);\n\n        self::assertTrue($buildEnvironment-\u003eisGitHubActions());\n    }\n}\n```\n#### `Ergebnis\\Environment\\ReadOnlyVariables`\n\nIf 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`](src/ReadOnlyVariables.php) as a test-double:\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nuse Ergebnis\\Environment;\nuse PHPUnit\\Framework;\n\nfinal class BuildEnvironmentTest extends Framework\\TestCase\n{\n    public function testIsGitHubActionsReturnsFalseWhenNoneOfTheExpectedEnvironmentVariablesAreAvailable(): void\n    {\n        $environmentVariables = Environment\\ReadOnlyVariables::empty();\n\n        $buildEnvironment = new BuildEnvironment($environmentVariables);\n\n        self::assertFalse($buildEnvironment-\u003eisGitHubActions());\n    }\n\n    public function testIsGitHubActionsReturnsFalseWhenValueOfGitHubActionsEnvironmentVariableIsNotTrue(): void\n    {\n        $environmentVariables = Environment\\ReadOnlyVariables::fromArray([\n            'GITHUB_ACTIONS' =\u003e 'false',\n        ]);\n\n        $buildEnvironment = new BuildEnvironment($environmentVariables);\n\n        self::assertFalse($buildEnvironment-\u003eisGitHubActions());\n    }\n\n    public function testIsGitHubActionsReturnsTrueWhenValueOfGitHubActionsEnvironmentVariableIsTrue(): void\n    {\n        $environmentVariables = Environment\\ReadOnlyVariables::fromArray([\n            'GITHUB_ACTIONS' =\u003e 'true',\n        ]);\n\n        $buildEnvironment = new BuildEnvironment($environmentVariables);\n\n        self::assertTrue($buildEnvironment-\u003eisGitHubActions());\n    }\n}\n```\n\n:bulb: The `ReadOnlyVariables` implementation will throw a [`ShouldNotBeUsed`](src/Exception/ShouldNotBeUsed.php) exception when the system under tests uses any of the following methods:\n\n- `set()`\n- `unset()`\n\n#### `Ergebnis\\Environment\\TestVariables`\n\nIf your tests depend on environment variables, you have the following challenges:\n\n- 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\n- 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\n\nTo solve this problem, you can add the [`@backupGlobals`](https://phpunit.readthedocs.io/en/9.0/annotations.html#backupglobals) annotation to your test cases when using [`phpunit/phpunit`](https://github.com/sebastianbergmann/phpunit), or use [`Ergebnis\\Environment\\TestVariables`](src/TestVariables.php):\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nuse Ergebnis\\Environment;\nuse PHPUnit\\Framework;\n\nfinal class FooTest extends Framework\\TestCase\n{\n    private static Environment\\TestVariables $environmentVariables;\n\n    protected function setUp() : void\n    {\n        // will back up environment variables FOO, BAR, and BAZ\n        self::$environmentVariables = Environment\\TestVariables::backup(\n            'FOO',\n            'BAR',\n            'BAZ'\n        );\n    }\n\n    protected function tearDown() : void\n    {\n        // will restore backed-up environment variables FOO, BAR, and BAZ to their initial state\n        self::$environmentVariables-\u003erestore();\n    }\n\n    public function testSomethingThatDependsOnEnvironmentVariableFooToBeSet(): void\n    {\n        self::$environmentVariables-\u003eset(\n            'FOO',\n            '9000'\n        );\n\n        // ...\n    }\n\n    public function testSomethingThatDependsOnEnvironmentVariableFooToBeUnset(): void\n    {\n        self::$environmentVariables-\u003eunset('FOO');\n\n        // ...\n    }\n\n    public function testSomethingThatDependsOnEnvironmentVariableQuxToBeSet(): void\n    {\n        // will throw exception because the environment variable QUX has not been backed up\n        self::$environmentVariables-\u003eset(\n            'QUX',\n            '9000'\n        );\n\n        // ...\n    }\n\n    public function testSomethingThatDependsOnEnvironmentVariableQuxToBeUnset(): void\n    {\n        // will throw exception because the environment variable QUX has not been backed up\n        self::$environmentVariables-\u003eunset('QUX');\n    }\n}\n```\n\n## Changelog\n\nThe maintainers of this project record notable changes to this project in a [changelog](CHANGELOG.md).\n\n## Contributing\n\nThe maintainers of this project suggest following the [contribution guide](.github/CONTRIBUTING.md).\n\n## Code of Conduct\n\nThe maintainers of this project ask contributors to follow the [code of conduct](https://github.com/ergebnis/.github/blob/main/CODE_OF_CONDUCT.md).\n\n## General Support Policy\n\nThe maintainers of this project provide limited support.\n\nYou can support the maintenance of this project by [sponsoring @localheinz](https://github.com/sponsors/localheinz) or [requesting an invoice for services related to this project](mailto:am@localheinz.com?subject=ergebnis/environment-variables:%20Requesting%20invoice%20for%20services).\n\n## PHP Version Support Policy\n\nThis project supports PHP versions with [active and security support](https://www.php.net/supported-versions.php).\n\nThe maintainers of this project add support for a PHP version following its initial release and drop support for a PHP version when it has reached the end of security support.\n\n## Security Policy\n\nThis project has a [security policy](.github/SECURITY.md).\n\n## License\n\nThis project uses the [MIT license](LICENSE.md).\n\n## Social\n\nFollow [@localheinz](https://twitter.com/intent/follow?screen_name=localheinz) and [@ergebnis](https://twitter.com/intent/follow?screen_name=ergebnis) on Twitter.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fergebnis%2Fenvironment-variables","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fergebnis%2Fenvironment-variables","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fergebnis%2Fenvironment-variables/lists"}