Skip to content

Commit 8f363b6

Browse files
fix: putenv() couldn't preserve existing zero or empty string, toEnv()/toServer() couldn't preserve existing null variables.
1 parent 81382da commit 8f363b6

File tree

4 files changed

+119
-9
lines changed

4 files changed

+119
-9
lines changed

src/josegonzalez/Dotenv/Loader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public function apacheSetenv($overwrite = false)
237237
$this->requireParse('apache_setenv');
238238
foreach ($this->environment as $key => $value) {
239239
$prefixedKey = $this->prefixed($key);
240-
if (apache_getenv($prefixedKey) && !$overwrite) {
240+
if (apache_getenv($prefixedKey) !== false && !$overwrite) {
241241
if ($this->skip['apacheSetenv']) {
242242
continue;
243243
}
@@ -281,7 +281,7 @@ public function putenv($overwrite = false)
281281
$this->requireParse('putenv');
282282
foreach ($this->environment as $key => $value) {
283283
$prefixedKey = $this->prefixed($key);
284-
if (getenv($prefixedKey) && !$overwrite) {
284+
if (getenv($prefixedKey) !== false && !$overwrite) {
285285
if ($this->skip['putenv']) {
286286
continue;
287287
}
@@ -303,7 +303,7 @@ public function toEnv($overwrite = false)
303303
$this->requireParse('toEnv');
304304
foreach ($this->environment as $key => $value) {
305305
$prefixedKey = $this->prefixed($key);
306-
if (isset($_ENV[$prefixedKey]) && !$overwrite) {
306+
if (array_key_exists($prefixedKey, $_ENV) && !$overwrite) {
307307
if ($this->skip['toEnv']) {
308308
continue;
309309
}
@@ -325,7 +325,7 @@ public function toServer($overwrite = false)
325325
$this->requireParse('toServer');
326326
foreach ($this->environment as $key => $value) {
327327
$prefixedKey = $this->prefixed($key);
328-
if (isset($_SERVER[$prefixedKey]) && !$overwrite) {
328+
if (array_key_exists($prefixedKey, $_SERVER) && !$overwrite) {
329329
if ($this->skip['toServer']) {
330330
continue;
331331
}

tests/josegonzalez/Dotenv/LoaderTest.php

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ function ($key) {
580580
$apacheSetenv = $this->getFunctionMock(__NAMESPACE__, 'apache_setenv');
581581
$apacheSetenv->expects($this->any())->willReturnCallback(
582582
function ($key, $value) {
583-
$GLOBALS['apache_test_data'][$key] = $value;
583+
$GLOBALS['apache_test_data'][$key] = (string)$value;
584584
return true;
585585
}
586586
);
@@ -607,15 +607,15 @@ public function testToApacheSetenvSkip()
607607
$apacheGetenv->expects($this->any())->willReturnCallback(
608608
function ($key) {
609609
if (isset($GLOBALS['apache_test_data'][$key])) {
610-
return $GLOBALS['apache_test_data'][$key];
610+
return (string)$GLOBALS['apache_test_data'][$key];
611611
}
612612
return false;
613613
}
614614
);
615615
$apacheSetenv = $this->getFunctionMock(__NAMESPACE__, 'apache_setenv');
616616
$apacheSetenv->expects($this->any())->willReturnCallback(
617617
function ($key, $value) {
618-
$GLOBALS['apache_test_data'][$key] = $value;
618+
$GLOBALS['apache_test_data'][$key] = (string)$value;
619619
return true;
620620
}
621621
);
@@ -646,15 +646,15 @@ public function testToApacheSetenvException()
646646
$apacheGetenv->expects($this->any())->willReturnCallback(
647647
function ($key) {
648648
if (isset($GLOBALS['apache_test_data'][$key])) {
649-
return $GLOBALS['apache_test_data'][$key];
649+
return (string)$GLOBALS['apache_test_data'][$key];
650650
}
651651
return false;
652652
}
653653
);
654654
$apacheSetenv = $this->getFunctionMock(__NAMESPACE__, 'apache_setenv');
655655
$apacheSetenv->expects($this->any())->willReturnCallback(
656656
function ($key, $value) {
657-
$GLOBALS['apache_test_data'][$key] = $value;
657+
$GLOBALS['apache_test_data'][$key] = (string)$value;
658658
return true;
659659
}
660660
);
@@ -664,6 +664,48 @@ function ($key, $value) {
664664
$this->Loader->apacheSetenv(false);
665665
}
666666

667+
668+
/**
669+
* @covers \josegonzalez\Dotenv\Loader::putenv
670+
*/
671+
public function testToApacheSetenvPreserveZeros()
672+
{
673+
if (version_compare(PHP_VERSION, '7.0', '<')) {
674+
$this->markTestSkipped('Unable to mock bare php functions');
675+
}
676+
677+
$apacheGetenv = $this->getFunctionMock(__NAMESPACE__, 'apache_getenv');
678+
$apacheGetenv->expects($this->any())->willReturnCallback(
679+
function ($key) {
680+
if (isset($GLOBALS['apache_test_data'][$key])) {
681+
return (string)$GLOBALS['apache_test_data'][$key];
682+
}
683+
return false;
684+
}
685+
);
686+
$apacheSetenv = $this->getFunctionMock(__NAMESPACE__, 'apache_setenv');
687+
$apacheSetenv->expects($this->any())->willReturnCallback(
688+
function ($key, $value) {
689+
$GLOBALS['apache_test_data'][$key] = (string)$value;
690+
return true;
691+
}
692+
);
693+
694+
$this->Loader->setFilepaths($this->fixturePath . 'zero_test_0.env');
695+
$this->Loader->parse();
696+
$this->Loader->apacheSetenv(false);
697+
698+
$this->Loader->setFilepaths($this->fixturePath . 'zero_test_1.env');
699+
$this->Loader->parse();
700+
$this->Loader->skipExisting('apacheSetenv');
701+
$this->Loader->apacheSetenv(false);
702+
703+
$this->assertEquals('0', apache_getenv('Z_NUMBER'));
704+
$this->assertEquals('', apache_getenv('Z_BOOL'));
705+
$this->assertEquals('', apache_getenv('Z_STRING'));
706+
$this->assertEquals('', apache_getenv('Z_NULLABLE'));
707+
}
708+
667709
/**
668710
* @covers \josegonzalez\Dotenv\Loader::define
669711
*/
@@ -746,6 +788,26 @@ public function testToPutenvException()
746788
$this->Loader->putenv(false);
747789
}
748790

791+
/**
792+
* @covers \josegonzalez\Dotenv\Loader::putenv
793+
*/
794+
public function testToPutenvPreserveZeros()
795+
{
796+
$this->Loader->setFilepaths($this->fixturePath . 'zero_test_0.env');
797+
$this->Loader->parse();
798+
$this->Loader->putenv(false);
799+
800+
$this->Loader->setFilepaths($this->fixturePath . 'zero_test_1.env');
801+
$this->Loader->parse();
802+
$this->Loader->skipExisting('putenv');
803+
$this->Loader->putenv(false);
804+
805+
$this->assertEquals('0', getenv('Z_NUMBER'));
806+
$this->assertEquals('', getenv('Z_BOOL'));
807+
$this->assertEquals('', getenv('Z_STRING'));
808+
$this->assertEquals('', getenv('Z_NULLABLE'));
809+
}
810+
749811
/**
750812
* @covers \josegonzalez\Dotenv\Loader::toEnv
751813
*/
@@ -788,6 +850,26 @@ public function testToEnvException()
788850
$this->Loader->toEnv(false);
789851
}
790852

853+
/**
854+
* @covers \josegonzalez\Dotenv\Loader::toEnv
855+
*/
856+
public function testToEnvPreserveZeros()
857+
{
858+
$this->Loader->setFilepaths($this->fixturePath . 'zero_test_0.env');
859+
$this->Loader->parse();
860+
$this->Loader->toEnv(false);
861+
862+
$this->Loader->setFilepaths($this->fixturePath . 'zero_test_1.env');
863+
$this->Loader->parse();
864+
$this->Loader->skipExisting('toEnv');
865+
$this->Loader->toEnv(false);
866+
867+
$this->assertEquals(0, $_ENV['Z_NUMBER']);
868+
$this->assertEquals(false, $_ENV['Z_BOOL']);
869+
$this->assertEquals('', $_ENV['Z_STRING']);
870+
$this->assertEquals(null, $_ENV['Z_NULLABLE']);
871+
}
872+
791873
/**
792874
* @covers \josegonzalez\Dotenv\Loader::toServer
793875
*/
@@ -830,6 +912,26 @@ public function testToServerException()
830912
$this->Loader->toServer(false);
831913
}
832914

915+
/**
916+
* @covers \josegonzalez\Dotenv\Loader::toServer
917+
*/
918+
public function testToServerPreserveZeros()
919+
{
920+
$this->Loader->setFilepaths($this->fixturePath . 'zero_test_0.env');
921+
$this->Loader->parse();
922+
$this->Loader->toServer(false);
923+
924+
$this->Loader->setFilepaths($this->fixturePath . 'zero_test_1.env');
925+
$this->Loader->parse();
926+
$this->Loader->skipExisting('toServer');
927+
$this->Loader->toServer(false);
928+
929+
$this->assertEquals(0, $_SERVER['Z_NUMBER']);
930+
$this->assertEquals(false, $_SERVER['Z_BOOL']);
931+
$this->assertEquals('', $_SERVER['Z_STRING']);
932+
$this->assertEquals(null, $_SERVER['Z_NULLABLE']);
933+
}
934+
833935
/**
834936
* @covers \josegonzalez\Dotenv\Loader::skipped
835937
* @covers \josegonzalez\Dotenv\Loader::skipExisting
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Z_NUMBER=0
2+
Z_BOOL=false
3+
Z_STRING=""
4+
Z_NULLABLE=null
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Z_NUMBER=1
2+
Z_BOOL=true
3+
Z_STRING=foo_bar
4+
Z_NULLABLE="None null"

0 commit comments

Comments
 (0)