From 83ac3253abcbccc46201ca7728c669c4c37db315 Mon Sep 17 00:00:00 2001 From: Dimitri Sitchet Tomkeu Date: Wed, 1 Jun 2022 15:53:18 +0200 Subject: [PATCH] the decision whether or not to overwrite an existing schema is up to the user --- src/Configuration.php | 6 ++++-- tests/ConfigurationTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Configuration.php b/src/Configuration.php index c1b4874..b08d7f4 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -65,11 +65,13 @@ public function __construct(array $baseSchemas = []) * * @psalm-allow-private-mutation */ - public function addSchema(string $key, Schema $schema): void + public function addSchema(string $key, Schema $schema, bool $overwrite = true): void { $this->invalidate(); - $this->configSchemas[$key] = $schema; + if ($overwrite || !isset($this->configSchemas[$key])) { + $this->configSchemas[$key] = $schema; + } } /** diff --git a/tests/ConfigurationTest.php b/tests/ConfigurationTest.php index 08851d5..f2ff4b1 100644 --- a/tests/ConfigurationTest.php +++ b/tests/ConfigurationTest.php @@ -21,6 +21,30 @@ final class ConfigurationTest extends TestCase { + public function testNotOverrideSchema(): void + { + $config = new Configuration([ + 'a' => Expect::string()->default('foo') + ]); + + // the previous schema will not be modified + $config->addSchema('a', Expect::int()->default(4), false); + + $this->assertSame('foo', $config->get('a')); + } + + public function testOverrideSchema(): void + { + $config = new Configuration([ + 'a' => Expect::string()->default('foo') + ]); + + // the previous schema will be modified + $config->addSchema('a', Expect::int()->default(42)); + + $this->assertSame(42, $config->get('a')); + } + public function testAddSchema(): void { $config = new Configuration();