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

Preserve existing zero-like variables #39

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
8 changes: 4 additions & 4 deletions src/josegonzalez/Dotenv/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public function apacheSetenv($overwrite = false)
$this->requireParse('apache_setenv');
foreach ($this->environment as $key => $value) {
$prefixedKey = $this->prefixed($key);
if (apache_getenv($prefixedKey) && !$overwrite) {
if (apache_getenv($prefixedKey) !== false && !$overwrite) {
if ($this->skip['apacheSetenv']) {
continue;
}
Expand Down Expand Up @@ -281,7 +281,7 @@ public function putenv($overwrite = false)
$this->requireParse('putenv');
foreach ($this->environment as $key => $value) {
$prefixedKey = $this->prefixed($key);
if (getenv($prefixedKey) && !$overwrite) {
if (getenv($prefixedKey) !== false && !$overwrite) {
if ($this->skip['putenv']) {
continue;
}
Expand All @@ -303,7 +303,7 @@ public function toEnv($overwrite = false)
$this->requireParse('toEnv');
foreach ($this->environment as $key => $value) {
$prefixedKey = $this->prefixed($key);
if (isset($_ENV[$prefixedKey]) && !$overwrite) {
if (array_key_exists($prefixedKey, $_ENV) && !$overwrite) {
if ($this->skip['toEnv']) {
continue;
}
Expand All @@ -325,7 +325,7 @@ public function toServer($overwrite = false)
$this->requireParse('toServer');
foreach ($this->environment as $key => $value) {
$prefixedKey = $this->prefixed($key);
if (isset($_SERVER[$prefixedKey]) && !$overwrite) {
if (array_key_exists($prefixedKey, $_SERVER) && !$overwrite) {
if ($this->skip['toServer']) {
continue;
}
Expand Down
112 changes: 107 additions & 5 deletions tests/josegonzalez/Dotenv/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ function ($key) {
$apacheSetenv = $this->getFunctionMock(__NAMESPACE__, 'apache_setenv');
$apacheSetenv->expects($this->any())->willReturnCallback(
function ($key, $value) {
$GLOBALS['apache_test_data'][$key] = $value;
$GLOBALS['apache_test_data'][$key] = (string)$value;
return true;
}
);
Expand All @@ -607,15 +607,15 @@ public function testToApacheSetenvSkip()
$apacheGetenv->expects($this->any())->willReturnCallback(
function ($key) {
if (isset($GLOBALS['apache_test_data'][$key])) {
return $GLOBALS['apache_test_data'][$key];
return (string)$GLOBALS['apache_test_data'][$key];
}
return false;
}
);
$apacheSetenv = $this->getFunctionMock(__NAMESPACE__, 'apache_setenv');
$apacheSetenv->expects($this->any())->willReturnCallback(
function ($key, $value) {
$GLOBALS['apache_test_data'][$key] = $value;
$GLOBALS['apache_test_data'][$key] = (string)$value;
return true;
}
);
Expand Down Expand Up @@ -646,15 +646,15 @@ public function testToApacheSetenvException()
$apacheGetenv->expects($this->any())->willReturnCallback(
function ($key) {
if (isset($GLOBALS['apache_test_data'][$key])) {
return $GLOBALS['apache_test_data'][$key];
return (string)$GLOBALS['apache_test_data'][$key];
}
return false;
}
);
$apacheSetenv = $this->getFunctionMock(__NAMESPACE__, 'apache_setenv');
$apacheSetenv->expects($this->any())->willReturnCallback(
function ($key, $value) {
$GLOBALS['apache_test_data'][$key] = $value;
$GLOBALS['apache_test_data'][$key] = (string)$value;
return true;
}
);
Expand All @@ -664,6 +664,48 @@ function ($key, $value) {
$this->Loader->apacheSetenv(false);
}


/**
* @covers \josegonzalez\Dotenv\Loader::putenv
*/
public function testToApacheSetenvPreserveZeros()
{
if (version_compare(PHP_VERSION, '7.0', '<')) {
$this->markTestSkipped('Unable to mock bare php functions');
}

$apacheGetenv = $this->getFunctionMock(__NAMESPACE__, 'apache_getenv');
$apacheGetenv->expects($this->any())->willReturnCallback(
function ($key) {
if (isset($GLOBALS['apache_test_data'][$key])) {
return (string)$GLOBALS['apache_test_data'][$key];
}
return false;
}
);
$apacheSetenv = $this->getFunctionMock(__NAMESPACE__, 'apache_setenv');
$apacheSetenv->expects($this->any())->willReturnCallback(
function ($key, $value) {
$GLOBALS['apache_test_data'][$key] = (string)$value;
return true;
}
);

$this->Loader->setFilepaths($this->fixturePath . 'zero_test_0.env');
$this->Loader->parse();
$this->Loader->apacheSetenv(false);

$this->Loader->setFilepaths($this->fixturePath . 'zero_test_1.env');
$this->Loader->parse();
$this->Loader->skipExisting('apacheSetenv');
$this->Loader->apacheSetenv(false);

$this->assertEquals('0', apache_getenv('Z_NUMBER'));
$this->assertEquals('', apache_getenv('Z_BOOL'));
$this->assertEquals('', apache_getenv('Z_STRING'));
$this->assertEquals('', apache_getenv('Z_NULLABLE'));
}

/**
* @covers \josegonzalez\Dotenv\Loader::define
*/
Expand Down Expand Up @@ -746,6 +788,26 @@ public function testToPutenvException()
$this->Loader->putenv(false);
}

/**
* @covers \josegonzalez\Dotenv\Loader::putenv
*/
public function testToPutenvPreserveZeros()
{
$this->Loader->setFilepaths($this->fixturePath . 'zero_test_0.env');
$this->Loader->parse();
$this->Loader->putenv(false);

$this->Loader->setFilepaths($this->fixturePath . 'zero_test_1.env');
$this->Loader->parse();
$this->Loader->skipExisting('putenv');
$this->Loader->putenv(false);

$this->assertEquals('0', getenv('Z_NUMBER'));
$this->assertEquals('', getenv('Z_BOOL'));
$this->assertEquals('', getenv('Z_STRING'));
$this->assertEquals('', getenv('Z_NULLABLE'));
}

/**
* @covers \josegonzalez\Dotenv\Loader::toEnv
*/
Expand Down Expand Up @@ -788,6 +850,26 @@ public function testToEnvException()
$this->Loader->toEnv(false);
}

/**
* @covers \josegonzalez\Dotenv\Loader::toEnv
*/
public function testToEnvPreserveZeros()
{
$this->Loader->setFilepaths($this->fixturePath . 'zero_test_0.env');
$this->Loader->parse();
$this->Loader->toEnv(false);

$this->Loader->setFilepaths($this->fixturePath . 'zero_test_1.env');
$this->Loader->parse();
$this->Loader->skipExisting('toEnv');
$this->Loader->toEnv(false);

$this->assertEquals(0, $_ENV['Z_NUMBER']);
$this->assertEquals(false, $_ENV['Z_BOOL']);
$this->assertEquals('', $_ENV['Z_STRING']);
$this->assertEquals(null, $_ENV['Z_NULLABLE']);
}

/**
* @covers \josegonzalez\Dotenv\Loader::toServer
*/
Expand Down Expand Up @@ -830,6 +912,26 @@ public function testToServerException()
$this->Loader->toServer(false);
}

/**
* @covers \josegonzalez\Dotenv\Loader::toServer
*/
public function testToServerPreserveZeros()
{
$this->Loader->setFilepaths($this->fixturePath . 'zero_test_0.env');
$this->Loader->parse();
$this->Loader->toServer(false);

$this->Loader->setFilepaths($this->fixturePath . 'zero_test_1.env');
$this->Loader->parse();
$this->Loader->skipExisting('toServer');
$this->Loader->toServer(false);

$this->assertEquals(0, $_SERVER['Z_NUMBER']);
$this->assertEquals(false, $_SERVER['Z_BOOL']);
$this->assertEquals('', $_SERVER['Z_STRING']);
$this->assertEquals(null, $_SERVER['Z_NULLABLE']);
}

/**
* @covers \josegonzalez\Dotenv\Loader::skipped
* @covers \josegonzalez\Dotenv\Loader::skipExisting
Expand Down
4 changes: 4 additions & 0 deletions tests/josegonzalez/fixtures/zero_test_0.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Z_NUMBER=0
Z_BOOL=false
Z_STRING=""
Z_NULLABLE=null
4 changes: 4 additions & 0 deletions tests/josegonzalez/fixtures/zero_test_1.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Z_NUMBER=1
Z_BOOL=true
Z_STRING=foo_bar
Z_NULLABLE="None null"