From b37f303e760403c9934d89a5b0dc3e6d88a47122 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 16 Jul 2017 09:09:11 +0200 Subject: [PATCH] Created CreateTagCommand --- module/CLI/config/cli.config.php | 1 + module/CLI/config/dependencies.config.php | 1 + .../CLI/src/Command/Api/DisableKeyCommand.php | 2 +- .../CLI/src/Command/Tag/CreateTagCommand.php | 69 +++++++++++++++++++ .../CLI/src/Command/Tag/ListTagsCommand.php | 4 +- .../test/Command/Tag/CreateTagCommandTest.php | 69 +++++++++++++++++++ 6 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 module/CLI/src/Command/Tag/CreateTagCommand.php create mode 100644 module/CLI/test/Command/Tag/CreateTagCommandTest.php diff --git a/module/CLI/config/cli.config.php b/module/CLI/config/cli.config.php index 53a5172b..14893d3d 100644 --- a/module/CLI/config/cli.config.php +++ b/module/CLI/config/cli.config.php @@ -18,6 +18,7 @@ return [ Command\Api\DisableKeyCommand::class, Command\Api\ListKeysCommand::class, Command\Tag\ListTagsCommand::class, + Command\Tag\CreateTagCommand::class, ] ], diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index 6795852d..11f0ce77 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -22,6 +22,7 @@ return [ Command\Api\DisableKeyCommand::class => AnnotatedFactory::class, Command\Api\ListKeysCommand::class => AnnotatedFactory::class, Command\Tag\ListTagsCommand::class => AnnotatedFactory::class, + Command\Tag\CreateTagCommand::class => AnnotatedFactory::class, ], ], diff --git a/module/CLI/src/Command/Api/DisableKeyCommand.php b/module/CLI/src/Command/Api/DisableKeyCommand.php index 738b8b43..48d9d564 100644 --- a/module/CLI/src/Command/Api/DisableKeyCommand.php +++ b/module/CLI/src/Command/Api/DisableKeyCommand.php @@ -32,7 +32,7 @@ class DisableKeyCommand extends Command { $this->apiKeyService = $apiKeyService; $this->translator = $translator; - parent::__construct(null); + parent::__construct(); } public function configure() diff --git a/module/CLI/src/Command/Tag/CreateTagCommand.php b/module/CLI/src/Command/Tag/CreateTagCommand.php new file mode 100644 index 00000000..97501847 --- /dev/null +++ b/module/CLI/src/Command/Tag/CreateTagCommand.php @@ -0,0 +1,69 @@ +tagService = $tagService; + $this->translator = $translator; + parent::__construct(); + } + + protected function configure() + { + $this + ->setName('tag:create') + ->setDescription($this->translator->translate('Creates one or more tags')) + ->addOption( + 'name', + 't', + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + $this->translator->translate('The name of the tags to create') + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $tagNames = $input->getOption('name'); + if (empty($tagNames)) { + $output->writeln(sprintf( + '%s', + $this->translator->translate('You have to provide at least one tag name') + )); + return; + } + + $this->tagService->createTags($tagNames); + $output->writeln($this->translator->translate('Created tags') . sprintf(': ["%s"]', implode( + '", "', + $tagNames + ))); + } +} diff --git a/module/CLI/src/Command/Tag/ListTagsCommand.php b/module/CLI/src/Command/Tag/ListTagsCommand.php index 201b09ae..3c781cb6 100644 --- a/module/CLI/src/Command/Tag/ListTagsCommand.php +++ b/module/CLI/src/Command/Tag/ListTagsCommand.php @@ -32,16 +32,16 @@ class ListTagsCommand extends Command */ public function __construct(TagServiceInterface $tagService, TranslatorInterface $translator) { - parent::__construct(); $this->tagService = $tagService; $this->translator = $translator; + parent::__construct(); } protected function configure() { $this ->setName('tag:list') - ->setDescription('Lists existing tags'); + ->setDescription($this->translator->translate('Lists existing tags')); } protected function execute(InputInterface $input, OutputInterface $output) diff --git a/module/CLI/test/Command/Tag/CreateTagCommandTest.php b/module/CLI/test/Command/Tag/CreateTagCommandTest.php new file mode 100644 index 00000000..413f0970 --- /dev/null +++ b/module/CLI/test/Command/Tag/CreateTagCommandTest.php @@ -0,0 +1,69 @@ +tagService = $this->prophesize(TagServiceInterface::class); + + $command = new CreateTagCommand($this->tagService->reveal(), Translator::factory([])); + $app = new Application(); + $app->add($command); + + $this->commandTester = new CommandTester($command); + } + + /** + * @test + */ + public function errorIsReturnedWhenNoTagsAreProvided() + { + $this->commandTester->execute([]); + + $output = $this->commandTester->getDisplay(); + $this->assertContains('You have to provide at least one tag name', $output); + } + + /** + * @test + */ + public function serviceIsInvokedOnSuccess() + { + $tagNames = ['foo', 'bar']; + $this->commandTester->execute([ + '--name' => $tagNames, + ]); + /** @var MethodProphecy $createTags */ + $createTags = $this->tagService->createTags($tagNames)->willReturn([]); + + $output = $this->commandTester->getDisplay(); + + $this->assertContains(sprintf('Created tags: ["%s"]', implode('", "', $tagNames)), $output); + $createTags->shouldHaveBeenCalled(); + } +}