diff --git a/.editorconfig b/.editorconfig index f0d4188..7d3af1f 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,13 +4,9 @@ root = false [*] -indent_style = tab -indent_size = 2 +indent_style = space +indent_size = 4 charset = "utf-8" end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true - -[*.yml] -indent_style = space -indent_size = 2 \ No newline at end of file diff --git a/src/josegonzalez/Dotenv/Load.php b/src/josegonzalez/Dotenv/Load.php index 6ebf41f..815d2bf 100644 --- a/src/josegonzalez/Dotenv/Load.php +++ b/src/josegonzalez/Dotenv/Load.php @@ -7,181 +7,181 @@ use JsonSerializable; use RuntimeException; -class Load implements JsonSerializable { +class Load implements JsonSerializable +{ - protected $_filepath = null; + protected $_filepath = null; - protected $_environment = null; + protected $_environment = null; - public function __construct($filepath = null) { - $this->setFilepath($filepath); - return $this; - } - - public function filepath() { - return $this->_filepath; - } - - public function setFilepath($filepath = null) { - if ($filepath == null) { - $filepath = __DIR__ . DIRECTORY_SEPARATOR . '.env'; - } - $this->_filepath = $filepath; - return $this; - } - - public static function load($options = null) { - $filepath = null; - if (is_string($options)) { - $filepath = $options; - $options = array(); + public function __construct($filepath = null) { + $this->setFilepath($filepath); + return $this; } - $dotenv = new \josegonzalez\Dotenv\Load($filepath); - $dotenv->parse(); - if (array_key_exists('expect', $options)) { - $dotenv->expect($options['expect']); + public function filepath() { + return $this->_filepath; } - if (array_key_exists('define', $options)) { - $dotenv->define(); + public function setFilepath($filepath = null) { + if ($filepath == null) { + $filepath = __DIR__ . DIRECTORY_SEPARATOR . '.env'; + } + $this->_filepath = $filepath; + return $this; } - if (array_key_exists('toEnv', $options)) { - $dotenv->toEnv($options['toEnv']); - } + public static function load($options = null) { + $filepath = null; + if (is_string($options)) { + $filepath = $options; + $options = array(); + } - if (array_key_exists('toServer', $options)) { - $dotenv->toServer($options['toServer']); - } + $dotenv = new \josegonzalez\Dotenv\Load($filepath); + $dotenv->parse(); + if (array_key_exists('expect', $options)) { + $dotenv->expect($options['expect']); + } - return $dotenv; - } + if (array_key_exists('define', $options)) { + $dotenv->define(); + } - public function parse() { - if (!file_exists($this->_filepath)) { - throw new InvalidArgumentException(sprintf("Environment file '%s' is not found.", $this->_filepath)); - } + if (array_key_exists('toEnv', $options)) { + $dotenv->toEnv($options['toEnv']); + } - if (!is_readable($this->_filepath)) { - throw new InvalidArgumentException(sprintf("Environment file '%s' is not readable.", $this->_filepath)); - } + if (array_key_exists('toServer', $options)) { + $dotenv->toServer($options['toServer']); + } - $fc = file_get_contents($this->_filepath); - if ($fc === false) { - throw new InvalidArgumentException(sprintf("Environment file '%s' is not readable.", $this->_filepath)); + return $dotenv; } - $lines = explode(PHP_EOL, $fc); + public function parse() { + if (!file_exists($this->_filepath)) { + throw new InvalidArgumentException(sprintf("Environment file '%s' is not found.", $this->_filepath)); + } - $this->_environment = array(); - foreach ($lines as $line) { - if (empty($line)) { - continue; - } + if (!is_readable($this->_filepath)) { + throw new InvalidArgumentException(sprintf("Environment file '%s' is not readable.", $this->_filepath)); + } - if (!preg_match('/(?:export )?([a-zA-Z_][a-zA-Z0-9_]*)=(.*)/', $line, $matches)) { - continue; - } + $fc = file_get_contents($this->_filepath); + if ($fc === false) { + throw new InvalidArgumentException(sprintf("Environment file '%s' is not readable.", $this->_filepath)); + } - $key = $matches[1]; - $value = $matches[2]; - if (preg_match('/^\'(.*)\'$/', $value, $matches)) { - $value = $matches[1]; - } elseif (preg_match('/^"(.*)"$/', $value, $matches)) { - $value = $matches[1]; - } + $lines = explode(PHP_EOL, $fc); - $this->_environment[$key] = $value; - } + $this->_environment = array(); + foreach ($lines as $line) { + if (empty($line)) { + continue; + } - return $this; - } + if (!preg_match('/(?:export )?([a-zA-Z_][a-zA-Z0-9_]*)=(.*)/', $line, $matches)) { + continue; + } - public function expect() { - $this->requireParse('expect'); + $key = $matches[1]; + $value = $matches[2]; + if (preg_match('/^\'(.*)\'$/', $value, $matches)) { + $value = $matches[1]; + } elseif (preg_match('/^"(.*)"$/', $value, $matches)) { + $value = $matches[1]; + } - $args = func_get_args(); - if (count($args) == 0) { - throw new InvalidArgumentException("No arguments were passed to expect()"); + $this->_environment[$key] = $value; + } + + return $this; } - if (isset($args[0]) && is_array($args[0])) { - $args = $args[0]; - } + public function expect() { + $this->requireParse('expect'); - $keys = (array) $args; - $missingEnvs = array(); + $args = func_get_args(); + if (count($args) == 0) { + throw new InvalidArgumentException("No arguments were passed to expect()"); + } - foreach ($keys as $key) { - if (!isset($this->_environment[$key])) { - $missingEnvs[] = $key; - } - } + if (isset($args[0]) && is_array($args[0])) { + $args = $args[0]; + } - if (!empty($missingEnvs)) { - throw new RuntimeException(sprintf("Required ENV vars missing: ['%s']", implode("', '", $missingEnvs))); - } + $keys = (array) $args; + $missingEnvs = array(); - return $this; - } + foreach ($keys as $key) { + if (!isset($this->_environment[$key])) { + $missingEnvs[] = $key; + } + } - public function define() { - $this->requireParse('define'); - foreach ($this->_environment as $key => $value) { - if (defined($key)) { - throw new LogicException(sprintf('Key "%s" has already been defined', $key)); - } + if (!empty($missingEnvs)) { + throw new RuntimeException(sprintf("Required ENV vars missing: ['%s']", implode("', '", $missingEnvs))); + } - define($key, $value); + return $this; } - return $this; - } + public function define() { + $this->requireParse('define'); + foreach ($this->_environment as $key => $value) { + if (defined($key)) { + throw new LogicException(sprintf('Key "%s" has already been defined', $key)); + } - public function toEnv($overwrite = false) { - $this->requireParse('toEnv'); - foreach ($this->_environment as $key => $value) { - if (isset($_ENV[$key]) && !$overwrite) { - throw new LogicException(sprintf('Key "%s" has already been defined in $_ENV', $key)); - } + define($key, $value); + } - $_ENV[$key] = $value; + return $this; } - return $this; - } + public function toEnv($overwrite = false) { + $this->requireParse('toEnv'); + foreach ($this->_environment as $key => $value) { + if (isset($_ENV[$key]) && !$overwrite) { + throw new LogicException(sprintf('Key "%s" has already been defined in $_ENV', $key)); + } - public function toServer($overwrite = false) { - $this->requireParse('toServer'); - foreach ($this->_environment as $key => $value) { - if (isset($_SERVER[$key]) && !$overwrite) { - throw new LogicException(sprintf('Key "%s" has already been defined in $_SERVER', $key)); - } + $_ENV[$key] = $value; + } - $_SERVER[$key] = $value; + return $this; } - return $this; - } + public function toServer($overwrite = false) { + $this->requireParse('toServer'); + foreach ($this->_environment as $key => $value) { + if (isset($_SERVER[$key]) && !$overwrite) { + throw new LogicException(sprintf('Key "%s" has already been defined in $_SERVER', $key)); + } + + $_SERVER[$key] = $value; + } - public function toArray() { - $this->requireParse('environment'); - return $this->_environment; - } + return $this; + } - public function jsonSerialize() { - return $this->toArray(); - } + public function toArray() { + $this->requireParse('environment'); + return $this->_environment; + } - public function __toString() { - return json_encode($this, JSON_PRETTY_PRINT); - } + public function jsonSerialize() { + return $this->toArray(); + } - protected function requireParse($method) { - if (!is_array($this->_environment)) { - throw new LogicException(sprintf('Environment must be parsed before calling %s', $method)); + public function __toString() { + return json_encode($this, JSON_PRETTY_PRINT); } - } + protected function requireParse($method) { + if (!is_array($this->_environment)) { + throw new LogicException(sprintf('Environment must be parsed before calling %s', $method)); + } + } } diff --git a/tests/josegonzalez/Dotenv/Load.php b/tests/josegonzalez/Dotenv/Load.php index c6adf6f..b9df7e3 100644 --- a/tests/josegonzalez/Dotenv/Load.php +++ b/tests/josegonzalez/Dotenv/Load.php @@ -1,172 +1,173 @@ env = $_ENV; - $this->server = $_SERVER; - $this->fixturePath = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR; - $this->Loader = new Loader($this->fixturePath . '.env'); - } - - public function tearDown() { - $_ENV = $this->env; - $_SERVER = $this->server; - unset($this->env); - unset($this->server); - unset($this->fixturePath); - unset($this->Loader); - } - - public function testFilepath() { - $this->assertEquals($this->fixturePath, $this->loader->filepath()); - } - - public function testSetFilepath() { - $this->Loader->setFilepath('/tmp/.env'); - $this->assertEquals('/tmp/.env', $this->Loader->filepath()); - } - - public function testParse() { - $this->Loader->parse(); - $environment = $this->Loader->toArray(); - - $this->assertEquals('bar', $environment['FOO']); - $this->assertEquals('baz', $environment['BAR']); - $this->assertEquals('with spaces', $environment['SPACED']); - $this->assertEquals('pgsql:host=localhost;dbname=test', $environment['EQUALS']); - $this->assertEquals('', $environment['NULL']); - } - - public function testParseExported() { - $this->Loader->setFilepath($this->fixturePath . 'exported.env'); - $this->Loader->parse(); - $environment = $this->Loader->toArray(); - - $this->assertEquals('bar', $environment['EFOO']); - $this->assertEquals('baz', $environment['EBAR']); - $this->assertEquals('with spaces', $environment['ESPACED']); - $this->assertEquals('pgsql:host=localhost;dbname=test', $environment['EEQUALS']); - $this->assertEquals('', $environment['ENULL']); - } - - public function testParseExported() { - $this->Loader->setFilepath($this->fixturePath . 'quoted.env'); - $this->Loader->parse(); - $environment = $this->Loader->toArray(); - - $this->assertEquals('bar', $environment['QFOO']); - $this->assertEquals('baz', $environment['QBAR']); - $this->assertEquals('with spaces', $environment['QSPACED']); - $this->assertEquals('pgsql:host=localhost;dbname=test', $environment['QEQUALS']); - $this->assertEquals('', $environment['QNULL']); - } - - public function testExpect() { - $this->Loader->parse(); - $this->assertTrue($this->Loader->expect('FOO')); - $this->assertTrue($this->Loader->expect(array('FOO', 'BAR'))); - } - - public function testDefine() { - $this->Loader->parse(); - $this->Loader->define(); - - $this->assertEquals('bar', FOO); - $this->assertEquals('baz', BAR); - $this->assertEquals('with spaces', SPACED); - $this->assertEquals('pgsql:host=localhost;dbname=test', EQUALS); - } - - public function testToEnv() { - $this->Loader->parse(); - $this->Loader->toEnv(); - - $this->assertEquals('bar', $_ENV['FOO']); - $this->assertEquals('baz', $_ENV['BAR']); - $this->assertEquals('with spaces', $_ENV['SPACED']); - $this->assertEquals('pgsql:host=localhost;dbname=test', $_ENV['EQUALS']); - $this->assertEquals('', $_ENV['NULL']); - } - - public function testToServer() { - $this->Loader->parse(); - $this->Loader->toServer(); - - $this->assertEquals('bar', $_SERVER['FOO']); - $this->assertEquals('baz', $_SERVER['BAR']); - $this->assertEquals('with spaces', $_SERVER['SPACED']); - $this->assertEquals('pgsql:host=localhost;dbname=test', $_SERVER['EQUALS']); - $this->assertEquals('', $_SERVER['NULL']); - } - - /** - * @expectedException LogicException - * @expectedExceptionMessage Environment must be parsed before calling expect() - */ - public function testExpectRequireException() { - $this->Loader->expect(); - } - - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage No arguments were passed to expect() - */ - public function testExpectArgumentException() { - $this->Loader->parse(); - $this->Loader->expect(); - } - - /** - * @expectedException RuntimeException - * @expectedExceptionMessage Required ENV vars missing: ['BAZ'] - */ - public function testExpectMissingRequiredException() { - $this->Loader->parse(); - $this->assertTrue($this->Loader->expect(array('BAZ'))); - } - - /** - * @expectedException LogicException - * @expectedExceptionMessage Environment must be parsed before calling define() - */ - public function testDefineRequireException() { - $this->Loader->define(); - } - - /** - * @expectedException LogicException - * @expectedExceptionMessage Key "NULL" has already been defined - */ - public function testDefineNullException() { - $this->Loader->parse(); - $this->Loader->define(); - } - - /** - * @expectedException LogicException - * @expectedExceptionMessage Environment must be parsed before calling toEnv() - */ - public function testToEnvRequireException() { - $this->Loader->toEnv(); - } - - /** - * @expectedException LogicException - * @expectedExceptionMessage Environment must be parsed before calling toServer() - */ - public function testToServerRequireException() { - $this->Loader->toServer(); - } - - /** - * @expectedException LogicException - * @expectedExceptionMessage Environment must be parsed before calling toArray() - */ - public function testToArrayException() { - $this->Loader->toArray(); - } - +use \PHPUnit_Framework_TestCase; + +class LoadTest extends PHPUnit_Framework_TestCase +{ + + public function setUp() { + $this->env = $_ENV; + $this->server = $_SERVER; + $this->fixturePath = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR; + $this->Loader = new Loader($this->fixturePath . '.env'); + } + + public function tearDown() { + $_ENV = $this->env; + $_SERVER = $this->server; + unset($this->env); + unset($this->server); + unset($this->fixturePath); + unset($this->Loader); + } + + public function testFilepath() { + $this->assertEquals($this->fixturePath, $this->loader->filepath()); + } + + public function testSetFilepath() { + $this->Loader->setFilepath('/tmp/.env'); + $this->assertEquals('/tmp/.env', $this->Loader->filepath()); + } + + public function testParse() { + $this->Loader->parse(); + $environment = $this->Loader->toArray(); + + $this->assertEquals('bar', $environment['FOO']); + $this->assertEquals('baz', $environment['BAR']); + $this->assertEquals('with spaces', $environment['SPACED']); + $this->assertEquals('pgsql:host=localhost;dbname=test', $environment['EQUALS']); + $this->assertEquals('', $environment['NULL']); + } + + public function testParseExported() { + $this->Loader->setFilepath($this->fixturePath . 'exported.env'); + $this->Loader->parse(); + $environment = $this->Loader->toArray(); + + $this->assertEquals('bar', $environment['EFOO']); + $this->assertEquals('baz', $environment['EBAR']); + $this->assertEquals('with spaces', $environment['ESPACED']); + $this->assertEquals('pgsql:host=localhost;dbname=test', $environment['EEQUALS']); + $this->assertEquals('', $environment['ENULL']); + } + + public function testParseExported() { + $this->Loader->setFilepath($this->fixturePath . 'quoted.env'); + $this->Loader->parse(); + $environment = $this->Loader->toArray(); + + $this->assertEquals('bar', $environment['QFOO']); + $this->assertEquals('baz', $environment['QBAR']); + $this->assertEquals('with spaces', $environment['QSPACED']); + $this->assertEquals('pgsql:host=localhost;dbname=test', $environment['QEQUALS']); + $this->assertEquals('', $environment['QNULL']); + } + + public function testExpect() { + $this->Loader->parse(); + $this->assertTrue($this->Loader->expect('FOO')); + $this->assertTrue($this->Loader->expect(array('FOO', 'BAR'))); + } + + public function testDefine() { + $this->Loader->parse(); + $this->Loader->define(); + + $this->assertEquals('bar', FOO); + $this->assertEquals('baz', BAR); + $this->assertEquals('with spaces', SPACED); + $this->assertEquals('pgsql:host=localhost;dbname=test', EQUALS); + } + + public function testToEnv() { + $this->Loader->parse(); + $this->Loader->toEnv(); + + $this->assertEquals('bar', $_ENV['FOO']); + $this->assertEquals('baz', $_ENV['BAR']); + $this->assertEquals('with spaces', $_ENV['SPACED']); + $this->assertEquals('pgsql:host=localhost;dbname=test', $_ENV['EQUALS']); + $this->assertEquals('', $_ENV['NULL']); + } + + public function testToServer() { + $this->Loader->parse(); + $this->Loader->toServer(); + + $this->assertEquals('bar', $_SERVER['FOO']); + $this->assertEquals('baz', $_SERVER['BAR']); + $this->assertEquals('with spaces', $_SERVER['SPACED']); + $this->assertEquals('pgsql:host=localhost;dbname=test', $_SERVER['EQUALS']); + $this->assertEquals('', $_SERVER['NULL']); + } + + /** + * @expectedException LogicException + * @expectedExceptionMessage Environment must be parsed before calling expect() + */ + public function testExpectRequireException() { + $this->Loader->expect(); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage No arguments were passed to expect() + */ + public function testExpectArgumentException() { + $this->Loader->parse(); + $this->Loader->expect(); + } + + /** + * @expectedException RuntimeException + * @expectedExceptionMessage Required ENV vars missing: ['BAZ'] + */ + public function testExpectMissingRequiredException() { + $this->Loader->parse(); + $this->assertTrue($this->Loader->expect(array('BAZ'))); + } + + /** + * @expectedException LogicException + * @expectedExceptionMessage Environment must be parsed before calling define() + */ + public function testDefineRequireException() { + $this->Loader->define(); + } + + /** + * @expectedException LogicException + * @expectedExceptionMessage Key "NULL" has already been defined + */ + public function testDefineNullException() { + $this->Loader->parse(); + $this->Loader->define(); + } + + /** + * @expectedException LogicException + * @expectedExceptionMessage Environment must be parsed before calling toEnv() + */ + public function testToEnvRequireException() { + $this->Loader->toEnv(); + } + + /** + * @expectedException LogicException + * @expectedExceptionMessage Environment must be parsed before calling toServer() + */ + public function testToServerRequireException() { + $this->Loader->toServer(); + } + + /** + * @expectedException LogicException + * @expectedExceptionMessage Environment must be parsed before calling toArray() + */ + public function testToArrayException() { + $this->Loader->toArray(); + } }