From 2b89556c0959365c2c97c7f467ee21ea0dbdad3c Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 21 Aug 2016 10:17:17 +0200 Subject: [PATCH] Allowed to display tags in the shortcode:list command --- .../Shortcode/ListShortcodesCommand.php | 30 +++++++++++++++++-- .../Shortcode/ListShortcodesCommandTest.php | 17 +++++++++++ module/Core/src/Entity/ShortUrl.php | 2 ++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/module/CLI/src/Command/Shortcode/ListShortcodesCommand.php b/module/CLI/src/Command/Shortcode/ListShortcodesCommand.php index d57d3311..a3f77903 100644 --- a/module/CLI/src/Command/Shortcode/ListShortcodesCommand.php +++ b/module/CLI/src/Command/Shortcode/ListShortcodesCommand.php @@ -55,12 +55,20 @@ class ListShortcodesCommand extends Command PaginableRepositoryAdapter::ITEMS_PER_PAGE ), 1 + ) + ->addOption( + 'tags', + 't', + InputOption::VALUE_NONE, + $this->translator->translate('Whether to display the tags or not') ); } public function execute(InputInterface $input, OutputInterface $output) { $page = intval($input->getOption('page')); + $showTags = $input->getOption('tags'); + /** @var QuestionHelper $helper */ $helper = $this->getHelper('question'); @@ -68,15 +76,31 @@ class ListShortcodesCommand extends Command $result = $this->shortUrlService->listShortUrls($page); $page++; $table = new Table($output); - $table->setHeaders([ + + $headers = [ $this->translator->translate('Short code'), $this->translator->translate('Original URL'), $this->translator->translate('Date created'), $this->translator->translate('Visits count'), - ]); + ]; + if ($showTags) { + $headers[] = $this->translator->translate('Tags'); + } + $table->setHeaders($headers); foreach ($result as $row) { - $table->addRow(array_values($row->jsonSerialize())); + $shortUrl = $row->jsonSerialize(); + if ($showTags) { + $shortUrl['tags'] = []; + foreach ($row->getTags() as $tag) { + $shortUrl['tags'][] = $tag->getName(); + } + $shortUrl['tags'] = implode(', ', $shortUrl['tags']); + } else { + unset($shortUrl['tags']); + } + + $table->addRow(array_values($shortUrl)); } $table->render(); diff --git a/module/CLI/test/Command/Shortcode/ListShortcodesCommandTest.php b/module/CLI/test/Command/Shortcode/ListShortcodesCommandTest.php index d8e4a685..a426eb06 100644 --- a/module/CLI/test/Command/Shortcode/ListShortcodesCommandTest.php +++ b/module/CLI/test/Command/Shortcode/ListShortcodesCommandTest.php @@ -108,6 +108,23 @@ class ListShortcodesCommandTest extends TestCase ]); } + /** + * @test + */ + public function ifTagsFlagIsProvidedTagsColumnIsIncluded() + { + $this->questionHelper->setInputStream($this->getInputStream('\n')); + $this->shortUrlService->listShortUrls(1)->willReturn(new Paginator(new ArrayAdapter())) + ->shouldBeCalledTimes(1); + + $this->commandTester->execute([ + 'command' => 'shortcode:list', + '--tags' => true, + ]); + $output = $this->commandTester->getDisplay(); + $this->assertTrue(strpos($output, 'Tags') > 0); + } + protected function getInputStream($inputData) { $stream = fopen('php://memory', 'r+', false); diff --git a/module/Core/src/Entity/ShortUrl.php b/module/Core/src/Entity/ShortUrl.php index f8964eb5..c89324ad 100644 --- a/module/Core/src/Entity/ShortUrl.php +++ b/module/Core/src/Entity/ShortUrl.php @@ -61,6 +61,7 @@ class ShortUrl extends AbstractEntity implements \JsonSerializable $this->setDateCreated(new \DateTime()); $this->setVisits(new ArrayCollection()); $this->setShortCode(''); + $this->tags = new ArrayCollection(); } /** @@ -177,6 +178,7 @@ class ShortUrl extends AbstractEntity implements \JsonSerializable 'originalUrl' => $this->originalUrl, 'dateCreated' => isset($this->dateCreated) ? $this->dateCreated->format(\DateTime::ISO8601) : null, 'visitsCount' => count($this->visits), + 'tags' => $this->tags, ]; } }