From 1befd4a60cd44cf4629bce803dc5a86a258b84f8 Mon Sep 17 00:00:00 2001 From: Baptiste Leduc Date: Sat, 19 May 2018 15:46:27 +0200 Subject: [PATCH 1/5] updating tests to remove trimed spaces from explicit variable --- tests/Dotenv/DotenvTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Dotenv/DotenvTest.php b/tests/Dotenv/DotenvTest.php index 48770161..6cdc6e1f 100644 --- a/tests/Dotenv/DotenvTest.php +++ b/tests/Dotenv/DotenvTest.php @@ -260,7 +260,7 @@ public function testDotenvAssertions() $dotenv->load(); $this->assertSame('val1', getenv('ASSERTVAR1')); $this->assertEmpty(getenv('ASSERTVAR2')); - $this->assertEmpty(getenv('ASSERTVAR3')); + $this->assertSame(' ', getenv('ASSERTVAR3')); $this->assertSame('0', getenv('ASSERTVAR4')); $this->assertSame('#foo', getenv('ASSERTVAR5')); $this->assertSame("val1\nval2", getenv('ASSERTVAR6')); @@ -281,6 +281,7 @@ public function testDotenvAssertions() $dotenv->required([ 'ASSERTVAR1', + 'ASSERTVAR3', 'ASSERTVAR4', 'ASSERTVAR5', 'ASSERTVAR6', From cda4b8a9e91d345a49f264c96653a5517fe5a970 Mon Sep 17 00:00:00 2001 From: Baptiste Leduc Date: Sat, 19 May 2018 15:49:44 +0200 Subject: [PATCH 2/5] removing trim on env values --- src/Loader.php | 11 +++-------- src/Validator.php | 2 +- 2 files changed, 4 insertions(+), 9 deletions(-) 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/src/Validator.php b/src/Validator.php index 9132f16d..4c553ec3 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -60,7 +60,7 @@ public function notEmpty() { return $this->assertCallback( function ($value) { - return strlen(trim($value)) > 0; + return strlen($value) > 0; }, 'is empty' ); From 8038bb840b76f4dd019910f09c5d1130eaf0fdf7 Mon Sep 17 00:00:00 2001 From: Baptiste Leduc Date: Sat, 19 May 2018 15:51:55 +0200 Subject: [PATCH 3/5] updating tests to have value with trailing space --- tests/Dotenv/DotenvTest.php | 2 +- tests/fixtures/env/assertions.env | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Dotenv/DotenvTest.php b/tests/Dotenv/DotenvTest.php index 6cdc6e1f..f4d655d8 100644 --- a/tests/Dotenv/DotenvTest.php +++ b/tests/Dotenv/DotenvTest.php @@ -260,7 +260,7 @@ public function testDotenvAssertions() $dotenv->load(); $this->assertSame('val1', getenv('ASSERTVAR1')); $this->assertEmpty(getenv('ASSERTVAR2')); - $this->assertSame(' ', getenv('ASSERTVAR3')); + $this->assertSame('val3 ', getenv('ASSERTVAR3')); $this->assertSame('0', getenv('ASSERTVAR4')); $this->assertSame('#foo', getenv('ASSERTVAR5')); $this->assertSame("val1\nval2", getenv('ASSERTVAR6')); diff --git a/tests/fixtures/env/assertions.env b/tests/fixtures/env/assertions.env index 565c1c6c..2adc2783 100644 --- a/tests/fixtures/env/assertions.env +++ b/tests/fixtures/env/assertions.env @@ -1,6 +1,6 @@ ASSERTVAR1="val1" ASSERTVAR2="" -ASSERTVAR3=" " +ASSERTVAR3="val3 " ASSERTVAR4="0" # empty looking value ASSERTVAR5=#foo ASSERTVAR6="val1 From d6d94e9fc92a533cf06ff24fe5599aefba895fcf Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 2 Jan 2019 20:46:57 +0000 Subject: [PATCH 4/5] Reverted change to validator --- src/Validator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Validator.php b/src/Validator.php index 4c553ec3..9132f16d 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -60,7 +60,7 @@ public function notEmpty() { return $this->assertCallback( function ($value) { - return strlen($value) > 0; + return strlen(trim($value)) > 0; }, 'is empty' ); From 5602ebc11a34d1dbc96d7deaa54c9d666a8e2efe Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 2 Jan 2019 20:47:02 +0000 Subject: [PATCH 5/5] Fixed tests --- tests/Dotenv/DotenvTest.php | 25 +++++-------------------- tests/fixtures/env/assertions.env | 3 ++- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/tests/Dotenv/DotenvTest.php b/tests/Dotenv/DotenvTest.php index f4d655d8..f8a4dd8a 100644 --- a/tests/Dotenv/DotenvTest.php +++ b/tests/Dotenv/DotenvTest.php @@ -264,8 +264,8 @@ public function testDotenvAssertions() $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', @@ -293,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 } @@ -315,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 2adc2783..f82325df 100644 --- a/tests/fixtures/env/assertions.env +++ b/tests/fixtures/env/assertions.env @@ -1,4 +1,5 @@ -ASSERTVAR1="val1" +ASSERTVAR1=val1 + ASSERTVAR2="" ASSERTVAR3="val3 " ASSERTVAR4="0" # empty looking value