Skip to content
This repository has been archived by the owner on Jun 12, 2022. It is now read-only.

Commit

Permalink
Updated how ConfigurationProvider::setConfig can load configs.
Browse files Browse the repository at this point in the history
Added a new test for a bad JSON config string.
  • Loading branch information
warrickbayman committed Mar 17, 2020
1 parent 3f8c6d6 commit bb2fc14
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
12 changes: 7 additions & 5 deletions src/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', []));
Expand Down
12 changes: 11 additions & 1 deletion tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]}');
}

/**
Expand Down

0 comments on commit bb2fc14

Please sign in to comment.