diff --git a/src/ArrayConfig.php b/src/ArrayConfig.php index 98896f2..06b3180 100644 --- a/src/ArrayConfig.php +++ b/src/ArrayConfig.php @@ -2,7 +2,7 @@ namespace Stefna\Config; -final class ArrayConfig implements Config +final class ArrayConfig implements Config, ExportableConfig { use GetConfigTrait; @@ -37,4 +37,9 @@ public function getRawValue(string $key): mixed } return $root; } + + public function getArrayCopy(): array + { + return $this->config; + } } diff --git a/src/ExportableConfig.php b/src/ExportableConfig.php new file mode 100644 index 0000000..b522789 --- /dev/null +++ b/src/ExportableConfig.php @@ -0,0 +1,11 @@ + + */ + public function getArrayCopy(): array; +} diff --git a/src/FileConfig.php b/src/FileConfig.php index 466f487..169c6ea 100644 --- a/src/FileConfig.php +++ b/src/FileConfig.php @@ -4,7 +4,7 @@ use Stefna\Config\Exception\FileNotFound; -final class FileConfig implements Config +final class FileConfig implements Config, ExportableConfig { use GetConfigTrait; @@ -15,16 +15,26 @@ public function __construct( ) {} public function getRawValue(string $key): mixed + { + $this->loadFile(); + return $this->loadedConfig->getRawValue($key); + } + + public function getArrayCopy(): array + { + $this->loadFile(); + return $this->loadedConfig->getArrayCopy(); + } + + private function loadFile(): void { if (isset($this->loadedConfig)) { - return $this->loadedConfig->getRawValue($key); + return; } if (!file_exists($this->configFile)) { throw new FileNotFound('Configuration not found: ' . $this->configFile); } $this->loadedConfig = ArrayConfig::fromArray(require $this->configFile); - - return $this->loadedConfig->getRawValue($key); } } diff --git a/tests/AbstractConfigTestCase.php b/tests/AbstractConfigTestCase.php index 3275aaa..656e479 100644 --- a/tests/AbstractConfigTestCase.php +++ b/tests/AbstractConfigTestCase.php @@ -5,6 +5,7 @@ use PHPUnit\Framework\TestCase; use Stefna\Config\ArrayConfig; use Stefna\Config\Config; +use Stefna\Config\ExportableConfig; use Stefna\Config\Tests\Stub\TestStub; abstract class AbstractConfigTestCase extends TestCase @@ -117,9 +118,22 @@ public function testDotKeyReturnEmptyOnExtraDots(): void $this->assertNull($config->get('.nesting')); } - protected function getDefaultArrayConfig(): ArrayConfig + public function testExportableConfig(): void + { + $config = $this->getConfig(); + if (!$config instanceof ExportableConfig) { + $this->markTestSkipped('Config does not implement ExportableConfig'); + } + + $this->assertSame($this->getDefaultConfigAsArray(), $config->getArrayCopy()); + } + + /** + * @return array + */ + protected function getDefaultConfigAsArray(): array { - return new ArrayConfig([ + return [ 'testString' => 'string', 'testNumberAsString' => '43', 'testNumber' => 42, @@ -140,7 +154,13 @@ protected function getDefaultArrayConfig(): ArrayConfig 'value' => 2, ], ], - ]); + ]; + } + + + protected function getDefaultArrayConfig(): ArrayConfig + { + return new ArrayConfig($this->getDefaultConfigAsArray()); } abstract protected function getConfig(): Config;