diff --git a/tests/Composer/Test/Command/RequireCommandTest.php b/tests/Composer/Test/Command/RequireCommandTest.php index 0ab7508d0..d7cc95775 100644 --- a/tests/Composer/Test/Command/RequireCommandTest.php +++ b/tests/Composer/Test/Command/RequireCommandTest.php @@ -12,6 +12,7 @@ namespace Composer\Test\Command; +use Composer\Json\JsonFile; use Composer\Test\TestCase; use InvalidArgumentException; @@ -263,4 +264,91 @@ Using version 1.1.0 for required/pkg OUTPUT ]; } + + /** + * @dataProvider provideInconsistentRequireKeys + * @param bool $isDev + * @param bool $isInteractive + * @param string $expectedWarning + */ + public function testInconsistentRequireKeys(bool $isDev, bool $isInteractive, string $expectedWarning): void + { + $currentKey = $isDev ? "require" : "require-dev"; + $otherKey = $isDev ? "require-dev" : "require"; + + $dir = $this->initTempComposer([ + 'repositories' => [ + 'packages' => [ + 'type' => 'package', + 'package' => [ + ['name' => 'required/pkg', 'version' => '1.0.0'], + ], + ], + ], + $currentKey => [ + "required/pkg" => "^1.0", + ], + ]); + + $package = self::getPackage('required/pkg'); + if ($isDev) { + $this->createComposerLock([], [$package]); + $this->createInstalledJson([], [$package]); + } else { + $this->createComposerLock([$package], []); + $this->createInstalledJson([$package], []); + } + + $appTester = $this->getApplicationTester(); + $command = [ + 'command' => 'require', + '--no-audit' => true, + '--dev' => $isDev, + '--no-install' => true, + 'packages' => ['required/pkg'] + ]; + + if ($isInteractive) + $appTester->setInputs(['yes']); + else + $command['--no-interaction'] = true; + + $appTester->run($command); + + self::assertStringContainsString( + $expectedWarning, + $appTester->getDisplay(true) + ); + + $composer_content = (new JsonFile($dir . '/composer.json'))->read(); + self::assertArrayHasKey($otherKey, $composer_content); + self::assertArrayNotHasKey($currentKey, $composer_content); + } + + public function provideInconsistentRequireKeys(): \Generator + { + yield [ + true, + false, + 'required/pkg is currently present in the require key and you ran the command with the --dev flag, which will move it to the require-dev key.' + ]; + + yield [ + false, + false, + 'required/pkg is currently present in the require-dev key and you ran the command without the --dev flag, which will move it to the require key.' + ]; + + yield [ + true, + true, + 'required/pkg is currently present in the require key and you ran the command with the --dev flag, which will move it to the require-dev key.' + ]; + + yield [ + false, + true, + 'required/pkg is currently present in the require-dev key and you ran the command without the --dev flag, which will move it to the require key.' + ]; + } }