Skip to content

Fixing bug where removed parameters were not handled correctly during recipes update #942

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

Merged
merged 1 commit into from
Aug 7, 2022
Merged
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
50 changes: 32 additions & 18 deletions src/Configurator/ContainerConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,32 @@ public function unconfigure(Recipe $recipe, $parameters, Lock $lock)
{
$this->write('Unsetting parameters');
$target = $this->options->get('root-dir').'/'.$this->getServicesPath();
$lines = [];
foreach (file($target) as $line) {
if ($this->removeParameters(1, $parameters, $line)) {
continue;
}
$lines[] = $line;
}
$lines = $this->removeParametersFromLines(file($target), $parameters);
file_put_contents($target, implode('', $lines));
}

public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array $newConfig): void
{
if ($originalConfig) {
$recipeUpdate->setOriginalFile(
$this->getServicesPath(),
$this->configureParameters($originalConfig, true)
);
$recipeUpdate->setOriginalFile(
$this->getServicesPath(),
$this->configureParameters($originalConfig, true)
);

// for the new file, we need to update any values *and* remove any removed values
$removedParameters = [];
foreach ($originalConfig as $name => $value) {
if (!isset($newConfig[$name])) {
$removedParameters[$name] = $value;
}
}

if ($newConfig) {
$recipeUpdate->setNewFile(
$this->getServicesPath(),
$this->configureParameters($newConfig, true)
);
}
$updatedFile = $this->configureParameters($newConfig, true);
$lines = $this->removeParametersFromLines(explode("\n", $updatedFile), $removedParameters);

$recipeUpdate->setNewFile(
$this->getServicesPath(),
implode("\n", $lines)
);
}

private function configureParameters(array $parameters, bool $update = false): string
Expand Down Expand Up @@ -114,6 +115,19 @@ private function configureParameters(array $parameters, bool $update = false): s
return implode('', $lines);
}

private function removeParametersFromLines(array $sourceLines, array $parameters): array
{
$lines = [];
foreach ($sourceLines as $line) {
if ($this->removeParameters(1, $parameters, $line)) {
continue;
}
$lines[] = $line;
}

return $lines;
}

private function removeParameters($level, $params, $line)
{
foreach ($params as $key => $value) {
Expand Down
70 changes: 62 additions & 8 deletions tests/Configurator/ContainerConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testConfigure()
services:

EOF
, file_get_contents($config));
, file_get_contents($config));

$configurator->unconfigure($recipe, ['locale' => 'en'], $lock);
$this->assertEquals(<<<EOF
Expand All @@ -67,7 +67,7 @@ public function testConfigure()
services:

EOF
, file_get_contents($config));
, file_get_contents($config));
}

public function testConfigureWithoutParametersKey()
Expand Down Expand Up @@ -95,7 +95,7 @@ public function testConfigureWithoutParametersKey()
services:

EOF
, file_get_contents($config));
, file_get_contents($config));

$configurator->unconfigure($recipe, ['locale' => 'en'], $lock);
$this->assertEquals(<<<EOF
Expand All @@ -104,7 +104,7 @@ public function testConfigureWithoutParametersKey()
services:

EOF
, file_get_contents($config));
, file_get_contents($config));
}

public function testConfigureWithoutDuplicated()
Expand Down Expand Up @@ -135,7 +135,7 @@ public function testConfigureWithoutDuplicated()
services:

EOF
, file_get_contents($config));
, file_get_contents($config));

$configurator->unconfigure($recipe, ['locale' => 'en'], $lock);
$this->assertEquals(<<<EOF
Expand All @@ -144,7 +144,7 @@ public function testConfigureWithoutDuplicated()
services:

EOF
, file_get_contents($config));
, file_get_contents($config));
}

public function testConfigureWithComplexContent()
Expand Down Expand Up @@ -184,7 +184,7 @@ public function testConfigureWithComplexContent()
services:

EOF
, file_get_contents($config));
, file_get_contents($config));

$configurator->unconfigure($recipe, ['locale' => 'en', 'foobar' => 'baz'], $lock);
$this->assertEquals(<<<EOF
Expand All @@ -197,7 +197,7 @@ public function testConfigureWithComplexContent()
services:

EOF
, file_get_contents($config));
, file_get_contents($config));
}

public function testConfigureWithComplexContent2()
Expand Down Expand Up @@ -356,6 +356,60 @@ public function testUpdate()

services:

EOF
], $recipeUpdate->getNewFiles());
}

public function testUpdateWithNoRemovedKeysInUpdate()
{
$configurator = new ContainerConfigurator(
$this->createMock(Composer::class),
$this->createMock(IOInterface::class),
new Options(['config-dir' => 'config', 'root-dir' => FLEX_TEST_DIR])
);

$recipeUpdate = new RecipeUpdate(
$this->createMock(Recipe::class),
$this->createMock(Recipe::class),
$this->createMock(Lock::class),
FLEX_TEST_DIR
);

@mkdir(FLEX_TEST_DIR.'/config');
file_put_contents(
FLEX_TEST_DIR.'/config/services.yaml',
<<<EOF
parameters:
locale: es
something: else

services:
foo_router: '@router'
EOF
);

$configurator->update(
$recipeUpdate,
['locale' => 'en'],
[]
);

$this->assertSame(['config/services.yaml' => <<<EOF
parameters:
locale: en
something: else

services:
foo_router: '@router'
EOF
], $recipeUpdate->getOriginalFiles());

$this->assertSame(['config/services.yaml' => <<<EOF
parameters:
something: else

services:
foo_router: '@router'
EOF
], $recipeUpdate->getNewFiles());
}
Expand Down