From abcf2f86be8e27bb4e7af6709c9c694adb824a5d Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 18 May 2023 09:01:57 +0200 Subject: [PATCH] Create service to delete orphan visits --- module/Core/config/dependencies.config.php | 2 + module/Core/src/Visit/VisitsDeleter.php | 22 ++++++++++ .../Core/src/Visit/VisitsDeleterInterface.php | 13 ++++++ module/Core/test/Visit/VisitsDeleterTest.php | 41 +++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 module/Core/src/Visit/VisitsDeleter.php create mode 100644 module/Core/src/Visit/VisitsDeleterInterface.php create mode 100644 module/Core/test/Visit/VisitsDeleterTest.php diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index 8e3e9a52..8b6acc3e 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -62,6 +62,7 @@ return [ Visit\VisitsTracker::class => ConfigAbstractFactory::class, Visit\RequestTracker::class => ConfigAbstractFactory::class, + Visit\VisitsDeleter::class => ConfigAbstractFactory::class, Visit\Geolocation\VisitLocator::class => ConfigAbstractFactory::class, Visit\Geolocation\VisitToLocationHelper::class => ConfigAbstractFactory::class, Visit\VisitsStatsHelper::class => ConfigAbstractFactory::class, @@ -122,6 +123,7 @@ return [ Options\TrackingOptions::class, ], Visit\RequestTracker::class => [Visit\VisitsTracker::class, Options\TrackingOptions::class], + Visit\VisitsDeleter::class => [Visit\Repository\VisitDeleterRepository::class], ShortUrl\ShortUrlService::class => [ 'em', ShortUrl\ShortUrlResolver::class, diff --git a/module/Core/src/Visit/VisitsDeleter.php b/module/Core/src/Visit/VisitsDeleter.php new file mode 100644 index 00000000..5a8adca5 --- /dev/null +++ b/module/Core/src/Visit/VisitsDeleter.php @@ -0,0 +1,22 @@ +repository->deleteOrphanVisits()); + } +} diff --git a/module/Core/src/Visit/VisitsDeleterInterface.php b/module/Core/src/Visit/VisitsDeleterInterface.php new file mode 100644 index 00000000..3a75a0d3 --- /dev/null +++ b/module/Core/src/Visit/VisitsDeleterInterface.php @@ -0,0 +1,13 @@ +repo = $this->createMock(VisitDeleterRepositoryInterface::class); + $this->visitsDeleter = new VisitsDeleter($this->repo); + } + + #[Test, DataProvider('provideVisitsCounts')] + public function returnsDeletedVisitsFromRepo(int $visitsCount): void + { + $this->repo->expects($this->once())->method('deleteOrphanVisits')->willReturn($visitsCount); + + $result = $this->visitsDeleter->deleteOrphanVisits(); + + self::assertEquals($visitsCount, $result->affectedItems); + } + + public static function provideVisitsCounts(): iterable + { + yield '45' => [45]; + yield '5000' => [5000]; + yield '0' => [0]; + } +}