|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ApplicationTest\Service; |
|
6
|
|
|
|
|
7
|
|
|
use Application\DBAL\Types\MessageTypeType; |
|
8
|
|
|
use Application\Model\Account; |
|
9
|
|
|
use Application\Model\Bookable; |
|
10
|
|
|
use Application\Model\Message; |
|
11
|
|
|
use Application\Model\User; |
|
12
|
|
|
use Application\Service\MessageQueuer; |
|
13
|
|
|
use Doctrine\ORM\EntityManager; |
|
14
|
|
|
use Prophecy\Argument; |
|
15
|
|
|
use Zend\View\Renderer\RendererInterface; |
|
16
|
|
|
|
|
17
|
|
|
class MessageQueuerTest extends \PHPUnit\Framework\TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
private function createMockMessageQueuer(): MessageQueuer |
|
20
|
|
|
{ |
|
21
|
|
|
global $container; |
|
22
|
|
|
|
|
23
|
|
|
$entityManager = $container->get(EntityManager::class); |
|
24
|
|
|
$renderer = $container->get(RendererInterface::class); |
|
25
|
|
|
|
|
26
|
|
|
$messageQueuer = new MessageQueuer( |
|
27
|
|
|
$entityManager, |
|
28
|
|
|
$renderer, |
|
29
|
|
|
'my-ichtus.lan' |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
return $messageQueuer; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testQueueRegister(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$user = $this->createMockUserMinimal(); |
|
38
|
|
|
$messageQueuer = $this->createMockMessageQueuer(); |
|
39
|
|
|
$message = $messageQueuer->queueRegister($user); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertMessage($message, $user, '[email protected]', MessageTypeType::REGISTER, 'Demande de création de compte au Club Nautique Ichtus'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testQueueUnregister(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$unregisteredUser = $this->createMockUser(); |
|
47
|
|
|
$admin = $this->createMockUserAdmin(); |
|
48
|
|
|
$messageQueuer = $this->createMockMessageQueuer(); |
|
49
|
|
|
$message = $messageQueuer->queueUnregister($admin, $unregisteredUser); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertMessage($message, $admin, '[email protected]', MessageTypeType::UNREGISTER, 'Démission'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testQueueResetPassword(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$user = $this->createMockUser(); |
|
57
|
|
|
$messageQueuer = $this->createMockMessageQueuer(); |
|
58
|
|
|
$message = $messageQueuer->queueResetPassword($user, '[email protected]'); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertMessage($message, $user, '[email protected]', MessageTypeType::RESET_PASSWORD, 'Demande de modification de mot de passe'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testQueueBalancePositive(): void |
|
64
|
|
|
{ |
|
65
|
|
|
$bookables = []; |
|
66
|
|
|
$bookables[] = $this->createBookable('Cotisation', '90.00'); |
|
67
|
|
|
$bookables[] = $this->createBookable('Fonds de réparation interne', '10.00'); |
|
68
|
|
|
|
|
69
|
|
|
$this->queueBalance($bookables, 'positive'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testQueueBalanceNegative(): void |
|
73
|
|
|
{ |
|
74
|
|
|
$bookables = []; |
|
75
|
|
|
$bookables[] = $this->createBookable('Cotisation', '90.00'); |
|
76
|
|
|
$bookables[] = $this->createBookable('Fonds de réparation interne', '10.00'); |
|
77
|
|
|
$bookables[] = $this->createBookable('Casier 1012', '20.00'); |
|
78
|
|
|
$bookables[] = $this->createBookable('Casier 1014', '20.00'); |
|
79
|
|
|
|
|
80
|
|
|
$this->queueBalance($bookables, 'negative'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function queueBalance(array $bookables, string $variant): void |
|
84
|
|
|
{ |
|
85
|
|
|
$user = new User(); |
|
86
|
|
|
$user->setLogin('john.doe'); |
|
87
|
|
|
$user->setFirstName('John'); |
|
88
|
|
|
$user->setLastName('Doe'); |
|
89
|
|
|
$user->setEmail('[email protected]'); |
|
90
|
|
|
|
|
91
|
|
|
$account = new Account(); |
|
92
|
|
|
$account->setBalance($variant === 'positive' ? '25.00' : '-45.00'); |
|
93
|
|
|
$account->setOwner($user); |
|
94
|
|
|
|
|
95
|
|
|
$messageQueuer = $this->createMockMessageQueuer(); |
|
96
|
|
|
$message = $messageQueuer->queueBalance($user, $bookables); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertMessage($message, $user, '[email protected]', MessageTypeType::BALANCE, 'Balance de compte', $variant); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testQueueAllBalance(): void |
|
102
|
|
|
{ |
|
103
|
|
|
$messageQueuer = $this->createMockMessageQueuer(); |
|
104
|
|
|
$actual = $messageQueuer->queueAllBalance(); |
|
105
|
|
|
|
|
106
|
|
|
self::assertsame(1, $actual); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testQueueNegativeBalance(): void |
|
110
|
|
|
{ |
|
111
|
|
|
$messageQueuer = $this->createMockMessageQueuer(); |
|
112
|
|
|
$actual = $messageQueuer->queueNegativeBalance(); |
|
113
|
|
|
|
|
114
|
|
|
self::assertsame(0, $actual); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
private function createMockUser(): User |
|
118
|
|
|
{ |
|
119
|
|
|
$prophecy = $this->prophesize(User::class); |
|
120
|
|
|
$prophecy->getId()->willReturn(123); |
|
121
|
|
|
$prophecy->getLogin()->willReturn('john.doe'); |
|
122
|
|
|
$prophecy->getFirstName()->willReturn('John'); |
|
123
|
|
|
$prophecy->getLastName()->willReturn('Doe'); |
|
124
|
|
|
$prophecy->getName()->willReturn('John Doe'); |
|
125
|
|
|
$prophecy->getEmail()->willReturn('[email protected]'); |
|
126
|
|
|
$prophecy->createToken()->willReturn(str_repeat('X', 32)); |
|
127
|
|
|
$prophecy->messageAdded(Argument::type(Message::class)); |
|
128
|
|
|
|
|
129
|
|
|
$user = $prophecy->reveal(); |
|
130
|
|
|
|
|
131
|
|
|
return $user; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
private function createMockUserAdmin(): User |
|
135
|
|
|
{ |
|
136
|
|
|
$prophecy = $this->prophesize(User::class); |
|
137
|
|
|
$prophecy->getLogin()->willReturn('admin'); |
|
138
|
|
|
$prophecy->getFirstName()->willReturn('Admin'); |
|
139
|
|
|
$prophecy->getLastName()->willReturn('Istrator'); |
|
140
|
|
|
$prophecy->getEmail()->willReturn('[email protected]'); |
|
141
|
|
|
$prophecy->messageAdded(Argument::type(Message::class)); |
|
142
|
|
|
|
|
143
|
|
|
$user = $prophecy->reveal(); |
|
144
|
|
|
|
|
145
|
|
|
return $user; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
private function createMockUserMinimal(): User |
|
149
|
|
|
{ |
|
150
|
|
|
$prophecy = $this->prophesize(User::class); |
|
151
|
|
|
$prophecy->getLogin(); |
|
152
|
|
|
$prophecy->getFirstName(); |
|
153
|
|
|
$prophecy->getLastName(); |
|
154
|
|
|
$prophecy->getEmail()->willReturn('[email protected]'); |
|
155
|
|
|
$prophecy->createToken()->willReturn(str_repeat('X', 32)); |
|
156
|
|
|
$prophecy->messageAdded(Argument::type(Message::class)); |
|
157
|
|
|
$user = $prophecy->reveal(); |
|
158
|
|
|
|
|
159
|
|
|
return $user; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
private function assertMessage(Message $message, ?User $user, string $email, string $type, string $subject, ?string $variant = null): void |
|
163
|
|
|
{ |
|
164
|
|
|
self::assertSame($type, $message->getType()); |
|
165
|
|
|
self::assertSame($email, $message->getEmail()); |
|
166
|
|
|
self::assertSame($user, $message->getRecipient()); |
|
167
|
|
|
self::assertNull($message->getDateSent()); |
|
168
|
|
|
self::assertSame($subject, $message->getSubject()); |
|
169
|
|
|
|
|
170
|
|
|
$variant = $variant ? '-' . $variant : $variant; |
|
171
|
|
|
$expectedBody = 'tests/data/emails/' . str_replace('_', '-', $type . $variant) . '.html'; |
|
172
|
|
|
$this->assertFile($expectedBody, $message->getBody()); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Custom assert that will not produce gigantic diff |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $file |
|
179
|
|
|
* @param string $actual |
|
180
|
|
|
*/ |
|
181
|
|
|
private function assertFile(string $file, string $actual): void |
|
182
|
|
|
{ |
|
183
|
|
|
// Log actual result for easier comparison with external diff tools |
|
184
|
|
|
$logFile = 'logs/' . $file; |
|
185
|
|
|
$dir = dirname($logFile); |
|
186
|
|
|
@mkdir($dir, 0777, true); |
|
|
|
|
|
|
187
|
|
|
file_put_contents($logFile, $actual); |
|
188
|
|
|
|
|
189
|
|
|
self::assertFileExists($file, 'Expected file must exist on disk, fix it with: cp ' . $logFile . ' ' . $file); |
|
190
|
|
|
$expected = file_get_contents($file); |
|
191
|
|
|
|
|
192
|
|
|
self::assertTrue($expected === $actual, 'File content does not match, compare with: meld ' . $file . ' ' . $logFile); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
private function createBookable(string $bookableName, string $periodicPrice): Bookable |
|
196
|
|
|
{ |
|
197
|
|
|
$bookable = new Bookable(); |
|
198
|
|
|
$bookable->setName($bookableName); |
|
199
|
|
|
$bookable->setPeriodicPrice($periodicPrice); |
|
200
|
|
|
|
|
201
|
|
|
return $bookable; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: