-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIncentiveListenerTest.php
92 lines (67 loc) · 2.61 KB
/
IncentiveListenerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types=1);
namespace RewardsProgramTest;
use PHPUnit\Framework\TestCase;
use \DateInterval;
use \DateTimeImmutable;
use RewardsProgram\IncentiveListener;
use RewardsProgram\UserLogDataEvent;
use RewardsProgram\UserBirthEvent;
use RewardsProgram\RewardRepository;
use RewardsProgram\User;
use RewardsProgram\UserEventRepository;
use Symfony\Component\EventDispatcher\EventDispatcher;
class IncentiveListenerTest extends TestCase
{
public function testBirthEventReward()
{
$user = new User("test123");
$rewardRepository = self::createMock(RewardRepository::class);
$rewardRepository->expects(self::once())
->method('createRewardForBirthEvent')
->with($user);
$eventDispatcher = new EventDispatcher();
new IncentiveListener($eventDispatcher, $rewardRepository, new UserEventRepository());
$eventDispatcher->dispatch(
new UserBirthEvent($user, new DateTimeImmutable()),
UserBirthEvent::NAME
);
}
public function testFiveConcurrentLogDataReward()
{
$user = new User("test123");
$rewardRepository = self::createMock(RewardRepository::class);
$rewardRepository->expects(self::once())
->method('createRewardForConcurrentDaysLogged')
->with($user);
$eventDispatcher = new EventDispatcher();
new IncentiveListener($eventDispatcher, $rewardRepository, new UserEventRepository());
$oneDayInterval = DateInterval::createFromDateString('1 day');
$eventDate = (new DateTimeImmutable("5 days ago"));
foreach (range(1, 5) as $i) {
$eventDispatcher->dispatch(
new UserLogDataEvent($user, $eventDate),
UserLogDataEvent::NAME
);
$eventDate = $eventDate->add($oneDayInterval);
}
}
public function testDayGapNoReward()
{
$user = new User("test123");
$rewardRepository = self::createMock(RewardRepository::class);
$rewardRepository->expects(self::never())
->method('createRewardForConcurrentDaysLogged')
->with($user);
$eventDispatcher = new EventDispatcher();
new IncentiveListener($eventDispatcher, $rewardRepository, new UserEventRepository());
$now = (new DateTimeImmutable());
foreach ([ 6, 5, 4, 2, 1 ] as $daysAgo) {
$date = $now->sub(DateInterval::createFromDateString("$daysAgo days"));
$eventDispatcher->dispatch(
new UserLogDataEvent($user, $date),
UserLogDataEvent::NAME
);
}
}
}