From bdfb220126d0758dc36d1977fcaab766879e2bd8 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 18 May 2023 09:04:28 +0200 Subject: [PATCH] Create REST action to delete orphan visits --- config/autoload/routes.config.php | 1 + module/Rest/config/dependencies.config.php | 2 + .../Action/Visit/DeleteOrphanVisitsAction.php | 33 ++++++++++++ .../Visit/DeleteOrphanVisitsActionTest.php | 53 +++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 module/Rest/src/Action/Visit/DeleteOrphanVisitsAction.php create mode 100644 module/Rest/test/Action/Visit/DeleteOrphanVisitsActionTest.php diff --git a/config/autoload/routes.config.php b/config/autoload/routes.config.php index 93464519..ea305d86 100644 --- a/config/autoload/routes.config.php +++ b/config/autoload/routes.config.php @@ -38,6 +38,7 @@ return (static function (): array { Action\Visit\DomainVisitsAction::getRouteDef(), Action\Visit\GlobalVisitsAction::getRouteDef(), Action\Visit\OrphanVisitsAction::getRouteDef(), + Action\Visit\DeleteOrphanVisitsAction::getRouteDef(), Action\Visit\NonOrphanVisitsAction::getRouteDef(), // Short URLs diff --git a/module/Rest/config/dependencies.config.php b/module/Rest/config/dependencies.config.php index 43625c41..acca571d 100644 --- a/module/Rest/config/dependencies.config.php +++ b/module/Rest/config/dependencies.config.php @@ -38,6 +38,7 @@ return [ Action\Visit\DomainVisitsAction::class => ConfigAbstractFactory::class, Action\Visit\GlobalVisitsAction::class => ConfigAbstractFactory::class, Action\Visit\OrphanVisitsAction::class => ConfigAbstractFactory::class, + Action\Visit\DeleteOrphanVisitsAction::class => ConfigAbstractFactory::class, Action\Visit\NonOrphanVisitsAction::class => ConfigAbstractFactory::class, Action\Tag\ListTagsAction::class => ConfigAbstractFactory::class, Action\Tag\TagsStatsAction::class => ConfigAbstractFactory::class, @@ -90,6 +91,7 @@ return [ Visit\VisitsStatsHelper::class, Visit\Transformer\OrphanVisitDataTransformer::class, ], + Action\Visit\DeleteOrphanVisitsAction::class => [Visit\VisitsDeleter::class], Action\Visit\NonOrphanVisitsAction::class => [Visit\VisitsStatsHelper::class], Action\ShortUrl\ListShortUrlsAction::class => [ ShortUrl\ShortUrlListService::class, diff --git a/module/Rest/src/Action/Visit/DeleteOrphanVisitsAction.php b/module/Rest/src/Action/Visit/DeleteOrphanVisitsAction.php new file mode 100644 index 00000000..d1d2bc84 --- /dev/null +++ b/module/Rest/src/Action/Visit/DeleteOrphanVisitsAction.php @@ -0,0 +1,33 @@ +visitsDeleter->deleteOrphanVisits($apiKey); + + return new JsonResponse($result->toArray('deletedVisits')); + } +} diff --git a/module/Rest/test/Action/Visit/DeleteOrphanVisitsActionTest.php b/module/Rest/test/Action/Visit/DeleteOrphanVisitsActionTest.php new file mode 100644 index 00000000..b7f6031e --- /dev/null +++ b/module/Rest/test/Action/Visit/DeleteOrphanVisitsActionTest.php @@ -0,0 +1,53 @@ +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]; + } +}