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

Add if-not-set modifier #81

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
24 changes: 24 additions & 0 deletions Model/Processor/ImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,37 @@ public function process()
continue;
}

if ($this->shouldSkipConfig($configPath, $scopeType, $scopeId, $value)) {
$this->getOutput()->writeln(sprintf('<comment>[%s] [%s] %s => %s</comment>', $scopeType, $scopeId, $configPath, 'SKIPPED'));

continue;
}

$this->configWriter->save($configPath, $value, $scopeType, $scopeId);
$this->getOutput()->writeln(sprintf('<comment>[%s] [%s] %s => %s</comment>', $scopeType, $scopeId, $configPath, $value));
}
}
}
}

/**
* @param string $configPath
* @param string $scopeType
* @param int $scopeId
* @param mixed $value
*
* @return bool
*/
private function shouldSkipConfig($configPath, $scopeType, $scopeId, $value)
{
if (is_array($value) && (isset($value['if-not-set']) || (isset($value['if']) && $value['if'] === 'not-set'))) {
$existingValue = $this->configWriter->get($configPath, $scopeType, $scopeId);
return $existingValue !== null;
}

return false;
}

/**
* @param array $files
*
Expand Down
148 changes: 148 additions & 0 deletions Test/Unit/Model/Processor/ImportProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,152 @@ public function process(): void
$processor->setReader($readerMock);
$processor->process();
}

/**
* @test
*/
public function processWithIfNotSetModifier(): void
{
$finderMock = $this->getMockBuilder(Finder::class)
->onlyMethods(['find'])
->getMock();
$finderMock->expects($this->once())->method('find')->willReturn(['abc.yaml']);

$parseResult = [
'test/config/custom_field_if_not_set' => [
'default' => [
0 => [
'if-not-set' => true,
'default' => 1,
],
],
],
];

$readerMock = $this->getMockBuilder(YamlReader::class)
->onlyMethods(['parse'])
->getMock();
$readerMock->expects($this->once())->method('parse')->willReturn($parseResult);

$this->scopeValidatorMock->expects($this->once())->method('validate')->willReturn(true);
$this->configWriterMock->expects($this->once())->method('save');
$this->configWriterMock->expects($this->once())->method('get')->willReturn(null);

$processor = new ImportProcessor($this->configWriterMock, $this->scopeValidatorMock, $this->scopeConverterMock, []);
$processor->setOutput($this->outputMock);
$processor->setFinder($finderMock);
$processor->setReader($readerMock);
$processor->process();
}

/**
* @test
*/
public function processWithIfNotSetModifierAndExistingValue(): void
{
$finderMock = $this->getMockBuilder(Finder::class)
->onlyMethods(['find'])
->getMock();
$finderMock->expects($this->once())->method('find')->willReturn(['abc.yaml']);

$parseResult = [
'test/config/custom_field_if_not_set' => [
'default' => [
0 => [
'if-not-set' => true,
'default' => 1,
],
],
],
];

$readerMock = $this->getMockBuilder(YamlReader::class)
->onlyMethods(['parse'])
->getMock();
$readerMock->expects($this->once())->method('parse')->willReturn($parseResult);

$this->scopeValidatorMock->expects($this->once())->method('validate')->willReturn(true);
$this->configWriterMock->expects($this->never())->method('save');
$this->configWriterMock->expects($this->once())->method('get')->willReturn('existing_value');

$processor = new ImportProcessor($this->configWriterMock, $this->scopeValidatorMock, $this->scopeConverterMock, []);
$processor->setOutput($this->outputMock);
$processor->setFinder($finderMock);
$processor->setReader($readerMock);
$processor->process();
}

/**
* @test
*/
public function processWithIfNotSetModifierAlternativeSyntax(): void
{
$finderMock = $this->getMockBuilder(Finder::class)
->onlyMethods(['find'])
->getMock();
$finderMock->expects($this->once())->method('find')->willReturn(['abc.yaml']);

$parseResult = [
'test/config/custom_field_if_not_set' => [
'default' => [
0 => [
'if' => 'not-set',
'default' => 1,
],
],
],
];

$readerMock = $this->getMockBuilder(YamlReader::class)
->onlyMethods(['parse'])
->getMock();
$readerMock->expects($this->once())->method('parse')->willReturn($parseResult);

$this->scopeValidatorMock->expects($this->once())->method('validate')->willReturn(true);
$this->configWriterMock->expects($this->once())->method('save');
$this->configWriterMock->expects($this->once())->method('get')->willReturn(null);

$processor = new ImportProcessor($this->configWriterMock, $this->scopeValidatorMock, $this->scopeConverterMock, []);
$processor->setOutput($this->outputMock);
$processor->setFinder($finderMock);
$processor->setReader($readerMock);
$processor->process();
}

/**
* @test
*/
public function processWithIfNotSetModifierAlternativeSyntaxAndExistingValue(): void
{
$finderMock = $this->getMockBuilder(Finder::class)
->onlyMethods(['find'])
->getMock();
$finderMock->expects($this->once())->method('find')->willReturn(['abc.yaml']);

$parseResult = [
'test/config/custom_field_if_not_set' => [
'default' => [
0 => [
'if' => 'not-set',
'default' => 1,
],
],
],
];

$readerMock = $this->getMockBuilder(YamlReader::class)
->onlyMethods(['parse'])
->getMock();
$readerMock->expects($this->once())->method('parse')->willReturn($parseResult);

$this->scopeValidatorMock->expects($this->once())->method('validate')->willReturn(true);
$this->configWriterMock->expects($this->never())->method('save');
$this->configWriterMock->expects($this->once())->method('get')->willReturn('existing_value');

$processor = new ImportProcessor($this->configWriterMock, $this->scopeValidatorMock, $this->scopeConverterMock, []);
$processor->setOutput($this->outputMock);
$processor->setFinder($finderMock);
$processor->setReader($readerMock);
$processor->process();
}
}
22 changes: 22 additions & 0 deletions docs/config-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,28 @@ vendorx/general/api_key:

This is helpful when you've got the same settings across different environments but want to keep one environment ( `X` env) unchanged without showing the exact value in the config file. It's a common scenario, especially when dealing with sensitive data. You really should only keep that kind of info in the environment’s database, not in your GIT repo.

### If-Not-Set Modifier

To conditionally set a configuration value only if it has not been set previously, use the `if-not-set` or `if: not-set` modifier. This is useful for setting initial configuration values for third-party extensions.

For example, this might be the content of your config file:

```
path/to/config:
if-not-set: true
default:
0: 1
```

Or

```
path/to/config:
if: not-set
default:
0: 1
```

### Recursive folder setup

If you choose to store your configuration files in subdirectories, e.g. per vendor, the recommended folder setup should look like this:
Expand Down