10000 Fix/broken merging of parameter bag env placeholders by mickadoo · Pull Request #20214 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Fix/broken merging of parameter bag env placeholders #20214

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

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public function getEnvPlaceholders()
*/
public function mergeEnvPlaceholders(self $bag)
{
$this->envPlaceholders = array_merge_recursive($this->envPlaceholders, $bag->getEnvPlaceholders());
if ($newPlaceholders = $bag->getEnvPlaceholders()) {
$this->envPlaceholders += $newPlaceholders;

foreach ($newPlaceholders as $env => $placeholders) {
$this->envPlaceholders[$env] += $placeholders;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,87 @@ public function testGetThrowsInvalidArgumentExceptionIfEnvNameContainsNonWordCha

public function testMergeWillNotDuplicateIdenticalParameters()
{
$originalPlaceholders = array('database_host' => array('localhost'));
$firstBag = new EnvPlaceholderParameterBag($originalPlaceholders);
$envVariableName = 'DB_HOST';
$parameter = sprintf('env(%s)', $envVariableName);
$firstBag = new EnvPlaceholderParameterBag();

// initialize placeholders
$firstBag->get('env(database_host)');
$firstBag->get($parameter);
$secondBag = clone $firstBag;

$firstBag->mergeEnvPlaceholders($secondBag);
$mergedPlaceholders = $firstBag->getEnvPlaceholders();

$this->assertCount(1, $mergedPlaceholders['database_host']);
$placeholderForVariable = $mergedPlaceholders[$envVariableName];
$placeholder = array_values($placeholderForVariable)[0];

$this->assertCount(1, $placeholderForVariable);
$this->assertInternalType('string', $placeholder);
$this->assertContains($envVariableName, $placeholder);
}

public function testMergeWhereFirstBagIsEmptyWillWork()
{
$envVariableName = 'DB_HOST';
$parameter = sprintf('env(%s)', $envVariableName);
$firstBag = new EnvPlaceholderParameterBag();
$secondBag = new EnvPlaceholderParameterBag();

// initialize placeholder only in second bag
$secondBag->get($parameter);

$this->assertEmpty($firstBag->getEnvPlaceholders());

$firstBag->mergeEnvPlaceholders($secondBag);
$mergedPlaceholders = $firstBag->getEnvPlaceholders();

$placeholderForVariable = $mergedPlaceholders[$envVariableName];
$placeholder = array_values($placeholderForVariable)[0];

$this->assertCount(1, $placeholderForVariable);
$this->assertInternalType('string', $placeholder);
$this->assertContains($envVariableName, $placeholder);
}

public function testMergeWherePlaceholderOnlyExistsInSecond()
{
$uniqueEnvName = 'DB_HOST';
$commonEnvName = 'DB_USER';

$uniqueParamName = sprintf('env(%s)', $uniqueEnvName);
$commonParamName = sprintf('env(%s)', $commonEnvName);

$firstBag = new EnvPlaceholderParameterBag();
// initialize common placeholder
$firstBag->get($commonParamName);
$secondBag = clone $firstBag;

// initialize unique placeholder
$secondBag->get($uniqueParamName);

$firstBag->mergeEnvPlaceholders($secondBag);
$merged = $firstBag->getEnvPlaceholders();

$this->assertCount(1, $merged[$uniqueEnvName]);
// second bag has same placeholder for commonEnvName
$this->assertCount(1, $merged[$commonEnvName]);
}

public function testMergeWithDifferentIdentifiersForPlaceholders()
{
$envName = 'DB_USER';
$paramName = sprintf('env(%s)', $envName);

$firstBag = new EnvPlaceholderParameterBag();
$secondBag = new EnvPlaceholderParameterBag();
// initialize placeholders
$firstPlaceholder = $firstBag->get($paramName);
$secondPlaceholder = $secondBag->get($paramName);

$firstBag->mergeEnvPlaceholders($secondBag);
$merged = $firstBag->getEnvPlaceholders();

$this->assertNotEquals($firstPlaceholder, $secondPlaceholder);
$this->assertCount(2, $merged[$envName]);
}
}
0