Create REST action to delete orphan visits

This commit is contained in:
Alejandro Celaya 2023-05-18 09:04:28 +02:00
parent abcf2f86be
commit bdfb220126
4 changed files with 89 additions and 0 deletions

View File

@ -38,6 +38,7 @@ return (static function (): array {
Action\Visit\DomainVisitsAction::getRouteDef(), Action\Visit\DomainVisitsAction::getRouteDef(),
Action\Visit\GlobalVisitsAction::getRouteDef(), Action\Visit\GlobalVisitsAction::getRouteDef(),
Action\Visit\OrphanVisitsAction::getRouteDef(), Action\Visit\OrphanVisitsAction::getRouteDef(),
Action\Visit\DeleteOrphanVisitsAction::getRouteDef(),
Action\Visit\NonOrphanVisitsAction::getRouteDef(), Action\Visit\NonOrphanVisitsAction::getRouteDef(),
// Short URLs // Short URLs

View File

@ -38,6 +38,7 @@ return [
Action\Visit\DomainVisitsAction::class => ConfigAbstractFactory::class, Action\Visit\DomainVisitsAction::class => ConfigAbstractFactory::class,
Action\Visit\GlobalVisitsAction::class => ConfigAbstractFactory::class, Action\Visit\GlobalVisitsAction::class => ConfigAbstractFactory::class,
Action\Visit\OrphanVisitsAction::class => ConfigAbstractFactory::class, Action\Visit\OrphanVisitsAction::class => ConfigAbstractFactory::class,
Action\Visit\DeleteOrphanVisitsAction::class => ConfigAbstractFactory::class,
Action\Visit\NonOrphanVisitsAction::class => ConfigAbstractFactory::class, Action\Visit\NonOrphanVisitsAction::class => ConfigAbstractFactory::class,
Action\Tag\ListTagsAction::class => ConfigAbstractFactory::class, Action\Tag\ListTagsAction::class => ConfigAbstractFactory::class,
Action\Tag\TagsStatsAction::class => ConfigAbstractFactory::class, Action\Tag\TagsStatsAction::class => ConfigAbstractFactory::class,
@ -90,6 +91,7 @@ return [
Visit\VisitsStatsHelper::class, Visit\VisitsStatsHelper::class,
Visit\Transformer\OrphanVisitDataTransformer::class, Visit\Transformer\OrphanVisitDataTransformer::class,
], ],
Action\Visit\DeleteOrphanVisitsAction::class => [Visit\VisitsDeleter::class],
Action\Visit\NonOrphanVisitsAction::class => [Visit\VisitsStatsHelper::class], Action\Visit\NonOrphanVisitsAction::class => [Visit\VisitsStatsHelper::class],
Action\ShortUrl\ListShortUrlsAction::class => [ Action\ShortUrl\ListShortUrlsAction::class => [
ShortUrl\ShortUrlListService::class, ShortUrl\ShortUrlListService::class,

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\Visit;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Common\Paginator\Util\PagerfantaUtilsTrait;
use Shlinkio\Shlink\Core\Visit\VisitsDeleterInterface;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
class DeleteOrphanVisitsAction extends AbstractRestAction
{
use PagerfantaUtilsTrait;
protected const ROUTE_PATH = '/visits/orphan';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_DELETE];
public function __construct(private readonly VisitsDeleterInterface $visitsDeleter)
{
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$apiKey = AuthenticationMiddleware::apiKeyFromRequest($request);
$result = $this->visitsDeleter->deleteOrphanVisits($apiKey);
return new JsonResponse($result->toArray('deletedVisits'));
}
}

View File

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\Visit;
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Model\BulkDeleteResult;
use Shlinkio\Shlink\Core\Visit\VisitsDeleterInterface;
use Shlinkio\Shlink\Rest\Action\Visit\DeleteOrphanVisitsAction;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class DeleteOrphanVisitsActionTest extends TestCase
{
private DeleteOrphanVisitsAction $action;
private MockObject & VisitsDeleterInterface $deleter;
protected function setUp(): void
{
$this->deleter = $this->createMock(VisitsDeleterInterface::class);
$this->action = new DeleteOrphanVisitsAction($this->deleter);
}
#[Test, DataProvider('provideVisitsCounts')]
public function orphanVisitsAreDeleted(int $visitsCount): void
{
$apiKey = ApiKey::create();
$request = ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, $apiKey);
$this->deleter->expects($this->once())->method('deleteOrphanVisits')->with($apiKey)->willReturn(
new BulkDeleteResult($visitsCount),
);
/** @var JsonResponse $resp */
$resp = $this->action->handle($request);
$payload = $resp->getPayload();
self::assertEquals(['deletedVisits' => $visitsCount], $payload);
}
public static function provideVisitsCounts(): iterable
{
yield '1' => [1];
yield '0' => [0];
yield '300' => [300];
yield '1234' => [1234];
}
}