diff --git a/src/Commands/AddCommand.php b/src/Commands/AddCommand.php index 6c3e0fa..1b67dec 100644 --- a/src/Commands/AddCommand.php +++ b/src/Commands/AddCommand.php @@ -26,15 +26,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int { $output->writeln('You are using Composer Plug and Play Plugin.'); - if (file_exists(PlugAndPlayInterface::PACKAGES_FILE) === false) { - $output->writeln('The [' . PlugAndPlayInterface::PACKAGES_FILE . '] file not exists.'); + $json = []; - return 1; - } - - $composer = file_get_contents(PlugAndPlayInterface::PACKAGES_FILE); + if (file_exists(PlugAndPlayInterface::PACKAGES_FILE)) { + $composer = file_get_contents(PlugAndPlayInterface::PACKAGES_FILE); - $json = json_decode($composer, true); + $json = json_decode($composer, true); + } $json['require'][$input->getArgument('package')] = $input->getArgument('version'); diff --git a/tests/Unit/Commands/AddCommandTest.php b/tests/Unit/Commands/AddCommandTest.php index 2ae6772..12aee82 100644 --- a/tests/Unit/Commands/AddCommandTest.php +++ b/tests/Unit/Commands/AddCommandTest.php @@ -72,12 +72,16 @@ public function testWithVersion(): void $this->assertJsonStringEqualsJsonFile($this->packagesFile(), $expected); } - public function testFileNotExists(): void + public function testMergeJsonFiles(): void { - $output = $this->runCommand('plug-and-play:add dex/extra'); + $this->runCommand('plug-and-play:add dex/extra'); - $message = 'The [' . PlugAndPlayInterface::PACKAGES_FILE . '] file not exists.'; + $expected = $this->encodeContent([ + 'require' => [ + 'dex/extra' => '*', + ], + ]); - $this->assertStringContainsString($message, $output->fetch()); + $this->assertJsonStringEqualsJsonFile($this->packagesFile(), $expected); } }