mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-27 16:26:37 +03:00
Created DeleteTagsCommand
This commit is contained in:
parent
602e11d5e7
commit
a138f4153d
4 changed files with 139 additions and 0 deletions
|
@ -21,6 +21,7 @@ return [
|
||||||
Command\Tag\ListTagsCommand::class,
|
Command\Tag\ListTagsCommand::class,
|
||||||
Command\Tag\CreateTagCommand::class,
|
Command\Tag\CreateTagCommand::class,
|
||||||
Command\Tag\RenameTagCommand::class,
|
Command\Tag\RenameTagCommand::class,
|
||||||
|
Command\Tag\DeleteTagsCommand::class,
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ return [
|
||||||
Command\Tag\ListTagsCommand::class => AnnotatedFactory::class,
|
Command\Tag\ListTagsCommand::class => AnnotatedFactory::class,
|
||||||
Command\Tag\CreateTagCommand::class => AnnotatedFactory::class,
|
Command\Tag\CreateTagCommand::class => AnnotatedFactory::class,
|
||||||
Command\Tag\RenameTagCommand::class => AnnotatedFactory::class,
|
Command\Tag\RenameTagCommand::class => AnnotatedFactory::class,
|
||||||
|
Command\Tag\DeleteTagsCommand::class => AnnotatedFactory::class,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
||||||
|
|
69
module/CLI/src/Command/Tag/DeleteTagsCommand.php
Normal file
69
module/CLI/src/Command/Tag/DeleteTagsCommand.php
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
<?php
|
||||||
|
namespace Shlinkio\Shlink\CLI\Command\Tag;
|
||||||
|
|
||||||
|
use Acelaya\ZsmAnnotatedServices\Annotation as DI;
|
||||||
|
use Shlinkio\Shlink\Core\Service\Tag\TagService;
|
||||||
|
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
use Zend\I18n\Translator\Translator;
|
||||||
|
use Zend\I18n\Translator\TranslatorInterface;
|
||||||
|
|
||||||
|
class DeleteTagsCommand extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var TagServiceInterface
|
||||||
|
*/
|
||||||
|
private $tagService;
|
||||||
|
/**
|
||||||
|
* @var TranslatorInterface
|
||||||
|
*/
|
||||||
|
private $translator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ListTagsCommand constructor.
|
||||||
|
* @param TagServiceInterface $tagService
|
||||||
|
* @param TranslatorInterface $translator
|
||||||
|
*
|
||||||
|
* @DI\Inject({TagService::class, Translator::class})
|
||||||
|
*/
|
||||||
|
public function __construct(TagServiceInterface $tagService, TranslatorInterface $translator)
|
||||||
|
{
|
||||||
|
$this->tagService = $tagService;
|
||||||
|
$this->translator = $translator;
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configure()
|
||||||
|
{
|
||||||
|
$this
|
||||||
|
->setName('tag:delete')
|
||||||
|
->setDescription($this->translator->translate('Deletes one or more tags.'))
|
||||||
|
->addOption(
|
||||||
|
'name',
|
||||||
|
't',
|
||||||
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
|
||||||
|
$this->translator->translate('The name of the tags to delete')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
|
{
|
||||||
|
$tagNames = $input->getOption('name');
|
||||||
|
if (empty($tagNames)) {
|
||||||
|
$output->writeln(sprintf(
|
||||||
|
'<comment>%s</comment>',
|
||||||
|
$this->translator->translate('You have to provide at least one tag name')
|
||||||
|
));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->tagService->deleteTags($tagNames);
|
||||||
|
$output->writeln($this->translator->translate('Deleted tags') . sprintf(': ["<info>%s</info>"]', implode(
|
||||||
|
'</info>", "<info>',
|
||||||
|
$tagNames
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
68
module/CLI/test/Command/Tag/DeleteTagsCommandTest.php
Normal file
68
module/CLI/test/Command/Tag/DeleteTagsCommandTest.php
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
<?php
|
||||||
|
namespace ShlinkioTest\Shlink\CLI\Command\Tag;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Prophecy\Prophecy\MethodProphecy;
|
||||||
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
|
use Shlinkio\Shlink\CLI\Command\Tag\DeleteTagsCommand;
|
||||||
|
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
|
||||||
|
use Symfony\Component\Console\Application;
|
||||||
|
use Symfony\Component\Console\Tester\CommandTester;
|
||||||
|
use Zend\I18n\Translator\Translator;
|
||||||
|
|
||||||
|
class DeleteTagsCommandTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var DeleteTagsCommand
|
||||||
|
*/
|
||||||
|
private $command;
|
||||||
|
/**
|
||||||
|
* @var CommandTester
|
||||||
|
*/
|
||||||
|
private $commandTester;
|
||||||
|
/**
|
||||||
|
* @var ObjectProphecy
|
||||||
|
*/
|
||||||
|
private $tagService;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->tagService = $this->prophesize(TagServiceInterface::class);
|
||||||
|
|
||||||
|
$command = new DeleteTagsCommand($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'];
|
||||||
|
/** @var MethodProphecy $deleteTags */
|
||||||
|
$deleteTags = $this->tagService->deleteTags($tagNames)->will(function () {
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->commandTester->execute([
|
||||||
|
'--name' => $tagNames,
|
||||||
|
]);
|
||||||
|
$output = $this->commandTester->getDisplay();
|
||||||
|
|
||||||
|
$this->assertContains(sprintf('Deleted tags: ["%s"]', implode('", "', $tagNames)), $output);
|
||||||
|
$deleteTags->shouldHaveBeenCalled();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue