Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced the fromOrDefault method to allow retrieving environment variables with a default value. #3

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ To create and work with environment variables, use the `from` method to get an i
EnvironmentVariable::from(name: 'MY_VAR');
```

To retrieve an environment variable with the option of providing a default value in case the variable does not exist,
use the `fromOrDefault` method.

If the environment variable is not found, the method will return the provided default value instead of throwing an
exception.

```php
EnvironmentVariable::fromOrDefault(name: 'MY_VAR', defaultValueIfNotFound: 'default_value');
```

### Conversions

Once you have an instance of the environment variable, you can convert its value into various types.
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
failOnRisky="true"
failOnWarning="true"
cacheDirectory=".phpunit.cache"
executionOrder="random"
beStrictAboutOutputDuringTests="true">

<source>
Expand Down
9 changes: 9 additions & 0 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ interface Environment
*/
public static function from(string $name): Environment;

/**
* Retrieves an instance of the environment variable or uses a default value if not found.
*
* @param string $name The name of the environment variable.
* @param string|null $defaultValueIfNotFound The default value to use if the environment variable is not found.
* @return EnvironmentVariable The environment variable instance, either with the found value or the default.
*/
public static function fromOrDefault(string $name, string $defaultValueIfNotFound = null): EnvironmentVariable;

/**
* Checks if the environment variable has a value. Values like `false`, `0`, and `-1` are valid and non-empty.
*
Expand Down
9 changes: 9 additions & 0 deletions src/EnvironmentVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public static function from(string $name): EnvironmentVariable
: new EnvironmentVariable(value: $environmentVariable, variable: $name);
}

public static function fromOrDefault(string $name, string $defaultValueIfNotFound = null): EnvironmentVariable
{
$environmentVariable = getenv($name);

return $environmentVariable === false
? new EnvironmentVariable(value: (string)$defaultValueIfNotFound, variable: $name)
: new EnvironmentVariable(value: $environmentVariable, variable: $name);
}

public function hasValue(): bool
{
return match (strtolower(trim($this->value))) {
Expand Down
43 changes: 40 additions & 3 deletions tests/EnvironmentVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,43 @@ public function testConvertToBoolean(mixed $value, string $variable, bool $expec
self::assertEquals($expected, $actual);
}

public function testFromOrDefaultWithDefaultValue(): void
{
/** @Given that the environment variable 'NON_EXISTENT_MY_VAR' does not exist */
$variable = 'NON_EXISTENT_MY_VAR';

/** @When I try to get the value of the environment variable with a default value */
$actual = EnvironmentVariable::fromOrDefault(name: $variable, defaultValueIfNotFound: '0');

/** @Then the result should match the default value */
self::assertEquals(0, $actual->toInteger());
}

public function testFromOrDefaultWithExistingVariable(): void
{
/** @Given that the environment variable 'MY_VAR' exists with the value 'existing_value' */
putenv(sprintf('%s=%s', 'MY_VAR', 'existing_value'));

/** @When I try to get the value of the environment variable */
$actual = EnvironmentVariable::fromOrDefault(name: 'MY_VAR', defaultValueIfNotFound: 'default_value');

/** @Then the result should match the existing value */
self::assertEquals('existing_value', $actual->toString());
}

public function testFromOrDefaultWhenVariableIsMissingAndNoDefault(): void
{
/** @Given that the environment variable 'NON_EXISTENT_VAR' does not exist */
$variable = 'NON_EXISTENT_VAR';

/** @When I try to get the value of the missing environment variable without a default value */
$actual = EnvironmentVariable::fromOrDefault(name: $variable);

/** @Then the result should be no value */
self::assertEmpty($actual->toString());
self::assertFalse($actual->hasValue());
}

#[DataProvider('hasValueDataProvider')]
public function testHasValue(mixed $value, string $variable): void
{
Expand Down Expand Up @@ -225,15 +262,15 @@ public static function hasValueDataProvider(): array
public static function hasNoValueDataProvider(): array
{
return [
'Null value' => [
'Null value' => [
'value' => null,
'variable' => 'NULL_VALUE'
],
'Empty string' => [
'Empty string' => [
'value' => '',
'variable' => 'EMPTY_STRING'
],
'String null value' => [
'String null value' => [
'value' => 'NULL',
'variable' => 'NULL_VALUE'
],
Expand Down
Loading