From c3ae0c2eb59daa5c9c25e9bed123e451deba45aa Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Sat, 5 Feb 2022 00:34:35 +0100 Subject: [PATCH] New method `Config::getOrFail()` --- CHANGELOG.md | 6 ++++++ src/Config.php | 16 ++++++++++++++++ tests/ConfigTest.php | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6775b22..5cf621c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Config.php b/src/Config.php index ec6607e..6b40113 100644 --- a/src/Config.php +++ b/src/Config.php @@ -17,6 +17,7 @@ use ArrayObject; use Berlioz\Config\Adapter\AdapterInterface; use Berlioz\Config\ConfigFunction; +use Berlioz\Config\Exception\ConfigException; /** * Class Config. @@ -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 */ diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 8133357..3562c71 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -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; @@ -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( @@ -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')); }