1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
4
|
|
|
|
5
|
|
|
use AbstractSmrAccount; |
6
|
|
|
use Smr\Exceptions\AccountNotFound; |
7
|
|
|
use Smr\SocialLogin\Facebook; |
8
|
|
|
use SmrTest\BaseIntegrationSpec; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers AbstractSmrAccount |
12
|
|
|
*/ |
13
|
|
|
class AbstractSmrAccountIntegrationTest extends BaseIntegrationSpec { |
14
|
|
|
|
15
|
|
|
protected function tablesToTruncate(): array { |
16
|
|
|
return ['account', 'account_auth']; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
protected function setUp(): void { |
20
|
|
|
AbstractSmrAccount::clearCache(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function test_createAccount(): void { |
24
|
|
|
$login = 'test'; |
25
|
|
|
$password = 'pw'; |
26
|
|
|
$email = '[email protected]'; |
27
|
|
|
$tz = 9; |
28
|
|
|
$referral = 0; |
29
|
|
|
$account = AbstractSmrAccount::createAccount($login, $password, $email, $tz, $referral); |
30
|
|
|
$this->assertSame($login, $account->getLogin()); |
31
|
|
|
$this->assertSame($email, $account->getEmail()); |
32
|
|
|
$this->assertSame($tz, $account->getOffset()); |
33
|
|
|
$this->assertSame($referral, $account->getReferrerID()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function test_createAccount_throws_if_referrer_does_not_exist(): void { |
37
|
|
|
$this->expectException(AccountNotFound::class); |
38
|
|
|
$this->expectExceptionMessage('Account ID 123 does not exist'); |
39
|
|
|
AbstractSmrAccount::createAccount('test', 'test', '[email protected]', 9, 123); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function test_get_account_by_account_id(): void { |
43
|
|
|
// Given the database has been set up with a user |
44
|
|
|
$original = AbstractSmrAccount::createAccount('test', 'test', '[email protected]', 9, 0); |
45
|
|
|
// And there is no force update |
46
|
|
|
$forceUpdate = false; |
47
|
|
|
// When the account is retrieved by its ID |
48
|
|
|
$account = AbstractSmrAccount::getAccount($original->getAccountID(), $forceUpdate); |
49
|
|
|
// Without forceUpdate, the two objects should be the same in memory |
50
|
|
|
$this->assertSame($original, $account); |
51
|
|
|
|
52
|
|
|
// With forceUpdate, the objects should be identical, but not the same |
53
|
|
|
$forceUpdate = true; |
54
|
|
|
$account = AbstractSmrAccount::getAccount($original->getAccountID(), $forceUpdate); |
55
|
|
|
$this->assertNotSame($original, $account); |
56
|
|
|
$this->assertEquals($original, $account); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function test_get_account_by_account_id_no_account_found_throws_exception(): void { |
60
|
|
|
$this->expectException(AccountNotFound::class); |
61
|
|
|
// Given there is no account record |
62
|
|
|
// When performing an account lookup by id |
63
|
|
|
AbstractSmrAccount::getAccount(123); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function test_get_account_by_name_happy_path(): void { |
67
|
|
|
// Given the database has been set up with a user |
68
|
|
|
$original = AbstractSmrAccount::createAccount('test', 'test', '[email protected]', 9, 0); |
69
|
|
|
// When retrieving account by name |
70
|
|
|
$account = AbstractSmrAccount::getAccountByName($original->getLogin()); |
71
|
|
|
// Then the record is found |
72
|
|
|
$this->assertSame($original, $account); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function test_get_account_by_name_throws_when_no_account_name_provided(): void { |
76
|
|
|
// When retrieving account by empty string name, exception is thrown |
77
|
|
|
$this->expectException(AccountNotFound::class); |
78
|
|
|
$this->expectExceptionMessage('Account login not found'); |
79
|
|
|
AbstractSmrAccount::getAccountByName(''); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function test_get_account_by_name_throws_when_no_record_found(): void { |
83
|
|
|
// When retrieving account by name, exception is thrown if no record exists |
84
|
|
|
$this->expectException(AccountNotFound::class); |
85
|
|
|
$this->expectExceptionMessage('Account login not found'); |
86
|
|
|
AbstractSmrAccount::getAccountByName('does_not_exist'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function test_get_account_by_email_happy_path(): void { |
90
|
|
|
// Given a record exists |
91
|
|
|
$original = AbstractSmrAccount::createAccount('test', 'test', '[email protected]', 9, 0); |
92
|
|
|
// When retrieving account by email |
93
|
|
|
$account = AbstractSmrAccount::getAccountByEmail($original->getEmail()); |
94
|
|
|
// Then the record is found |
95
|
|
|
$this->assertSame($original, $account); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function test_get_account_by_email_throws_when_no_email_provided(): void { |
99
|
|
|
// When retrieving account by empty string email, exception is thrown |
100
|
|
|
$this->expectException(AccountNotFound::class); |
101
|
|
|
$this->expectExceptionMessage('Account email not found'); |
102
|
|
|
AbstractSmrAccount::getAccountByEmail(''); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function test_get_account_by_email_throws_when_no_record_found(): void { |
106
|
|
|
// When retrieving account by email, exception is thrown if no record exists |
107
|
|
|
$this->expectException(AccountNotFound::class); |
108
|
|
|
$this->expectExceptionMessage('Account email not found'); |
109
|
|
|
AbstractSmrAccount::getAccountByEmail('does_not_exist'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function test_get_account_by_discord_happy_path(): void { |
113
|
|
|
// Given a record exists |
114
|
|
|
// Given the database has been set up with a user |
115
|
|
|
$original = AbstractSmrAccount::createAccount('test', 'test', '[email protected]', 9, 0); |
116
|
|
|
$original->setDiscordId('123'); |
117
|
|
|
$original->update(); |
118
|
|
|
// When retrieving account by discord |
119
|
|
|
$account = AbstractSmrAccount::getAccountByDiscordId($original->getDiscordId(), true); |
120
|
|
|
// Then the record is found |
121
|
|
|
$this->assertSame($original->getAccountID(), $account->getAccountID()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function test_get_account_by_discord_throws_when_no_discord_provided(): void { |
125
|
|
|
// When retrieving account by empty string discord ID, exception is thrown |
126
|
|
|
$this->expectException(AccountNotFound::class); |
127
|
|
|
$this->expectExceptionMessage('Account discord ID not found'); |
128
|
|
|
AbstractSmrAccount::getAccountByDiscordId(''); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function test_get_account_by_discord_throws_when_no_record_found(): void { |
132
|
|
|
// When retrieving account by discord, exception is thrown if no record exists. |
133
|
|
|
$this->expectException(AccountNotFound::class); |
134
|
|
|
$this->expectExceptionMessage('Account discord ID not found'); |
135
|
|
|
AbstractSmrAccount::getAccountByDiscordId('does_not_exist'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function test_get_account_by_irc_happy_path(): void { |
139
|
|
|
// Given a record exists |
140
|
|
|
// Given the database has been set up with a user |
141
|
|
|
$original = AbstractSmrAccount::createAccount('test', 'test', '[email protected]', 9, 0); |
142
|
|
|
$original->setIrcNick('nick'); |
143
|
|
|
$original->update(); |
144
|
|
|
// When retrieving account by irc |
145
|
|
|
$account = AbstractSmrAccount::getAccountByIrcNick($original->getIrcNick(), true); |
146
|
|
|
// Then the record is found |
147
|
|
|
$this->assertSame($original->getAccountID(), $account->getAccountID()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function test_get_account_by_irc_throws_when_no_irc_provided(): void { |
151
|
|
|
// When retrieving account by empty string irc, exception is thrown |
152
|
|
|
$this->expectException(AccountNotFound::class); |
153
|
|
|
$this->expectExceptionMessage('Account IRC nick not found'); |
154
|
|
|
AbstractSmrAccount::getAccountByIrcNick(''); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function test_get_account_by_irc_returns_null_when_no_record_found(): void { |
158
|
|
|
// When retrieving account by irc, exception is thrown if no record exists. |
159
|
|
|
$this->expectException(AccountNotFound::class); |
160
|
|
|
$this->expectExceptionMessage('Account IRC nick not found'); |
161
|
|
|
AbstractSmrAccount::getAccountByIrcNick('does_not_exist'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function test_get_account_by_social_happy_path(): void { |
165
|
|
|
|
166
|
|
|
// Given a record exists |
167
|
|
|
$original = AbstractSmrAccount::createAccount('test', 'test', '[email protected]', 9, 0); |
168
|
|
|
$authUserID = 'MySocialUserID'; |
169
|
|
|
$original->addAuthMethod(Facebook::getLoginType(), $authUserID); |
170
|
|
|
// And a valid social login |
171
|
|
|
/* |
172
|
|
|
* Unfortunately we cannot use the simple createMock() method, because the SocialLogin class uses |
173
|
|
|
* a static method for getLoginType(). PHPUnit cannot operate on static methods, so it throws a warning. |
174
|
|
|
* Instead we create a partial mock, and all other methods will call their default method, and prevent the warning |
175
|
|
|
* that causes PHPUnit to fail due to "warnings" or "risky" tests. |
176
|
|
|
*/ |
177
|
|
|
$isValid = 'isValid'; |
178
|
|
|
$getUserId = 'getUserId'; |
179
|
|
|
$socialLogin = $this->createPartialMock(Facebook::class, [$isValid, $getUserId]); |
180
|
|
|
$socialLogin |
181
|
|
|
->expects(self::once()) |
182
|
|
|
->method($isValid) |
183
|
|
|
->willReturn(true); |
184
|
|
|
$socialLogin |
185
|
|
|
->expects(self::once()) |
186
|
|
|
->method($getUserId) |
187
|
|
|
->willReturn($authUserID); |
188
|
|
|
// When retrieving account by social |
189
|
|
|
$account = AbstractSmrAccount::getAccountBySocialLogin($socialLogin, true); |
190
|
|
|
// Then the record is found |
191
|
|
|
$this->assertSame($original->getAccountID(), $account->getAccountID()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function test_get_account_by_social_throws_when_social_invalid(): void { |
195
|
|
|
// Given an invalid social login |
196
|
|
|
$socialLogin = $this->createMock(Facebook::class); |
197
|
|
|
$socialLogin |
198
|
|
|
->expects(self::once()) |
199
|
|
|
->method('isValid') |
200
|
|
|
->willReturn(false); |
201
|
|
|
// When retrieving account by invalid social, exception is thrown |
202
|
|
|
$this->expectException(AccountNotFound::class); |
203
|
|
|
$this->expectExceptionMessage('Account social login not found'); |
204
|
|
|
AbstractSmrAccount::getAccountBySocialLogin($socialLogin); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function test_get_account_by_social_throws_when_no_record_found(): void { |
208
|
|
|
// Given no record exists |
209
|
|
|
// And a valid social login |
210
|
|
|
/* |
211
|
|
|
* Unfortunately we cannot use the simple createMock() method, because the SocialLogin class uses |
212
|
|
|
* a static method for getLoginType(). PHPUnit cannot operate on static methods, so it throws a warning. |
213
|
|
|
* Instead we create a partial mock, and all other methods will call their default method, and prevent the warning |
214
|
|
|
* that causes PHPUnit to fail due to "warnings" or "risky" tests. |
215
|
|
|
*/ |
216
|
|
|
$isValid = 'isValid'; |
217
|
|
|
$getUserId = 'getUserId'; |
218
|
|
|
$socialLogin = $this->createPartialMock(Facebook::class, [$isValid, $getUserId]); |
219
|
|
|
$socialLogin |
220
|
|
|
->expects(self::once()) |
221
|
|
|
->method($isValid) |
222
|
|
|
->willReturn(true); |
223
|
|
|
$socialLogin |
224
|
|
|
->expects(self::once()) |
225
|
|
|
->method($getUserId) |
226
|
|
|
->willReturn('123'); |
227
|
|
|
// When retrieving account by social, exception is thrown if no record exists |
228
|
|
|
$this->expectException(AccountNotFound::class); |
229
|
|
|
$this->expectExceptionMessage('Account social login not found'); |
230
|
|
|
AbstractSmrAccount::getAccountBySocialLogin($socialLogin); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function test_date_format_methods(): void { |
234
|
|
|
$account = AbstractSmrAccount::createAccount('test', 'test', '[email protected]', 9, 0); |
235
|
|
|
|
236
|
|
|
// Arbitrary epoch for testing (Sat, 24 Apr 2021 01:39:51 GMT) |
237
|
|
|
$epoch = 1619228391; |
238
|
|
|
|
239
|
|
|
// Test default formats |
240
|
|
|
self::assertSame('2021-04-24', date($account->getDateFormat(), $epoch)); |
241
|
|
|
self::assertSame('1:39:51 AM', date($account->getTimeFormat(), $epoch)); |
242
|
|
|
|
243
|
|
|
// Test combined formats |
244
|
|
|
self::assertSame('2021-04-24 1:39:51 AM', date($account->getDateTimeFormat(), $epoch)); |
245
|
|
|
self::assertSame('2021-04-24<br />1:39:51 AM', date($account->getDateTimeFormatSplit(), $epoch)); |
246
|
|
|
|
247
|
|
|
// Now modify the formats |
248
|
|
|
$account->setDateFormat('Y M D'); |
249
|
|
|
$account->setTimeFormat('H i s'); |
250
|
|
|
|
251
|
|
|
// Test the modified formats |
252
|
|
|
self::assertSame('2021 Apr Sat', date($account->getDateFormat(), $epoch)); |
253
|
|
|
self::assertSame('01 39 51', date($account->getTimeFormat(), $epoch)); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
} |
257
|
|
|
|