Skip to content

Commit 995c797

Browse files
Stas-Sorokafabpot
authored andcommitted
[Dotenv] improved code coverage and removed unreachable code
1 parent 4b2d274 commit 995c797

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

Dotenv.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,6 @@ private function lexValue()
271271
throw $this->createFormatException('Missing quote to end the value');
272272
}
273273
}
274-
if ("\n" === $this->data[$this->cursor]) {
275-
throw $this->createFormatException('Missing quote to end the value');
276-
}
277274
++$this->cursor;
278275
$value = str_replace(array('\\"', '\r', '\n'), array('"', "\r", "\n"), $value);
279276
$resolvedValue = $value;

Tests/DotenvTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ public function getEnvDataWithFormatErrors()
4040
array('FOO', "Missing = in the environment variable declaration in \".env\" at line 1.\n...FOO...\n ^ line 1 offset 3"),
4141
array('FOO="foo', "Missing quote to end the value in \".env\" at line 1.\n...FOO=\"foo...\n ^ line 1 offset 8"),
4242
array('FOO=\'foo', "Missing quote to end the value in \".env\" at line 1.\n...FOO='foo...\n ^ line 1 offset 8"),
43+
array('FOO=\'foo'."\n", "Missing quote to end the value in \".env\" at line 1.\n...FOO='foo\\n...\n ^ line 1 offset 8"),
4344
array('export FOO', "Unable to unset an environment variable in \".env\" at line 1.\n...export FOO...\n ^ line 1 offset 10"),
4445
array('FOO=${FOO', "Unclosed braces on variable expansion in \".env\" at line 1.\n...FOO=\${FOO...\n ^ line 1 offset 9"),
46+
array('FOO= BAR', "Whitespace are not supported before the value in \".env\" at line 1.\n...FOO= BAR...\n ^ line 1 offset 4"),
47+
array('Стасян', "Invalid character in variable name in \".env\" at line 1.\n...Стасян...\n ^ line 1 offset 0"),
48+
array('FOO!', "Missing = in the environment variable declaration in \".env\" at line 1.\n...FOO!...\n ^ line 1 offset 3"),
49+
array('FOO=$(echo foo', "Missing closing parenthesis. in \".env\" at line 1.\n...FOO=$(echo foo...\n ^ line 1 offset 14"),
50+
array('FOO=$(echo foo'."\n", "Missing closing parenthesis. in \".env\" at line 1.\n...FOO=$(echo foo\\n...\n ^ line 1 offset 14"),
4551
);
4652

4753
if ('\\' !== \DIRECTORY_SEPARATOR) {
@@ -64,6 +70,7 @@ public function getEnvData()
6470
{
6571
putenv('LOCAL=local');
6672
$_ENV['REMOTE'] = 'remote';
73+
$_SERVER['SERVERVAR'] = 'servervar';
6774

6875
$tests = array(
6976
// backslashes
@@ -150,6 +157,7 @@ public function getEnvData()
150157
array('FOO=" $ "', array('FOO' => ' $ ')),
151158
array('BAR=$LOCAL', array('BAR' => 'local')),
152159
array('BAR=$REMOTE', array('BAR' => 'remote')),
160+
array('BAR=$SERVERVAR', array('BAR' => 'servervar')),
153161
array('FOO=$NOTDEFINED', array('FOO' => '')),
154162
);
155163

@@ -222,6 +230,7 @@ public function testLoadEnv()
222230

223231
// .env.local
224232

233+
$_SERVER['TEST_APP_ENV'] = 'local';
225234
file_put_contents("$path.local", 'FOO=localBAR');
226235
(new DotEnv())->loadEnv($path, 'TEST_APP_ENV');
227236
$this->assertSame('localBAR', getenv('FOO'));

0 commit comments

Comments
 (0)