Skip to content

Commit 96527a6

Browse files
authored
Release/1.1.0 (#4)
1 parent 52bebfe commit 96527a6

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ To create and work with environment variables, use the `from` method to get an i
3535
EnvironmentVariable::from(name: 'MY_VAR');
3636
```
3737

38+
To retrieve an environment variable with the option of providing a default value in case the variable does not exist,
39+
use the `fromOrDefault` method.
40+
41+
If the environment variable is not found, the method will return the provided default value instead of throwing an
42+
exception.
43+
44+
```php
45+
EnvironmentVariable::fromOrDefault(name: 'MY_VAR', defaultValueIfNotFound: 'default_value');
46+
```
47+
3848
### Conversions
3949

4050
Once you have an instance of the environment variable, you can convert its value into various types.

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
failOnRisky="true"
77
failOnWarning="true"
88
cacheDirectory=".phpunit.cache"
9+
executionOrder="random"
910
beStrictAboutOutputDuringTests="true">
1011

1112
<source>

src/Environment.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ interface Environment
2121
*/
2222
public static function from(string $name): Environment;
2323

24+
/**
25+
* Retrieves an instance of the environment variable or uses a default value if not found.
26+
*
27+
* @param string $name The name of the environment variable.
28+
* @param string|null $defaultValueIfNotFound The default value to use if the environment variable is not found.
29+
* @return EnvironmentVariable The environment variable instance, either with the found value or the default.
30+
*/
31+
public static function fromOrDefault(string $name, string $defaultValueIfNotFound = null): EnvironmentVariable;
32+
2433
/**
2534
* Checks if the environment variable has a value. Values like `false`, `0`, and `-1` are valid and non-empty.
2635
*

src/EnvironmentVariable.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ public static function from(string $name): EnvironmentVariable
2222
: new EnvironmentVariable(value: $environmentVariable, variable: $name);
2323
}
2424

25+
public static function fromOrDefault(string $name, string $defaultValueIfNotFound = null): EnvironmentVariable
26+
{
27+
$environmentVariable = getenv($name);
28+
29+
return $environmentVariable === false
30+
? new EnvironmentVariable(value: (string)$defaultValueIfNotFound, variable: $name)
31+
: new EnvironmentVariable(value: $environmentVariable, variable: $name);
32+
}
33+
2534
public function hasValue(): bool
2635
{
2736
return match (strtolower(trim($this->value))) {

tests/EnvironmentVariableTest.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,43 @@ public function testConvertToBoolean(mixed $value, string $variable, bool $expec
5050
self::assertEquals($expected, $actual);
5151
}
5252

53+
public function testFromOrDefaultWithDefaultValue(): void
54+
{
55+
/** @Given that the environment variable 'NON_EXISTENT_MY_VAR' does not exist */
56+
$variable = 'NON_EXISTENT_MY_VAR';
57+
58+
/** @When I try to get the value of the environment variable with a default value */
59+
$actual = EnvironmentVariable::fromOrDefault(name: $variable, defaultValueIfNotFound: '0');
60+
61+
/** @Then the result should match the default value */
62+
self::assertEquals(0, $actual->toInteger());
63+
}
64+
65+
public function testFromOrDefaultWithExistingVariable(): void
66+
{
67+
/** @Given that the environment variable 'MY_VAR' exists with the value 'existing_value' */
68+
putenv(sprintf('%s=%s', 'MY_VAR', 'existing_value'));
69+
70+
/** @When I try to get the value of the environment variable */
71+
$actual = EnvironmentVariable::fromOrDefault(name: 'MY_VAR', defaultValueIfNotFound: 'default_value');
72+
73+
/** @Then the result should match the existing value */
74+
self::assertEquals('existing_value', $actual->toString());
75+
}
76+
77+
public function testFromOrDefaultWhenVariableIsMissingAndNoDefault(): void
78+
{
79+
/** @Given that the environment variable 'NON_EXISTENT_VAR' does not exist */
80+
$variable = 'NON_EXISTENT_VAR';
81+
82+
/** @When I try to get the value of the missing environment variable without a default value */
83+
$actual = EnvironmentVariable::fromOrDefault(name: $variable);
84+
85+
/** @Then the result should be no value */
86+
self::assertEmpty($actual->toString());
87+
self::assertFalse($actual->hasValue());
88+
}
89+
5390
#[DataProvider('hasValueDataProvider')]
5491
public function testHasValue(mixed $value, string $variable): void
5592
{
@@ -225,15 +262,15 @@ public static function hasValueDataProvider(): array
225262
public static function hasNoValueDataProvider(): array
226263
{
227264
return [
228-
'Null value' => [
265+
'Null value' => [
229266
'value' => null,
230267
'variable' => 'NULL_VALUE'
231268
],
232-
'Empty string' => [
269+
'Empty string' => [
233270
'value' => '',
234271
'variable' => 'EMPTY_STRING'
235272
],
236-
'String null value' => [
273+
'String null value' => [
237274
'value' => 'NULL',
238275
'variable' => 'NULL_VALUE'
239276
],

0 commit comments

Comments
 (0)