diff --git a/CHANGELOG.md b/CHANGELOG.md index e81dc273..19b50747 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this Additionally, the endpoint also supports filtering by `searchTerm` query param. When provided, only tags matching it will be returned. +* [#1315](https://github.com/shlinkio/shlink/issues/1315) Included new `GET /tags/stats` endpoint, which effectively deprecates `GET /tags?withStats=true`. + ### Changed * [#1277](https://github.com/shlinkio/shlink/issues/1277) Reduced docker image size to 45% of the original size. * [#1268](https://github.com/shlinkio/shlink/issues/1268) Updated dependencies, including symfony/console 6 and mezzio/mezzio-swoole 4. diff --git a/module/Rest/src/Action/Tag/ListTagsAction.php b/module/Rest/src/Action/Tag/ListTagsAction.php index bad8f62e..ba25ffe5 100644 --- a/module/Rest/src/Action/Tag/ListTagsAction.php +++ b/module/Rest/src/Action/Tag/ListTagsAction.php @@ -29,8 +29,7 @@ class ListTagsAction extends AbstractRestAction public function handle(ServerRequestInterface $request): ResponseInterface { - $query = $request->getQueryParams(); - $params = TagsParams::fromRawData($query); + $params = TagsParams::fromRawData($request->getQueryParams()); $apiKey = AuthenticationMiddleware::apiKeyFromRequest($request); if (! $params->withStats()) { diff --git a/module/Rest/src/Action/Tag/TagsStatsAction.php b/module/Rest/src/Action/Tag/TagsStatsAction.php index eeb149db..cec8edd6 100644 --- a/module/Rest/src/Action/Tag/TagsStatsAction.php +++ b/module/Rest/src/Action/Tag/TagsStatsAction.php @@ -26,8 +26,7 @@ class TagsStatsAction extends AbstractRestAction public function handle(ServerRequestInterface $request): ResponseInterface { - $query = $request->getQueryParams(); - $params = TagsParams::fromRawData($query); + $params = TagsParams::fromRawData($request->getQueryParams()); $apiKey = AuthenticationMiddleware::apiKeyFromRequest($request); $tagsInfo = $this->tagService->tagsInfo($params, $apiKey); diff --git a/module/Rest/test/Action/Tag/TagsStatsActionTest.php b/module/Rest/test/Action/Tag/TagsStatsActionTest.php new file mode 100644 index 00000000..3f98b64e --- /dev/null +++ b/module/Rest/test/Action/Tag/TagsStatsActionTest.php @@ -0,0 +1,73 @@ +tagService = $this->prophesize(TagServiceInterface::class); + $this->action = new TagsStatsAction($this->tagService->reveal()); + } + + /** @test */ + public function returnsTagsStatsWhenRequested(): void + { + $stats = [ + new TagInfo(new Tag('foo'), 1, 1), + new TagInfo(new Tag('bar'), 3, 10), + ]; + $itemsCount = count($stats); + $tagsInfo = $this->tagService->tagsInfo(Argument::any(), Argument::type(ApiKey::class))->willReturn( + new Paginator(new ArrayAdapter($stats)), + ); + $req = $this->requestWithApiKey()->withQueryParams(['withStats' => 'true']); + + /** @var JsonResponse $resp */ + $resp = $this->action->handle($req); + $payload = $resp->getPayload(); + + self::assertEquals([ + 'tags' => [ + 'data' => $stats, + 'pagination' => [ + 'currentPage' => 1, + 'pagesCount' => 1, + 'itemsPerPage' => 10, + 'itemsInCurrentPage' => $itemsCount, + 'totalItems' => $itemsCount, + ], + ], + ], $payload); + $tagsInfo->shouldHaveBeenCalled(); + } + + private function requestWithApiKey(): ServerRequestInterface + { + return ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, ApiKey::create()); + } +}