diff --git a/src/Commands/AddCommand.php b/src/Commands/AddCommand.php new file mode 100644 index 0000000..6c3e0fa --- /dev/null +++ b/src/Commands/AddCommand.php @@ -0,0 +1,47 @@ +naming('plug-and-play:add'); + $this->setDescription('Add a required package to install using plug and play plugin.'); + $this->addArgument('package', InputArgument::REQUIRED, 'Package to add in plug and play dependencies'); + $this->addArgument('version', InputArgument::OPTIONAL, 'Version of the package', '*'); + } + + 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.'); + + return 1; + } + + $composer = file_get_contents(PlugAndPlayInterface::PACKAGES_FILE); + + $json = json_decode($composer, true); + + $json['require'][$input->getArgument('package')] = $input->getArgument('version'); + + $json = json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + + file_put_contents(PlugAndPlayInterface::PACKAGES_FILE, $json); + + return 0; + } +} diff --git a/src/PlugAndPlayPlugin.php b/src/PlugAndPlayPlugin.php index 4ee019a..189e668 100644 --- a/src/PlugAndPlayPlugin.php +++ b/src/PlugAndPlayPlugin.php @@ -7,6 +7,7 @@ use Composer\Plugin\Capability\CommandProvider; use Composer\Plugin\Capable; use Composer\Plugin\PluginInterface; +use Dex\Composer\PlugAndPlay\Commands\AddCommand; use Dex\Composer\PlugAndPlay\Commands\DumpAutoloadCommand; use Dex\Composer\PlugAndPlay\Commands\InitCommand; use Dex\Composer\PlugAndPlay\Commands\InstallCommand; @@ -44,6 +45,7 @@ public function getCommands(): array new InstallCommand(), new UpdateCommand(), new DumpAutoloadCommand(), + new AddCommand(), new InitCommand(), ]; } diff --git a/tests/CommandTestCase.php b/tests/CommandTestCase.php new file mode 100644 index 0000000..ac22037 --- /dev/null +++ b/tests/CommandTestCase.php @@ -0,0 +1,36 @@ +directory"); + $output = new BufferedOutput(); + + $application->doRun($input, $output); + + return $output; + } + + protected function encodeContent(array $data): string + { + return json_encode( + $data, + JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE + ); + } + + protected function packagesFile(): string + { + return $this->directory . PlugAndPlayInterface::PACKAGES_FILE; + } +} diff --git a/tests/Unit/Commands/AddCommandTest.php b/tests/Unit/Commands/AddCommandTest.php new file mode 100644 index 0000000..2ae6772 --- /dev/null +++ b/tests/Unit/Commands/AddCommandTest.php @@ -0,0 +1,83 @@ + 'dex/test', + 'config' => [ + 'allow-plugins' => [ + 'dex/composer-plug-and-play' => true, + 'dex/fake' => true, + ], + ], + ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + + mkdir($this->directory); + mkdir($this->directory . PlugAndPlayInterface::PACKAGES_PATH); + file_put_contents($this->directory . 'composer.json', $composer); + } + + protected function createPackagesComposerJson(): void + { + $base = json_encode([ + 'minimum-stability' => 'dev', + ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + + file_put_contents($this->packagesFile(), $base); + } + + protected function tearDown(): void + { + exec('rm -r ' . $this->directory); + } + + public function test(): void + { + $this->createPackagesComposerJson(); + + $this->runCommand('plug-and-play:add dex/extra'); + + $expected = $this->encodeContent([ + 'minimum-stability' => 'dev', + 'require' => [ + 'dex/extra' => '*', + ], + ]); + + $this->assertFileExists($this->packagesFile()); + $this->assertJsonStringEqualsJsonFile($this->packagesFile(), $expected); + } + + public function testWithVersion(): void + { + $this->createPackagesComposerJson(); + + $this->runCommand('plug-and-play:add dex/extra 1.0'); + + $expected = $this->encodeContent([ + 'minimum-stability' => 'dev', + 'require' => [ + 'dex/extra' => '1.0', + ], + ]); + + $this->assertFileExists($this->packagesFile()); + $this->assertJsonStringEqualsJsonFile($this->packagesFile(), $expected); + } + + public function testFileNotExists(): void + { + $output = $this->runCommand('plug-and-play:add dex/extra'); + + $message = 'The [' . PlugAndPlayInterface::PACKAGES_FILE . '] file not exists.'; + + $this->assertStringContainsString($message, $output->fetch()); + } +}