Created TagStatsActionTest

This commit is contained in:
Alejandro Celaya 2022-01-09 17:37:00 +01:00
parent d5851bbb6a
commit a6b1647f27
4 changed files with 77 additions and 4 deletions

View file

@ -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.

View file

@ -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()) {

View file

@ -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);

View file

@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\Tag;
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\ServerRequestFactory;
use Pagerfanta\Adapter\ArrayAdapter;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
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;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use function count;
class TagsStatsActionTest extends TestCase
{
use ProphecyTrait;
private TagsStatsAction $action;
private ObjectProphecy $tagService;
public function setUp(): void
{
$this->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());
}
}