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();