Skip to content

Commit

Permalink
Merge pull request #32 from boesing/bugfix/update-lock-with-additiona…
Browse files Browse the repository at this point in the history
…l-options

bugfix: use proper methods to receive input option informations
  • Loading branch information
boesing authored Feb 15, 2021
2 parents 5c063cd + 0161b47 commit 3d6d6eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
32 changes: 25 additions & 7 deletions src/DependencyRewriterV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;

use function array_merge;
use function assert;
use function call_user_func;
use function dirname;
Expand Down Expand Up @@ -262,13 +263,10 @@ private function updateLockFile(): void
'--working-dir' => dirname($this->composerFile),
];

foreach (self::COMPOSER_LOCK_UPDATE_OPTIONS as $optionName) {
if (! $this->input->hasOption($optionName)) {
continue;
}

$input[sprintf('--%s', $optionName)] = $this->input->getOption($optionName);
}
$input = array_merge($input, $this->extractAdditionalInputOptionsFromInput(
$this->input,
self::COMPOSER_LOCK_UPDATE_OPTIONS
));

$application->run(new ArrayInput($input));
}
Expand Down Expand Up @@ -348,4 +346,24 @@ private function createComposerFile()
{
return new JsonFile($this->composerFile, null, $this->io);
}

/**
* @psalm-param list<non-empty-string> $options
* @psalm-return array<non-empty-string,mixed>
*/
private function extractAdditionalInputOptionsFromInput(InputInterface $input, array $options): array
{
$additionalInputOptions = [];
foreach ($options as $optionName) {
$option = sprintf('--%s', $optionName);
assert(! empty($option));
if (! $input->hasParameterOption($option, true)) {
continue;
}
/** @psalm-suppress MixedAssignment */
$additionalInputOptions[$option] = $input->getParameterOption($option, false, true);
}

return $additionalInputOptions;
}
}
9 changes: 5 additions & 4 deletions test/DependencyRewriterV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -970,26 +970,27 @@ public function testComposerOptionsArePassedToUpdateLockCommand(
$consecutiveGetOptionReturnValues = [];

foreach (DependencyRewriterV2::COMPOSER_LOCK_UPDATE_OPTIONS as $optionName) {
$consecutiveHasOptionArguments[] = [$optionName];
$option = sprintf('--%s', $optionName);
$consecutiveHasOptionArguments[] = [$option, true];
$passed = array_key_exists($optionName, $optionsPassedToComposer);
$consecutiveHasOptionReturnValues[] = $passed;
if (! $passed) {
continue;
}

$consecutiveGetOptionArguments[] = [$optionName];
$consecutiveGetOptionArguments[] = [$option, false, true];
$consecutiveGetOptionReturnValues[] = $optionsPassedToComposer[$optionName];
}

$input
->expects(self::exactly(count(DependencyRewriterV2::COMPOSER_LOCK_UPDATE_OPTIONS)))
->method('hasOption')
->method('hasParameterOption')
->withConsecutive(...$consecutiveHasOptionArguments)
->willReturnOnConsecutiveCalls(...$consecutiveHasOptionReturnValues);

$input
->expects(self::exactly(count($consecutiveGetOptionArguments)))
->method('getOption')
->method('getParameterOption')
->withConsecutive(...$consecutiveGetOptionArguments)
->willReturnOnConsecutiveCalls(...$consecutiveGetOptionReturnValues);

Expand Down

0 comments on commit 3d6d6eb

Please sign in to comment.