@@ -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