|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
use Mockery as m; |
|
4
|
|
|
use Mockery\MockInterface; |
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
|
|
7
|
|
|
class Record |
|
8
|
|
|
{ |
|
9
|
|
|
public int $account_id = 1; |
|
10
|
|
|
public string $login = "login"; |
|
11
|
|
|
public string $password = "password"; |
|
12
|
|
|
public string $email = "email"; |
|
13
|
|
|
public string $last_login = "last login"; |
|
14
|
|
|
public string $validation_code = "validation code"; |
|
15
|
|
|
public string $offset = "offset"; |
|
16
|
|
|
public string $images = "Yes"; |
|
17
|
|
|
public string $fontsize = "font size"; |
|
18
|
|
|
public string $password_reset = "password reset"; |
|
19
|
|
|
public int $mail_banned = 0; |
|
20
|
|
|
public string $friendly_colour = "colour"; |
|
21
|
|
|
public string $neutral_colour = "colour"; |
|
22
|
|
|
public string $enemy_colour = "colour"; |
|
23
|
|
|
public string $css_link = "css link"; |
|
24
|
|
|
public string $referral_id = 'referral_id'; |
|
25
|
|
|
public string $max_rank_achieved = 'max_rank_achieved'; |
|
26
|
|
|
public string $hof_name = 'hof_name'; |
|
27
|
|
|
public string $discord_id = 'discord_id'; |
|
28
|
|
|
public string $irc_nick = 'irc_nick'; |
|
29
|
|
|
public string $date_short = 'date_short'; |
|
30
|
|
|
public string $time_short = 'time_short'; |
|
31
|
|
|
public string $template = 'template'; |
|
32
|
|
|
public string $colour_scheme = 'colour_scheme'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Class AbstractSmrAccountTest |
|
37
|
|
|
* @covers AbstractSmrAccount |
|
38
|
|
|
* @covers Globals |
|
39
|
|
|
* @runTestsInSeparateProcesses |
|
40
|
|
|
*/ |
|
41
|
|
|
class AbstractSmrAccountTest extends TestCase |
|
42
|
|
|
{ |
|
43
|
|
|
private AbstractSmrAccount $abstractSmrAccount; |
|
44
|
|
|
|
|
45
|
|
|
public function test_get_account_by_account_id() |
|
46
|
|
|
{ |
|
47
|
|
|
//# Given the database has been set up with a user |
|
48
|
|
|
$record = new Record(); |
|
49
|
|
|
self::setupMockMysqlDatabase($record); |
|
50
|
|
|
//# And there is no force update |
|
51
|
|
|
$forceUpdate = false; |
|
52
|
|
|
//# When the account is retrieved by its ID |
|
53
|
|
|
$this->abstractSmrAccount = AbstractSmrAccount::getAccount($record->account_id, $forceUpdate); |
|
54
|
|
|
//# Then the integrity of the user is correct |
|
55
|
|
|
$this->assertEquals($record->account_id, $this->abstractSmrAccount->getAccountID()); |
|
56
|
|
|
$this->assertEquals($record->login, $this->abstractSmrAccount->getLogin()); |
|
57
|
|
|
$this->assertEquals($record->email, $this->abstractSmrAccount->getEmail()); |
|
58
|
|
|
$this->assertEquals($record->last_login, $this->abstractSmrAccount->getLastLogin()); |
|
59
|
|
|
$this->assertEquals($record->validation_code, $this->abstractSmrAccount->getValidationCode()); |
|
60
|
|
|
$this->assertEquals($record->offset, $this->abstractSmrAccount->getOffset()); |
|
61
|
|
|
$this->assertEquals(true, $this->abstractSmrAccount->isDisplayShipImages()); |
|
62
|
|
|
$this->assertEquals($record->fontsize, $this->abstractSmrAccount->getFontSize()); |
|
63
|
|
|
$this->assertEquals($record->password_reset, $this->abstractSmrAccount->getPasswordReset()); |
|
64
|
|
|
$this->assertEquals($record->mail_banned, $this->abstractSmrAccount->getMailBanned()); |
|
65
|
|
|
$this->assertEquals($record->friendly_colour, $this->abstractSmrAccount->getFriendlyColour()); |
|
66
|
|
|
$this->assertEquals($record->neutral_colour, $this->abstractSmrAccount->getNeutralColour()); |
|
67
|
|
|
$this->assertEquals($record->enemy_colour, $this->abstractSmrAccount->getEnemyColour()); |
|
68
|
|
|
$this->assertEquals($record->css_link, $this->abstractSmrAccount->getCssLink()); |
|
69
|
|
|
$this->assertEquals($record->referral_id, $this->abstractSmrAccount->getReferrerID()); |
|
70
|
|
|
$this->assertEquals($record->hof_name, $this->abstractSmrAccount->getHofName()); |
|
71
|
|
|
$this->assertEquals($record->discord_id, $this->abstractSmrAccount->getDiscordId()); |
|
72
|
|
|
$this->assertEquals($record->irc_nick, $this->abstractSmrAccount->getIrcNick()); |
|
73
|
|
|
$this->assertEquals($record->date_short, $this->abstractSmrAccount->getShortDateFormat()); |
|
74
|
|
|
$this->assertEquals($record->time_short, $this->abstractSmrAccount->getShortTimeFormat()); |
|
75
|
|
|
$this->assertEquals($record->template, $this->abstractSmrAccount->getTemplate()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function test_get_account_by_account_id_no_account_found_throws_exception() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->expectException(AccountNotFoundException::class); |
|
81
|
|
|
//# Given there is no account record |
|
82
|
|
|
$mysqlDatabase = m::mock("overload:" . SmrMySqlDatabase::class)->shouldIgnoreMissing(); |
|
83
|
|
|
$mysqlDatabase |
|
84
|
|
|
->shouldReceive("nextRecord") |
|
85
|
|
|
->andReturnNull(); |
|
86
|
|
|
//# When performing an account lookup by id |
|
87
|
|
|
AbstractSmrAccount::getAccount("bad account id"); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function test_get_account_by_account_id_force_update_from_database() |
|
91
|
|
|
{ |
|
92
|
|
|
//# Given the database has been set up with a user |
|
93
|
|
|
$record = new Record(); |
|
94
|
|
|
self::setupMockMysqlDatabase($record); |
|
95
|
|
|
//# And the force update flag is true |
|
96
|
|
|
$forceUpdate = true; |
|
97
|
|
|
//# And the account has been retrieved once |
|
98
|
|
|
AbstractSmrAccount::getAccount($record->account_id, $forceUpdate); |
|
99
|
|
|
//# When retrieving the account a second time |
|
100
|
|
|
AbstractSmrAccount::getAccount($record->account_id, $forceUpdate); |
|
101
|
|
|
//# Then verify multiple interactions with the database |
|
102
|
|
|
//# There are a total of three mocks in the container: |
|
103
|
|
|
//# One constructed here in this test, and one for each time getAccount was called. |
|
104
|
|
|
$this->assertCount(3, m::getContainer()->getMocks()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function test_get_account_by_id_multiple_times_without_force_refresh_calls_database_once() |
|
108
|
|
|
{ |
|
109
|
|
|
//# Given the database has been set up with a user |
|
110
|
|
|
$record = new Record(); |
|
111
|
|
|
self::setupMockMysqlDatabase($record); |
|
112
|
|
|
//# And the force update flag is true |
|
113
|
|
|
$forceUpdate = false; |
|
114
|
|
|
//# And the account has been retrieved once |
|
115
|
|
|
AbstractSmrAccount::getAccount($record->account_id, $forceUpdate); |
|
116
|
|
|
//# When retrieving the account a second time |
|
117
|
|
|
AbstractSmrAccount::getAccount($record->account_id, $forceUpdate); |
|
118
|
|
|
//# Then verify multiple interactions with the database |
|
119
|
|
|
//# There are a total of two mocks in the container: |
|
120
|
|
|
//# One constructed here in this test, and one for the only time a database connection was spawned. |
|
121
|
|
|
$this->assertCount(2, m::getContainer()->getMocks()); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function test_get_account_by_name_happy_path() |
|
125
|
|
|
{ |
|
126
|
|
|
//# Given a record exists |
|
127
|
|
|
$record = new Record(); |
|
128
|
|
|
self::setupMockMysqlDatabase($record); |
|
129
|
|
|
//# When retrieving account by name |
|
130
|
|
|
$account = AbstractSmrAccount::getAccountByName($record->login); |
|
131
|
|
|
//# Then the record is found |
|
132
|
|
|
$this->assertEquals($record->account_id, $account->getAccountID()); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function test_get_account_by_name_returns_null_when_no_account_name_provided() |
|
136
|
|
|
{ |
|
137
|
|
|
//# Given a record exists |
|
138
|
|
|
$record = new Record(); |
|
139
|
|
|
self::setupMockMysqlDatabase($record); |
|
140
|
|
|
//# When retrieving account by null name |
|
141
|
|
|
$account = AbstractSmrAccount::getAccountByName(null); |
|
142
|
|
|
//# Then the record is null |
|
143
|
|
|
$this->assertNull($account); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function test_get_account_by_name_returns_null_when_no_record_found() |
|
147
|
|
|
{ |
|
148
|
|
|
//# Given no record exists |
|
149
|
|
|
self::setupMockMysqlDatabase(null); |
|
150
|
|
|
//# When retrieving account by name |
|
151
|
|
|
$account = AbstractSmrAccount::getAccountByName("any"); |
|
152
|
|
|
//# Then the record is null |
|
153
|
|
|
$this->assertNull($account); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function test_get_account_by_email_happy_path() |
|
157
|
|
|
{ |
|
158
|
|
|
//# Given a record exists |
|
159
|
|
|
$record = new Record(); |
|
160
|
|
|
self::setupMockMysqlDatabase($record); |
|
161
|
|
|
//# When retrieving account by email |
|
162
|
|
|
$account = AbstractSmrAccount::getAccountByEmail($record->email); |
|
163
|
|
|
//# Then the record is found |
|
164
|
|
|
$this->assertEquals($record->account_id, $account->getAccountID()); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function test_get_account_by_email_returns_null_when_no_email_provided() |
|
168
|
|
|
{ |
|
169
|
|
|
//# Given a record exists |
|
170
|
|
|
$record = new Record(); |
|
171
|
|
|
self::setupMockMysqlDatabase($record); |
|
172
|
|
|
//# When retrieving account by null email |
|
173
|
|
|
$account = AbstractSmrAccount::getAccountByEmail(null); |
|
174
|
|
|
//# Then the record is null |
|
175
|
|
|
$this->assertNull($account); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function test_get_account_by_email_returns_null_when_no_record_found() |
|
179
|
|
|
{ |
|
180
|
|
|
//# Given no record exists |
|
181
|
|
|
self::setupMockMysqlDatabase(null); |
|
182
|
|
|
//# When retrieving account by email |
|
183
|
|
|
$account = AbstractSmrAccount::getAccountByEmail("any"); |
|
184
|
|
|
//# Then the record is null |
|
185
|
|
|
$this->assertNull($account); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function test_get_account_by_discord_happy_path() |
|
189
|
|
|
{ |
|
190
|
|
|
//# Given a record exists |
|
191
|
|
|
$record = new Record(); |
|
192
|
|
|
self::setupMockMysqlDatabase($record); |
|
193
|
|
|
//# When retrieving account by discord |
|
194
|
|
|
$account = AbstractSmrAccount::getAccountByDiscordId($record->login); |
|
195
|
|
|
//# Then the record is found |
|
196
|
|
|
$this->assertEquals($record->account_id, $account->getAccountID()); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function test_get_account_by_discord_returns_null_when_no_discord_provided() |
|
200
|
|
|
{ |
|
201
|
|
|
//# Given a record exists |
|
202
|
|
|
$record = new Record(); |
|
203
|
|
|
self::setupMockMysqlDatabase($record); |
|
204
|
|
|
//# When retrieving account by null discord |
|
205
|
|
|
$account = AbstractSmrAccount::getAccountByDiscordId(null); |
|
206
|
|
|
//# Then the record is null |
|
207
|
|
|
$this->assertNull($account); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function test_get_account_by_discord_returns_null_when_no_record_found() |
|
211
|
|
|
{ |
|
212
|
|
|
//# Given no record exists |
|
213
|
|
|
self::setupMockMysqlDatabase(null); |
|
214
|
|
|
//# When retrieving account by discord |
|
215
|
|
|
$account = AbstractSmrAccount::getAccountByDiscordId("any"); |
|
216
|
|
|
//# Then the record is null |
|
217
|
|
|
$this->assertNull($account); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function test_get_account_by_irc_happy_path() |
|
221
|
|
|
{ |
|
222
|
|
|
//# Given a record exists |
|
223
|
|
|
$record = new Record(); |
|
224
|
|
|
self::setupMockMysqlDatabase($record); |
|
225
|
|
|
//# When retrieving account by irc |
|
226
|
|
|
$account = AbstractSmrAccount::getAccountByIrcNick($record->login); |
|
227
|
|
|
//# Then the record is found |
|
228
|
|
|
$this->assertEquals($record->account_id, $account->getAccountID()); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
public function test_get_account_by_irc_returns_null_when_no_irc_provided() |
|
232
|
|
|
{ |
|
233
|
|
|
//# Given a record exists |
|
234
|
|
|
$record = new Record(); |
|
235
|
|
|
self::setupMockMysqlDatabase($record); |
|
236
|
|
|
//# When retrieving account by null irc |
|
237
|
|
|
$account = AbstractSmrAccount::getAccountByIrcNick(null); |
|
238
|
|
|
//# Then the record is null |
|
239
|
|
|
$this->assertNull($account); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
public function test_get_account_by_irc_returns_null_when_no_record_found() |
|
243
|
|
|
{ |
|
244
|
|
|
//# Given no record exists |
|
245
|
|
|
self::setupMockMysqlDatabase(null); |
|
246
|
|
|
//# When retrieving account by irc |
|
247
|
|
|
$account = AbstractSmrAccount::getAccountByIrcNick("any"); |
|
248
|
|
|
//# Then the record is null |
|
249
|
|
|
$this->assertNull($account); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
///// |
|
253
|
|
|
public function test_get_account_by_social_happy_path() |
|
254
|
|
|
{ |
|
255
|
|
|
//# Given a record exists |
|
256
|
|
|
$record = new Record(); |
|
257
|
|
|
self::setupMockMysqlDatabase($record); |
|
258
|
|
|
//# And a valid social login |
|
259
|
|
|
$socialLogin = m::mock(SocialLogin::class)->shouldIgnoreMissing(); |
|
260
|
|
|
$socialLogin |
|
261
|
|
|
->expects() |
|
262
|
|
|
->isValid() |
|
263
|
|
|
->andReturns(true); |
|
264
|
|
|
//# When retrieving account by social |
|
265
|
|
|
$account = AbstractSmrAccount::getAccountBySocialLogin($socialLogin); |
|
|
|
|
|
|
266
|
|
|
//# Then the record is found |
|
267
|
|
|
$this->assertEquals($record->account_id, $account->getAccountID()); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
public function test_get_account_by_social_returns_null_when_social_invalid() |
|
271
|
|
|
{ |
|
272
|
|
|
//# Given a record exists |
|
273
|
|
|
$record = new Record(); |
|
274
|
|
|
self::setupMockMysqlDatabase($record); |
|
275
|
|
|
//# And an invalid social login |
|
276
|
|
|
$socialLogin = m::mock(SocialLogin::class)->shouldIgnoreMissing(); |
|
277
|
|
|
$socialLogin |
|
278
|
|
|
->expects() |
|
279
|
|
|
->isValid() |
|
280
|
|
|
->andReturns(false); |
|
281
|
|
|
//# When retrieving account by null social |
|
282
|
|
|
$account = AbstractSmrAccount::getAccountBySocialLogin($socialLogin); |
|
|
|
|
|
|
283
|
|
|
//# Then the record is null |
|
284
|
|
|
$this->assertNull($account); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
public function test_get_account_by_social_returns_null_when_no_record_found() |
|
288
|
|
|
{ |
|
289
|
|
|
//# Given no record exists |
|
290
|
|
|
self::setupMockMysqlDatabase(null); |
|
291
|
|
|
//# And a valid social login |
|
292
|
|
|
$socialLogin = m::mock(SocialLogin::class)->shouldIgnoreMissing(); |
|
293
|
|
|
$socialLogin |
|
294
|
|
|
->expects() |
|
295
|
|
|
->isValid() |
|
296
|
|
|
->andReturns(true); |
|
297
|
|
|
//# When retrieving account by social |
|
298
|
|
|
$account = AbstractSmrAccount::getAccountBySocialLogin($socialLogin); |
|
|
|
|
|
|
299
|
|
|
//# Then the record is null |
|
300
|
|
|
$this->assertNull($account); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
public function tearDown(): void |
|
304
|
|
|
{ |
|
305
|
|
|
m::close(); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
private static function setupMockMysqlDatabase(?Record $record): MockInterface |
|
309
|
|
|
{ |
|
310
|
|
|
//# Force the mock to be used in the autoloader |
|
311
|
|
|
$mysqlDatabase = m::mock("overload:" . SmrMySqlDatabase::class)->shouldIgnoreMissing(); |
|
312
|
|
|
if (isset($record)) { |
|
313
|
|
|
$mysqlDatabase |
|
314
|
|
|
->shouldReceive("escapeNumber") |
|
315
|
|
|
->with($record->account_id) |
|
316
|
|
|
->andReturn($record->account_id); |
|
317
|
|
|
$mysqlDatabase |
|
318
|
|
|
->shouldReceive("nextRecord") |
|
319
|
|
|
->andReturn($record); |
|
320
|
|
|
$mysqlDatabase |
|
321
|
|
|
->shouldReceive("getRow") |
|
322
|
|
|
->andReturn((array)$record); |
|
323
|
|
|
} |
|
324
|
|
|
return $mysqlDatabase; |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|