From 631e26658534504bf92415c0073e24ecbb34cbbc Mon Sep 17 00:00:00 2001 From: Louis-Michel Couture Date: Mon, 13 Jul 2015 19:27:30 -0400 Subject: [PATCH] Move loader creation to the constructor This allow the use of "required" to validate variables without loading without explicitely loading a .env file. Add test case for required validation without loading --- src/Dotenv.php | 3 +-- tests/Dotenv/DotenvTest.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Dotenv.php b/src/Dotenv.php index e010962e..56374083 100644 --- a/src/Dotenv.php +++ b/src/Dotenv.php @@ -26,6 +26,7 @@ class Dotenv public function __construct($path, $file = '.env') { $this->filePath = $this->getFilePath($path, $file); + $this->loader = new Loader($this->filePath, $immutable = true); } /** @@ -35,8 +36,6 @@ public function __construct($path, $file = '.env') */ public function load() { - $this->loader = new Loader($this->filePath, $immutable = true); - return $this->loader->load(); } diff --git a/tests/Dotenv/DotenvTest.php b/tests/Dotenv/DotenvTest.php index 3c5f75e2..9133b9f5 100644 --- a/tests/Dotenv/DotenvTest.php +++ b/tests/Dotenv/DotenvTest.php @@ -271,4 +271,14 @@ public function testDotenvHitsLastChain() $dotenv->load(); $dotenv->required('ASSERTVAR3')->notEmpty(); } + + /** + * @expectedException RuntimeException + * @expectedExceptionMessage One or more environment variables failed assertions: foo is missing + */ + public function testDotenvValidateRequiredWithoutLoading() + { + $dotenv = new Dotenv($this->fixturesFolder, 'assertions.env'); + $dotenv->required('foo'); + } }