Skip to content

Commit

Permalink
New method Config::getOrFail()
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Feb 4, 2022
1 parent 00fe47e commit c3ae0c2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [2.1.0] - 2022-02-05

### Added

- New method `Config::getOrFail()`

## [2.0.0] - 2021-09-08

### Added
Expand Down
16 changes: 16 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ArrayObject;
use Berlioz\Config\Adapter\AdapterInterface;
use Berlioz\Config\ConfigFunction;
use Berlioz\Config\Exception\ConfigException;

/**
* Class Config.
Expand Down Expand Up @@ -94,6 +95,21 @@ public function addConfig(AdapterInterface ...$config): void
usort($this->configs, fn($config1, $config2) => $config2->getPriority() <=> $config1->getPriority());
}

/**
* Get value or fail.
*
* Key given in parameter must be in format: key.key2.key3
*
* @param string $key
*
* @return mixed
* @throws ConfigException
*/
public function getOrFail(string $key): mixed
{
return $this->get($key) ?: throw new ConfigException(sprintf('Missing configuration value at "%s"', $key));
}

/**
* @inheritDoc
*/
Expand Down
32 changes: 30 additions & 2 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
namespace Berlioz\Config\Tests;

use ArrayObject;
use Berlioz\Config\Adapter\ArrayAdapter;
use Berlioz\Config\Adapter\JsonAdapter;
use Berlioz\Config\Config;
use Berlioz\Config\ConfigFunction\EnvFunction;
use Berlioz\Config\Exception\ConfigException;
use Berlioz\Config\Tests\ConfigFunction\FakeFunction;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -78,6 +78,31 @@ public function testAddConfig()
$this->assertSame(array_merge([$aPrioritizedConfig], $configs, [$aDefaultConfig]), $config->all());
}

public function testGetOrFail_valid()
{
$config = new Config([new JsonAdapter(__DIR__ . '/config.json5', true)]);

$this->assertEquals('STRING', $config->getOrFail('bar'));
}

public function testGetOrFail_empty()
{
$this->expectException(ConfigException::class);
$this->expectDeprecationMessage('Missing configuration value at "section2.baz"');

$config = new Config([new JsonAdapter(__DIR__ . '/config.json5', true)]);
$config->getOrFail('section2.baz');
}

public function testGetOrFail_unknown()
{
$this->expectException(ConfigException::class);
$this->expectDeprecationMessage('Missing configuration value at "baz.unknown.foo"');

$config = new Config([new JsonAdapter(__DIR__ . '/config.json5', true)]);
$config->getOrFail('baz.unknown.foo');
}

public function testGet()
{
$config = new FakeConfig(
Expand All @@ -95,7 +120,10 @@ public function testGet()
$this->assertEquals('value-test', $config->get('section2.bar'));
$this->assertSame(123456, $config->get('section2.qux'));
$this->assertEquals(['value2', '{not}', 'QUX QUX QUX', 'QUX QUX QUX'], $config->get('section.qux'));
$this->assertEquals(['bar' => 'value-test', 'baz' => 123456, 'qux' => 123456, '123' => 'FOO'], $config->get('section2'));
$this->assertEquals(
['bar' => 'value-test', 'baz' => 123456, 'qux' => 123456, '123' => 'FOO'],
$config->get('section2')
);
$this->assertSame(true, $config->get('baz'));
}

Expand Down

0 comments on commit c3ae0c2

Please sign in to comment.