-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from edersoares/add-command
Command `plug-and-play:add`
- Loading branch information
Showing
4 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |