Refactored TagInfo to wrap the raw tag name instead of a Tag entity

This commit is contained in:
Alejandro Celaya 2022-01-23 09:37:02 +01:00
parent dd6bcd68cc
commit 8adb6596fb
9 changed files with 14 additions and 18 deletions

View File

@ -46,8 +46,7 @@ class ListTagsCommand extends Command
return map( return map(
$tags, $tags,
static fn (TagInfo $tagInfo) => static fn (TagInfo $tagInfo) => [$tagInfo->tag(), $tagInfo->shortUrlsCount(), $tagInfo->visitsCount()],
[$tagInfo->tag()->__toString(), $tagInfo->shortUrlsCount(), $tagInfo->visitsCount()],
); );
} }
} }

View File

@ -10,7 +10,6 @@ use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\Command\Tag\ListTagsCommand; use Shlinkio\Shlink\CLI\Command\Tag\ListTagsCommand;
use Shlinkio\Shlink\Common\Paginator\Paginator; use Shlinkio\Shlink\Common\Paginator\Paginator;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Tag\Model\TagInfo; use Shlinkio\Shlink\Core\Tag\Model\TagInfo;
use Shlinkio\Shlink\Core\Tag\TagServiceInterface; use Shlinkio\Shlink\Core\Tag\TagServiceInterface;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
@ -45,8 +44,8 @@ class ListTagsCommandTest extends TestCase
public function listOfTagsIsPrinted(): void public function listOfTagsIsPrinted(): void
{ {
$tagsInfo = $this->tagService->tagsInfo(Argument::any())->willReturn(new Paginator(new ArrayAdapter([ $tagsInfo = $this->tagService->tagsInfo(Argument::any())->willReturn(new Paginator(new ArrayAdapter([
new TagInfo(new Tag('foo'), 10, 2), new TagInfo('foo', 10, 2),
new TagInfo(new Tag('bar'), 7, 32), new TagInfo('bar', 7, 32),
]))); ])));
$this->commandTester->execute([]); $this->commandTester->execute([]);

View File

@ -108,13 +108,13 @@ class TagRepository extends EntitySpecificationRepository implements TagReposito
$nativeQb->addOrderBy('t.name_1', $orderMainQuery || $orderDir === null ? 'ASC' : $orderDir); $nativeQb->addOrderBy('t.name_1', $orderMainQuery || $orderDir === null ? 'ASC' : $orderDir);
$rsm = new ResultSetMappingBuilder($this->getEntityManager()); $rsm = new ResultSetMappingBuilder($this->getEntityManager());
$rsm->addRootEntityFromClassMetadata(Tag::class, 't'); $rsm->addScalarResult('name', 'tag');
$rsm->addScalarResult('short_urls_count', 'shortUrlsCount'); $rsm->addScalarResult('short_urls_count', 'shortUrlsCount');
$rsm->addScalarResult('visits_count', 'visitsCount'); $rsm->addScalarResult('visits_count', 'visitsCount');
return map( return map(
$this->getEntityManager()->createNativeQuery($nativeQb->getSQL(), $rsm)->getResult(), $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']),
); );
} }

View File

@ -5,15 +5,14 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Tag\Model; namespace Shlinkio\Shlink\Core\Tag\Model;
use JsonSerializable; use JsonSerializable;
use Shlinkio\Shlink\Core\Entity\Tag;
final class TagInfo implements JsonSerializable 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; return $this->tag;
} }

View File

@ -103,7 +103,7 @@ class TagRepositoryTest extends DatabaseTestCase
foreach ($expectedList as $index => [$tag, $shortUrlsCount, $visitsCount]) { foreach ($expectedList as $index => [$tag, $shortUrlsCount, $visitsCount]) {
self::assertEquals($shortUrlsCount, $result[$index]->shortUrlsCount()); self::assertEquals($shortUrlsCount, $result[$index]->shortUrlsCount());
self::assertEquals($visitsCount, $result[$index]->visitsCount()); self::assertEquals($visitsCount, $result[$index]->visitsCount());
self::assertEquals($tag, $result[$index]->tag()->__toString()); self::assertEquals($tag, $result[$index]->tag());
} }
} }

View File

@ -67,7 +67,7 @@ class TagServiceTest extends TestCase
TagsListFiltering $expectedFiltering, TagsListFiltering $expectedFiltering,
int $countCalls, int $countCalls,
): void { ): 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); $find = $this->repo->findTagsWithInfo($expectedFiltering)->willReturn($expected);
$count = $this->repo->matchSingleScalarResult(Argument::cetera())->willReturn(2); $count = $this->repo->matchSingleScalarResult(Argument::cetera())->willReturn(2);

View File

@ -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 // This part is deprecated. To get tags with stats, the /tags/stats endpoint should be used instead
$tagsInfo = $this->tagService->tagsInfo($params, $apiKey); $tagsInfo = $this->tagService->tagsInfo($params, $apiKey);
$rawTags = $this->serializePaginator($tagsInfo, null, 'stats'); $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]); return new JsonResponse(['tags' => $rawTags]);
} }

View File

@ -76,8 +76,8 @@ class ListTagsActionTest extends TestCase
public function returnsStatsWhenRequested(): void public function returnsStatsWhenRequested(): void
{ {
$stats = [ $stats = [
new TagInfo(new Tag('foo'), 1, 1), new TagInfo('foo', 1, 1),
new TagInfo(new Tag('bar'), 3, 10), new TagInfo('bar', 3, 10),
]; ];
$itemsCount = count($stats); $itemsCount = count($stats);
$tagsInfo = $this->tagService->tagsInfo(Argument::any(), Argument::type(ApiKey::class))->willReturn( $tagsInfo = $this->tagService->tagsInfo(Argument::any(), Argument::type(ApiKey::class))->willReturn(

View File

@ -13,7 +13,6 @@ use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Common\Paginator\Paginator; use Shlinkio\Shlink\Common\Paginator\Paginator;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Tag\Model\TagInfo; use Shlinkio\Shlink\Core\Tag\Model\TagInfo;
use Shlinkio\Shlink\Core\Tag\TagServiceInterface; use Shlinkio\Shlink\Core\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\Tag\TagsStatsAction; use Shlinkio\Shlink\Rest\Action\Tag\TagsStatsAction;
@ -38,8 +37,8 @@ class TagsStatsActionTest extends TestCase
public function returnsTagsStatsWhenRequested(): void public function returnsTagsStatsWhenRequested(): void
{ {
$stats = [ $stats = [
new TagInfo(new Tag('foo'), 1, 1), new TagInfo('foo', 1, 1),
new TagInfo(new Tag('bar'), 3, 10), new TagInfo('bar', 3, 10),
]; ];
$itemsCount = count($stats); $itemsCount = count($stats);
$tagsInfo = $this->tagService->tagsInfo(Argument::any(), Argument::type(ApiKey::class))->willReturn( $tagsInfo = $this->tagService->tagsInfo(Argument::any(), Argument::type(ApiKey::class))->willReturn(