Skip to content

Commit

Permalink
Merge pull request #34 from edersoares/add-command
Browse files Browse the repository at this point in the history
Command `plug-and-play:add`
  • Loading branch information
edersoares authored Sep 23, 2022
2 parents a8caba0 + e91008c commit 7ea0281
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Commands/AddCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Dex\Composer\PlugAndPlay\Commands;

use Composer\Command\BaseCommand;
use Dex\Composer\PlugAndPlay\PlugAndPlayInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class AddCommand extends BaseCommand
{
use CommandNaming, ComposerCreator;

protected function configure(): void
{
parent::configure();

$this->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('<info>You are using Composer Plug and Play Plugin.</info>');

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;
}
}
2 changes: 2 additions & 0 deletions src/PlugAndPlayPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,6 +45,7 @@ public function getCommands(): array
new InstallCommand(),
new UpdateCommand(),
new DumpAutoloadCommand(),
new AddCommand(),
new InitCommand(),
];
}
Expand Down
36 changes: 36 additions & 0 deletions tests/CommandTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Dex\Composer\PlugAndPlay\Tests;

use Dex\Composer\PlugAndPlay\PlugAndPlayInterface;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;

abstract class CommandTestCase extends TestCase
{
protected string $directory = __DIR__ . '/../../tmp/';

protected function runCommand(string $command): BufferedOutput
{
$application = new Application();
$input = new StringInput("$command -d $this->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;
}
}
83 changes: 83 additions & 0 deletions tests/Unit/Commands/AddCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Dex\Composer\PlugAndPlay\Tests\Unit\Commands;

use Dex\Composer\PlugAndPlay\PlugAndPlayInterface;
use Dex\Composer\PlugAndPlay\Tests\CommandTestCase;

class AddCommandTest extends CommandTestCase
{
protected function setUp(): void
{
$composer = json_encode([
'name' => '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());
}
}

0 comments on commit 7ea0281

Please sign in to comment.