diff --git a/CHANGELOG.md b/CHANGELOG.md index 9af42e5..656be4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [0.3.2] 17-03-2020 +### Changed +* Changed how the `ConfigurationProvider::setConfig` method loads config. +* Added some new tests around the loading of config files. + ## [0.3.1] 17-03-2020 ### Added * Added a new more tests. diff --git a/src/ConfigurationProvider.php b/src/ConfigurationProvider.php index 65e4260..b17dc70 100644 --- a/src/ConfigurationProvider.php +++ b/src/ConfigurationProvider.php @@ -32,17 +32,19 @@ public function loadConfigFile(string $filename): void } try { - $config = json_decode(file_get_contents($filename), true, 512, JSON_THROW_ON_ERROR); - - $this->setConfig($config); + $this->setConfig(file_get_contents($filename)); } catch (\Exception $e) { - throw new \JsonException('Unable to read config file.'."\n".$e->getMessage()); + throw new \JsonException('Unable to read config.'."\n".$e->getMessage()); exit(1); } } - public function setConfig(array $config): void + public function setConfig($config): void { + if (is_string($config)) { + $config = json_decode($config, true, 512, JSON_THROW_ON_ERROR); + } + $this->repository = Arr::get($config, 'repository'); $this->loadServers(Arr::get($config, 'servers'), Arr::get($config, 'common', [])); diff --git a/tests/ConfigurationTest.php b/tests/ConfigurationTest.php index 6e6dcc8..f2b4566 100644 --- a/tests/ConfigurationTest.php +++ b/tests/ConfigurationTest.php @@ -50,7 +50,17 @@ public function it_will_throw_an_exception_if_the_server_key_doesnt_exist() public function it_will_throw_an_exception_if_it_cannot_read_the_config_file() { $this->expectException(FileNotFoundException::class); - $config = (new ConfigurationProvider('no-such-config.json')); + (new ConfigurationProvider('no-such-config.json')); + } + + /** + * @test + */ + public function it_will_throw_an_exception_if_the_config_is_invalid() + { + $this->expectException(\JsonException::class); + $config = new ConfigurationProvider(); + $config->setConfig('{"repository": "repo", "servers": ["server1": "bad-server]}'); } /**