ergebnis / environment-variables
提供环境变量的抽象。
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.39.0
- ergebnis/data-provider: ^3.1.0
- ergebnis/license: ^2.3.0
- ergebnis/php-cs-fixer-config: ^6.12.0
- ergebnis/phpunit-slow-test-detector: ^2.4.0
- infection/infection: ~0.27.8
- phpunit/phpunit: ^10.4.2
- psalm/plugin-phpunit: ~0.18.4
- rector/rector: ~0.18.11
- vimeo/psalm: ^5.16.0
This package is auto-updated.
Last update: 2024-09-10 13:21:29 UTC
README
本项目提供了一个带有环境变量抽象的 composer
包。
安装
运行
composer require ergebnis/environment-variables
使用
此包提供了接口 Ergebnis\Environment\Variables
,以及以下生产实现:
此包还提供了以下测试实现:
Ergebnis\Environment\FakeVariables
Ergebnis\Environment\ReadOnlyVariables
Ergebnis\Environment\TestVariables
生产实现
Ergebnis\Environment\SystemVariables
如果您想在生产环境中以面向对象的方式读取、设置和取消设置环境变量,可以使用 Ergebnis\Environment\SystemVariables
<?php declare(strict_types=1); use Ergebnis\Environment; final class BuildEnvironment { private Environment\Variables $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'); } }
测试实现
Ergebnis\Environment\FakeVariables
如果您想在测试环境中以面向对象的方式读取、设置和取消设置环境变量,但实际上不希望修改系统环境变量,可以使用 Ergebnis\Environment\FakeVariables
作为测试替身
<?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
如果您想在测试环境中以面向对象的方式读取环境变量,但实际上不希望修改系统环境变量,也不允许测试中的系统进行修改,可以使用 Ergebnis\Environment\ReadOnlyVariables
作为测试替身
<?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()); } }
💡 当系统测试使用以下方法时,ReadOnlyVariables
实现将抛出 ShouldNotBeUsed
异常:
set()
unset()
Ergebnis\Environment\TestVariables
如果您的测试依赖于环境变量,您将面临以下挑战:
- 当您在测试中修改环境变量时,您希望将测试运行之前存在的环境变量恢复到它们的原始值
- 当您在未备份之前修改测试中的环境变量,并且忘记恢复时,可能会影响其他测试
为了解决这个问题,您可以在使用 phpunit/phpunit
时将 @backupGlobals
注解添加到您的测试用例中,或者使用 Ergebnis\Environment\TestVariables
<?php declare(strict_types=1); use Ergebnis\Environment; use PHPUnit\Framework; final class FooTest extends Framework\TestCase { private static Environment\TestVariables $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'); } }
变更日志
此项目的维护者在 变更日志 中记录了此项目的显著更改。
贡献
此项目的维护者建议遵循 贡献指南。
行为准则
此项目的维护者要求贡献者遵守 行为准则。
一般支持策略
本项目维护者提供有限支持。
您可以通过赞助 @localheinz 或 请求与本项目相关的服务发票来支持本项目的维护。
PHP 版本支持策略
本项目支持具有活跃和安全支持的 PHP 版本。
本项目维护者在其初始发布后添加对 PHP 版本的支持,并在其达到安全支持结束时停止对该 PHP 版本的支持。
安全策略
本项目有一个安全策略。
许可证
本项目使用MIT 许可证。
社交
关注 Twitter 上的@localheinz 和 @ergebnis。