Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the decision whether or not to overwrite an existing schema is up to the user #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down