diff --git a/module/CLI/src/Command/Tag/ListTagsCommand.php b/module/CLI/src/Command/Tag/ListTagsCommand.php index 5a8389f3..11e22a4f 100644 --- a/module/CLI/src/Command/Tag/ListTagsCommand.php +++ b/module/CLI/src/Command/Tag/ListTagsCommand.php @@ -6,7 +6,7 @@ namespace Shlinkio\Shlink\CLI\Command\Tag; use Shlinkio\Shlink\CLI\Util\ExitCodes; use Shlinkio\Shlink\CLI\Util\ShlinkTable; -use Shlinkio\Shlink\Core\Entity\Tag; +use Shlinkio\Shlink\Core\Tag\Model\TagInfo; use Shlinkio\Shlink\Core\Tag\TagServiceInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; @@ -35,17 +35,20 @@ class ListTagsCommand extends Command protected function execute(InputInterface $input, OutputInterface $output): ?int { - ShlinkTable::fromOutput($output)->render(['Name'], $this->getTagsRows()); + ShlinkTable::fromOutput($output)->render(['Name', 'URLs amount', 'Visits amount'], $this->getTagsRows()); return ExitCodes::EXIT_SUCCESS; } private function getTagsRows(): array { - $tags = $this->tagService->listTags(); + $tags = $this->tagService->tagsInfo(); if (empty($tags)) { - return [['No tags yet']]; + return [['No tags found', '-', '-']]; } - return map($tags, fn (Tag $tag) => [(string) $tag]); + return map( + $tags, + fn (TagInfo $tagInfo) => [(string) $tagInfo->tag(), $tagInfo->shortUrlsCount(), $tagInfo->visitsCount()], + ); } } diff --git a/module/CLI/test/Command/Tag/ListTagsCommandTest.php b/module/CLI/test/Command/Tag/ListTagsCommandTest.php index 4318e906..b6087307 100644 --- a/module/CLI/test/Command/Tag/ListTagsCommandTest.php +++ b/module/CLI/test/Command/Tag/ListTagsCommandTest.php @@ -8,6 +8,7 @@ use PHPUnit\Framework\TestCase; use Prophecy\Prophecy\ObjectProphecy; use Shlinkio\Shlink\CLI\Command\Tag\ListTagsCommand; use Shlinkio\Shlink\Core\Entity\Tag; +use Shlinkio\Shlink\Core\Tag\Model\TagInfo; use Shlinkio\Shlink\Core\Tag\TagServiceInterface; use Symfony\Component\Console\Application; use Symfony\Component\Console\Tester\CommandTester; @@ -31,28 +32,32 @@ class ListTagsCommandTest extends TestCase /** @test */ public function noTagsPrintsEmptyMessage(): void { - $listTags = $this->tagService->listTags()->willReturn([]); + $tagsInfo = $this->tagService->tagsInfo()->willReturn([]); $this->commandTester->execute([]); $output = $this->commandTester->getDisplay(); - $this->assertStringContainsString('No tags yet', $output); - $listTags->shouldHaveBeenCalled(); + $this->assertStringContainsString('No tags found', $output); + $tagsInfo->shouldHaveBeenCalled(); } /** @test */ public function listOfTagsIsPrinted(): void { - $listTags = $this->tagService->listTags()->willReturn([ - new Tag('foo'), - new Tag('bar'), + $tagsInfo = $this->tagService->tagsInfo()->willReturn([ + new TagInfo(new Tag('foo'), 10, 2), + new TagInfo(new Tag('bar'), 7, 32), ]); $this->commandTester->execute([]); $output = $this->commandTester->getDisplay(); - $this->assertStringContainsString('foo', $output); - $this->assertStringContainsString('bar', $output); - $listTags->shouldHaveBeenCalled(); + $this->assertStringContainsString('| foo', $output); + $this->assertStringContainsString('| bar', $output); + $this->assertStringContainsString('| 10 ', $output); + $this->assertStringContainsString('| 2 ', $output); + $this->assertStringContainsString('| 7 ', $output); + $this->assertStringContainsString('| 32 ', $output); + $tagsInfo->shouldHaveBeenCalled(); } } diff --git a/module/Core/src/Tag/Model/TagInfo.php b/module/Core/src/Tag/Model/TagInfo.php index 0237f062..dbc51316 100644 --- a/module/Core/src/Tag/Model/TagInfo.php +++ b/module/Core/src/Tag/Model/TagInfo.php @@ -25,6 +25,16 @@ final class TagInfo implements JsonSerializable return $this->tag; } + public function shortUrlsCount(): int + { + return $this->shortUrlsCount; + } + + public function visitsCount(): int + { + return $this->visitsCount; + } + public function jsonSerialize(): array { return [