PHP code example of ergebnis / environment-variables
1. Go to this page and download the library: Download ergebnis/environment-variables library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ergebnis / environment-variables example snippets
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');
}
}
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());
}
}
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());
}
}
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');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.