Allowed to display tags in the shortcode:list command

This commit is contained in:
Alejandro Celaya 2016-08-21 10:17:17 +02:00
parent e3021120e3
commit 2b89556c09
3 changed files with 46 additions and 3 deletions

View File

@ -55,12 +55,20 @@ class ListShortcodesCommand extends Command
PaginableRepositoryAdapter::ITEMS_PER_PAGE PaginableRepositoryAdapter::ITEMS_PER_PAGE
), ),
1 1
)
->addOption(
'tags',
't',
InputOption::VALUE_NONE,
$this->translator->translate('Whether to display the tags or not')
); );
} }
public function execute(InputInterface $input, OutputInterface $output) public function execute(InputInterface $input, OutputInterface $output)
{ {
$page = intval($input->getOption('page')); $page = intval($input->getOption('page'));
$showTags = $input->getOption('tags');
/** @var QuestionHelper $helper */ /** @var QuestionHelper $helper */
$helper = $this->getHelper('question'); $helper = $this->getHelper('question');
@ -68,15 +76,31 @@ class ListShortcodesCommand extends Command
$result = $this->shortUrlService->listShortUrls($page); $result = $this->shortUrlService->listShortUrls($page);
$page++; $page++;
$table = new Table($output); $table = new Table($output);
$table->setHeaders([
$headers = [
$this->translator->translate('Short code'), $this->translator->translate('Short code'),
$this->translator->translate('Original URL'), $this->translator->translate('Original URL'),
$this->translator->translate('Date created'), $this->translator->translate('Date created'),
$this->translator->translate('Visits count'), $this->translator->translate('Visits count'),
]); ];
if ($showTags) {
$headers[] = $this->translator->translate('Tags');
}
$table->setHeaders($headers);
foreach ($result as $row) { 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(); $table->render();

View File

@ -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) protected function getInputStream($inputData)
{ {
$stream = fopen('php://memory', 'r+', false); $stream = fopen('php://memory', 'r+', false);

View File

@ -61,6 +61,7 @@ class ShortUrl extends AbstractEntity implements \JsonSerializable
$this->setDateCreated(new \DateTime()); $this->setDateCreated(new \DateTime());
$this->setVisits(new ArrayCollection()); $this->setVisits(new ArrayCollection());
$this->setShortCode(''); $this->setShortCode('');
$this->tags = new ArrayCollection();
} }
/** /**
@ -177,6 +178,7 @@ class ShortUrl extends AbstractEntity implements \JsonSerializable
'originalUrl' => $this->originalUrl, 'originalUrl' => $this->originalUrl,
'dateCreated' => isset($this->dateCreated) ? $this->dateCreated->format(\DateTime::ISO8601) : null, 'dateCreated' => isset($this->dateCreated) ? $this->dateCreated->format(\DateTime::ISO8601) : null,
'visitsCount' => count($this->visits), 'visitsCount' => count($this->visits),
'tags' => $this->tags,
]; ];
} }
} }