diff --git a/src/Loader.php b/src/Loader.php index 54e3e5e2..96c5dc9f 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -294,12 +294,7 @@ protected function splitCompoundStringIntoParts($name, $value) */ protected function sanitiseVariableValue($name, $value) { - if ($value === null) { - return [$name, $value]; - } - - $value = trim($value); - if ($value === '') { + if ($value === null || trim($value) === '') { return [$name, $value]; } @@ -325,7 +320,7 @@ protected function sanitiseVariableValue($name, $value) $value = str_replace('\\\\', '\\', $value); } else { $parts = explode(' #', $value, 2); - $value = trim($parts[0]); + $value = $parts[0]; // Unquoted values cannot contain whitespace if (preg_match('/\s+/', $value) > 0) { @@ -338,7 +333,7 @@ protected function sanitiseVariableValue($name, $value) } } - return [$name, trim($value)]; + return [$name, $value]; } /** diff --git a/tests/Dotenv/DotenvTest.php b/tests/Dotenv/DotenvTest.php index 48770161..f8a4dd8a 100644 --- a/tests/Dotenv/DotenvTest.php +++ b/tests/Dotenv/DotenvTest.php @@ -260,12 +260,12 @@ public function testDotenvAssertions() $dotenv->load(); $this->assertSame('val1', getenv('ASSERTVAR1')); $this->assertEmpty(getenv('ASSERTVAR2')); - $this->assertEmpty(getenv('ASSERTVAR3')); + $this->assertSame('val3 ', getenv('ASSERTVAR3')); $this->assertSame('0', getenv('ASSERTVAR4')); $this->assertSame('#foo', getenv('ASSERTVAR5')); $this->assertSame("val1\nval2", getenv('ASSERTVAR6')); - $this->assertSame("val3", getenv('ASSERTVAR7')); - $this->assertSame("val3", getenv('ASSERTVAR8')); + $this->assertSame("\nval3", getenv('ASSERTVAR7')); + $this->assertSame("val3\n", getenv('ASSERTVAR8')); $dotenv->required([ 'ASSERTVAR1', @@ -281,6 +281,7 @@ public function testDotenvAssertions() $dotenv->required([ 'ASSERTVAR1', + 'ASSERTVAR3', 'ASSERTVAR4', 'ASSERTVAR5', 'ASSERTVAR6', @@ -292,9 +293,7 @@ public function testDotenvAssertions() 'ASSERTVAR1', 'ASSERTVAR4', 'ASSERTVAR5', - 'ASSERTVAR7', - 'ASSERTVAR8', - ])->notEmpty()->allowedValues(['0', 'val1', 'val3', '#foo']); + ])->notEmpty()->allowedValues(['0', 'val1', '#foo']); $this->assertTrue(true); // anything wrong an an exception will be thrown } @@ -314,26 +313,13 @@ public function testDotenvEmptyThrowsRuntimeException() /** * @expectedException \Dotenv\Exception\ValidationException - * @expectedExceptionMessage One or more environment variables failed assertions: ASSERTVAR3 is empty. + * @expectedExceptionMessage One or more environment variables failed assertions: ASSERTVAR9 is empty. */ public function testDotenvStringOfSpacesConsideredEmpty() { $dotenv = Dotenv::create($this->fixturesFolder, 'assertions.env'); $dotenv->load(); - $this->assertEmpty(getenv('ASSERTVAR3')); - - $dotenv->required('ASSERTVAR3')->notEmpty(); - } - - /** - * @expectedException \Dotenv\Exception\ValidationException - * @expectedExceptionMessage One or more environment variables failed assertions: ASSERTVAR3 is empty. - */ - public function testDotenvHitsLastChain() - { - $dotenv = Dotenv::create($this->fixturesFolder, 'assertions.env'); - $dotenv->load(); - $dotenv->required('ASSERTVAR3')->notEmpty(); + $dotenv->required('ASSERTVAR9')->notEmpty(); } /** diff --git a/tests/fixtures/env/assertions.env b/tests/fixtures/env/assertions.env index 565c1c6c..f82325df 100644 --- a/tests/fixtures/env/assertions.env +++ b/tests/fixtures/env/assertions.env @@ -1,6 +1,7 @@ -ASSERTVAR1="val1" +ASSERTVAR1=val1 + ASSERTVAR2="" -ASSERTVAR3=" " +ASSERTVAR3="val3 " ASSERTVAR4="0" # empty looking value ASSERTVAR5=#foo ASSERTVAR6="val1