Skip to content

Commit

Permalink
convert lottery periods to stu date
Browse files Browse the repository at this point in the history
  • Loading branch information
g5bot committed Aug 22, 2024
1 parent 4c2a94f commit d0164b8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
15 changes: 8 additions & 7 deletions src/Module/Trade/Lib/LotteryFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
);
}
}
38 changes: 35 additions & 3 deletions tests/Module/Trade/Lib/LotteryFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

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

Expand All @@ -84,7 +116,7 @@ public function testGetTicketsOfLastPeriod(): void
$tickets = [];

$this->lotteryTicketRepository->shouldReceive('getByPeriod')
->with('2022.12')
->with('2392.12')
->once()
->andReturn($tickets);

Expand Down

0 comments on commit d0164b8

Please sign in to comment.