From d0164b8fc16049929c3604e1353f5bdef03e5f90 Mon Sep 17 00:00:00 2001 From: Janko Date: Thu, 22 Aug 2024 21:02:52 +0200 Subject: [PATCH] convert lottery periods to stu date --- src/Module/Trade/Lib/LotteryFacade.php | 15 ++++---- tests/Module/Trade/Lib/LotteryFacadeTest.php | 38 ++++++++++++++++++-- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/Module/Trade/Lib/LotteryFacade.php b/src/Module/Trade/Lib/LotteryFacade.php index a565d2701..d8fbb5410 100644 --- a/src/Module/Trade/Lib/LotteryFacade.php +++ b/src/Module/Trade/Lib/LotteryFacade.php @@ -15,9 +15,7 @@ final class LotteryFacade implements LotteryFacadeInterface { - public function __construct(private LotteryTicketRepositoryInterface $lotteryTicketRepository, private PrivateMessageSenderInterface $privateMessageSender, private StuTime $stuTime) - { - } + public function __construct(private LotteryTicketRepositoryInterface $lotteryTicketRepository, private PrivateMessageSenderInterface $privateMessageSender, private StuTime $stuTime) {} #[Override] public function createLotteryTicket(UserInterface $user, bool $sendPm): void @@ -63,11 +61,14 @@ public function getTicketsOfLastPeriod(): array private function getCurrentOrLastPeriod(bool $isLastPeriod): string { $time = $this->stuTime->time(); - if ($isLastPeriod) { - return date("Y.m", $time - TimeConstants::ONE_DAY_IN_SECONDS); - } else { - return date("Y.m", $time); + $time -= TimeConstants::ONE_DAY_IN_SECONDS; } + + return sprintf( + '%d.%s', + (int)date("Y", $time) + StuTime::STU_YEARS_IN_FUTURE_OFFSET, + date("m", $time) + ); } } diff --git a/tests/Module/Trade/Lib/LotteryFacadeTest.php b/tests/Module/Trade/Lib/LotteryFacadeTest.php index eb782742b..7d78ad060 100644 --- a/tests/Module/Trade/Lib/LotteryFacadeTest.php +++ b/tests/Module/Trade/Lib/LotteryFacadeTest.php @@ -8,6 +8,8 @@ use Override; use Stu\Module\Control\StuTime; use Stu\Module\Message\Lib\PrivateMessageSenderInterface; +use Stu\Orm\Entity\LotteryTicketInterface; +use Stu\Orm\Entity\UserInterface; use Stu\Orm\Repository\LotteryTicketRepositoryInterface; use Stu\StuTestCase; @@ -44,12 +46,42 @@ public function setUp(): void ); } + public function testCreateLotteryTicket(): void + { + $user = $this->mock(UserInterface::class); + $ticket = $this->mock(LotteryTicketInterface::class); + + $time = 1672575107; + + $this->lotteryTicketRepository->shouldReceive('prototype') + ->withNoArgs() + ->once() + ->andReturn($ticket); + $this->lotteryTicketRepository->shouldReceive('save') + ->with($ticket) + ->once(); + + $this->stuTime->shouldReceive('time') + ->withNoArgs() + ->once() + ->andReturn($time); + + $ticket->shouldReceive('setUser') + ->with($user) + ->once(); + $ticket->shouldReceive('setPeriod') + ->with('2393.01') + ->once(); + + $this->lotteryFacade->createLotteryTicket($user, false); + } + public function testGetTicketAmount(): void { $time = 1672575107; $this->lotteryTicketRepository->shouldReceive('getAmountByPeriod') - ->with('2023.01') + ->with('2393.01') ->once() ->andReturn(123); @@ -66,7 +98,7 @@ public function testGetTicketAmountByUser(): void $time = 1672575107; $this->lotteryTicketRepository->shouldReceive('getAmountByPeriodAndUser') - ->with('2023.01', 42) + ->with('2393.01', 42) ->once() ->andReturn(123); @@ -84,7 +116,7 @@ public function testGetTicketsOfLastPeriod(): void $tickets = []; $this->lotteryTicketRepository->shouldReceive('getByPeriod') - ->with('2022.12') + ->with('2392.12') ->once() ->andReturn($tickets);