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