From 8adb6596fb48d2ff99311d3c0259798c755b53af Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 23 Jan 2022 09:37:02 +0100 Subject: [PATCH] Refactored TagInfo to wrap the raw tag name instead of a Tag entity --- module/CLI/src/Command/Tag/ListTagsCommand.php | 3 +-- module/CLI/test/Command/Tag/ListTagsCommandTest.php | 5 ++--- module/Core/src/Repository/TagRepository.php | 4 ++-- module/Core/src/Tag/Model/TagInfo.php | 5 ++--- module/Core/test-db/Repository/TagRepositoryTest.php | 2 +- module/Core/test/Tag/TagServiceTest.php | 2 +- module/Rest/src/Action/Tag/ListTagsAction.php | 2 +- module/Rest/test/Action/Tag/ListTagsActionTest.php | 4 ++-- module/Rest/test/Action/Tag/TagsStatsActionTest.php | 5 ++--- 9 files changed, 14 insertions(+), 18 deletions(-) diff --git a/module/CLI/src/Command/Tag/ListTagsCommand.php b/module/CLI/src/Command/Tag/ListTagsCommand.php index 7d21613d..9c7269fa 100644 --- a/module/CLI/src/Command/Tag/ListTagsCommand.php +++ b/module/CLI/src/Command/Tag/ListTagsCommand.php @@ -46,8 +46,7 @@ class ListTagsCommand extends Command return map( $tags, - static fn (TagInfo $tagInfo) => - [$tagInfo->tag()->__toString(), $tagInfo->shortUrlsCount(), $tagInfo->visitsCount()], + static fn (TagInfo $tagInfo) => [$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 879b2eb7..499442d0 100644 --- a/module/CLI/test/Command/Tag/ListTagsCommandTest.php +++ b/module/CLI/test/Command/Tag/ListTagsCommandTest.php @@ -10,7 +10,6 @@ use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; use Shlinkio\Shlink\CLI\Command\Tag\ListTagsCommand; use Shlinkio\Shlink\Common\Paginator\Paginator; -use Shlinkio\Shlink\Core\Entity\Tag; use Shlinkio\Shlink\Core\Tag\Model\TagInfo; use Shlinkio\Shlink\Core\Tag\TagServiceInterface; use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; @@ -45,8 +44,8 @@ class ListTagsCommandTest extends TestCase public function listOfTagsIsPrinted(): void { $tagsInfo = $this->tagService->tagsInfo(Argument::any())->willReturn(new Paginator(new ArrayAdapter([ - new TagInfo(new Tag('foo'), 10, 2), - new TagInfo(new Tag('bar'), 7, 32), + new TagInfo('foo', 10, 2), + new TagInfo('bar', 7, 32), ]))); $this->commandTester->execute([]); diff --git a/module/Core/src/Repository/TagRepository.php b/module/Core/src/Repository/TagRepository.php index dc3b2e65..66aebae3 100644 --- a/module/Core/src/Repository/TagRepository.php +++ b/module/Core/src/Repository/TagRepository.php @@ -108,13 +108,13 @@ class TagRepository extends EntitySpecificationRepository implements TagReposito $nativeQb->addOrderBy('t.name_1', $orderMainQuery || $orderDir === null ? 'ASC' : $orderDir); $rsm = new ResultSetMappingBuilder($this->getEntityManager()); - $rsm->addRootEntityFromClassMetadata(Tag::class, 't'); + $rsm->addScalarResult('name', 'tag'); $rsm->addScalarResult('short_urls_count', 'shortUrlsCount'); $rsm->addScalarResult('visits_count', 'visitsCount'); return map( $this->getEntityManager()->createNativeQuery($nativeQb->getSQL(), $rsm)->getResult(), - static fn (array $row) => new TagInfo($row[0], (int) $row['shortUrlsCount'], (int) $row['visitsCount']), + static fn (array $row) => new TagInfo($row['tag'], (int) $row['shortUrlsCount'], (int) $row['visitsCount']), ); } diff --git a/module/Core/src/Tag/Model/TagInfo.php b/module/Core/src/Tag/Model/TagInfo.php index 1a436cd4..6e917399 100644 --- a/module/Core/src/Tag/Model/TagInfo.php +++ b/module/Core/src/Tag/Model/TagInfo.php @@ -5,15 +5,14 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Tag\Model; use JsonSerializable; -use Shlinkio\Shlink\Core\Entity\Tag; final class TagInfo implements JsonSerializable { - public function __construct(private Tag $tag, private int $shortUrlsCount, private int $visitsCount) + public function __construct(private string $tag, private int $shortUrlsCount, private int $visitsCount) { } - public function tag(): Tag + public function tag(): string { return $this->tag; } diff --git a/module/Core/test-db/Repository/TagRepositoryTest.php b/module/Core/test-db/Repository/TagRepositoryTest.php index 50129e54..fe544376 100644 --- a/module/Core/test-db/Repository/TagRepositoryTest.php +++ b/module/Core/test-db/Repository/TagRepositoryTest.php @@ -103,7 +103,7 @@ class TagRepositoryTest extends DatabaseTestCase foreach ($expectedList as $index => [$tag, $shortUrlsCount, $visitsCount]) { self::assertEquals($shortUrlsCount, $result[$index]->shortUrlsCount()); self::assertEquals($visitsCount, $result[$index]->visitsCount()); - self::assertEquals($tag, $result[$index]->tag()->__toString()); + self::assertEquals($tag, $result[$index]->tag()); } } diff --git a/module/Core/test/Tag/TagServiceTest.php b/module/Core/test/Tag/TagServiceTest.php index c3efc4b5..8c301f0f 100644 --- a/module/Core/test/Tag/TagServiceTest.php +++ b/module/Core/test/Tag/TagServiceTest.php @@ -67,7 +67,7 @@ class TagServiceTest extends TestCase TagsListFiltering $expectedFiltering, int $countCalls, ): void { - $expected = [new TagInfo(new Tag('foo'), 1, 1), new TagInfo(new Tag('bar'), 3, 10)]; + $expected = [new TagInfo('foo', 1, 1), new TagInfo('bar', 3, 10)]; $find = $this->repo->findTagsWithInfo($expectedFiltering)->willReturn($expected); $count = $this->repo->matchSingleScalarResult(Argument::cetera())->willReturn(2); diff --git a/module/Rest/src/Action/Tag/ListTagsAction.php b/module/Rest/src/Action/Tag/ListTagsAction.php index ba25ffe5..ab81400c 100644 --- a/module/Rest/src/Action/Tag/ListTagsAction.php +++ b/module/Rest/src/Action/Tag/ListTagsAction.php @@ -41,7 +41,7 @@ class ListTagsAction extends AbstractRestAction // This part is deprecated. To get tags with stats, the /tags/stats endpoint should be used instead $tagsInfo = $this->tagService->tagsInfo($params, $apiKey); $rawTags = $this->serializePaginator($tagsInfo, null, 'stats'); - $rawTags['data'] = map($tagsInfo, static fn (TagInfo $info) => $info->tag()->__toString()); + $rawTags['data'] = map($tagsInfo, static fn (TagInfo $info) => $info->tag()); return new JsonResponse(['tags' => $rawTags]); } diff --git a/module/Rest/test/Action/Tag/ListTagsActionTest.php b/module/Rest/test/Action/Tag/ListTagsActionTest.php index 504f7b4f..123e4945 100644 --- a/module/Rest/test/Action/Tag/ListTagsActionTest.php +++ b/module/Rest/test/Action/Tag/ListTagsActionTest.php @@ -76,8 +76,8 @@ class ListTagsActionTest extends TestCase public function returnsStatsWhenRequested(): void { $stats = [ - new TagInfo(new Tag('foo'), 1, 1), - new TagInfo(new Tag('bar'), 3, 10), + new TagInfo('foo', 1, 1), + new TagInfo('bar', 3, 10), ]; $itemsCount = count($stats); $tagsInfo = $this->tagService->tagsInfo(Argument::any(), Argument::type(ApiKey::class))->willReturn( diff --git a/module/Rest/test/Action/Tag/TagsStatsActionTest.php b/module/Rest/test/Action/Tag/TagsStatsActionTest.php index 3f98b64e..2cb3ad64 100644 --- a/module/Rest/test/Action/Tag/TagsStatsActionTest.php +++ b/module/Rest/test/Action/Tag/TagsStatsActionTest.php @@ -13,7 +13,6 @@ use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Psr\Http\Message\ServerRequestInterface; use Shlinkio\Shlink\Common\Paginator\Paginator; -use Shlinkio\Shlink\Core\Entity\Tag; use Shlinkio\Shlink\Core\Tag\Model\TagInfo; use Shlinkio\Shlink\Core\Tag\TagServiceInterface; use Shlinkio\Shlink\Rest\Action\Tag\TagsStatsAction; @@ -38,8 +37,8 @@ class TagsStatsActionTest extends TestCase public function returnsTagsStatsWhenRequested(): void { $stats = [ - new TagInfo(new Tag('foo'), 1, 1), - new TagInfo(new Tag('bar'), 3, 10), + new TagInfo('foo', 1, 1), + new TagInfo('bar', 3, 10), ]; $itemsCount = count($stats); $tagsInfo = $this->tagService->tagsInfo(Argument::any(), Argument::type(ApiKey::class))->willReturn(