1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
4
|
|
|
|
5
|
|
|
use AbstractSmrAccount; |
6
|
|
|
use AccountNotFoundException; |
7
|
|
|
use Mockery as m; |
8
|
|
|
use SmrTest\BaseIntegrationTest; |
9
|
|
|
use SocialLogin; |
10
|
|
|
use SocialLogins\Facebook; |
11
|
|
|
|
12
|
|
|
class Record { |
13
|
|
|
public int $account_id = 1; |
14
|
|
|
public string $login = "login"; |
15
|
|
|
public string $password = "password"; |
16
|
|
|
public string $email = "email"; |
17
|
|
|
public string $last_login = "last login"; |
18
|
|
|
public string $validation_code = "validation code"; |
19
|
|
|
public string $offset = "offset"; |
20
|
|
|
public string $images = "Yes"; |
21
|
|
|
public string $fontsize = "font size"; |
22
|
|
|
public string $password_reset = "password reset"; |
23
|
|
|
public int $mail_banned = 0; |
24
|
|
|
public string $friendly_colour = "colour"; |
25
|
|
|
public string $neutral_colour = "colour"; |
26
|
|
|
public string $enemy_colour = "colour"; |
27
|
|
|
public string $css_link = "css link"; |
28
|
|
|
public string $referral_id = 'referral_id'; |
29
|
|
|
public string $max_rank_achieved = 'max_rank_achieved'; |
30
|
|
|
public string $hof_name = 'hof_name'; |
31
|
|
|
public string $discord_id = 'discord_id'; |
32
|
|
|
public string $irc_nick = 'irc_nick'; |
33
|
|
|
public string $date_short = 'date_short'; |
34
|
|
|
public string $time_short = 'time_short'; |
35
|
|
|
public string $template = 'template'; |
36
|
|
|
public string $colour_scheme = 'colour_scheme'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Class AbstractSmrAccountTest |
41
|
|
|
* @covers AbstractSmrAccount |
42
|
|
|
*/ |
43
|
|
|
class AbstractSmrAccountIntegrationTest extends BaseIntegrationTest { |
44
|
|
|
public function test_account_creation() { |
45
|
|
|
$account = AbstractSmrAccount::createAccount("test", "test", "[email protected]", 9, 0); |
46
|
|
|
$this->assertEquals("test", $account->getLogin()); |
47
|
|
|
$this->assertEquals("[email protected]", $account->getEmail()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/* |
51
|
|
|
* Can easily add forcerefresh tests by asserting original == account for forcerefresh = false |
52
|
|
|
* then can tests force refresh true by comparing account ids since the account detauls may get populated with |
53
|
|
|
* other stuff on refresh |
54
|
|
|
*/ |
55
|
|
|
public function test_get_account_by_account_id() { |
56
|
|
|
// Given the database has been set up with a user |
57
|
|
|
$account = AbstractSmrAccount::createAccount("test", "test", "[email protected]", 9, 0); |
58
|
|
|
// And there is no force update |
59
|
|
|
$forceUpdate = false; |
60
|
|
|
// When the account is retrieved by its ID |
61
|
|
|
$abstractSmrAccount = AbstractSmrAccount::getAccount($account->getAccountID(), $forceUpdate); |
62
|
|
|
// Then the integrity of the user is correct |
63
|
|
|
$this->assertEquals($account, $abstractSmrAccount); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function test_get_account_by_account_id_no_account_found_throws_exception() { |
67
|
|
|
$this->expectException(AccountNotFoundException::class); |
68
|
|
|
// Given there is no account record |
69
|
|
|
// When performing an account lookup by id |
70
|
|
|
AbstractSmrAccount::getAccount(123); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function test_get_account_by_name_happy_path() { |
74
|
|
|
// Given the database has been set up with a user |
75
|
|
|
$original = AbstractSmrAccount::createAccount("test", "test", "[email protected]", 9, 0); |
76
|
|
|
// When retrieving account by name |
77
|
|
|
$account = AbstractSmrAccount::getAccountByName($original->getLogin()); |
78
|
|
|
// Then the record is found |
79
|
|
|
$this->assertEquals($original, $account); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function test_get_account_by_name_returns_null_when_no_account_name_provided() { |
83
|
|
|
// When retrieving account by null name |
84
|
|
|
$account = AbstractSmrAccount::getAccountByName(null); |
85
|
|
|
// Then the record is null |
86
|
|
|
$this->assertNull($account); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function test_get_account_by_name_returns_null_when_no_record_found() { |
90
|
|
|
// Given no record exists |
91
|
|
|
// When retrieving account by name |
92
|
|
|
$account = AbstractSmrAccount::getAccountByName("any"); |
93
|
|
|
// Then the record is null |
94
|
|
|
$this->assertNull($account); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function test_get_account_by_email_happy_path() { |
98
|
|
|
// Given a record exists |
99
|
|
|
$original = AbstractSmrAccount::createAccount("test", "test", "[email protected]", 9, 0); |
100
|
|
|
// When retrieving account by email |
101
|
|
|
$account = AbstractSmrAccount::getAccountByEmail($original->getEmail()); |
102
|
|
|
// Then the record is found |
103
|
|
|
$this->assertEquals($original, $account); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function test_get_account_by_email_returns_null_when_no_email_provided() { |
107
|
|
|
// When retrieving account by null email |
108
|
|
|
$account = AbstractSmrAccount::getAccountByEmail(null); |
109
|
|
|
// Then the record is null |
110
|
|
|
$this->assertNull($account); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function test_get_account_by_email_returns_null_when_no_record_found() { |
114
|
|
|
// Given no record exists |
115
|
|
|
// When retrieving account by email |
116
|
|
|
$account = AbstractSmrAccount::getAccountByEmail("any"); |
117
|
|
|
// Then the record is null |
118
|
|
|
$this->assertNull($account); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function test_get_account_by_discord_happy_path() { |
122
|
|
|
// Given a record exists |
123
|
|
|
// Given the database has been set up with a user |
124
|
|
|
$original = AbstractSmrAccount::createAccount("test", "test", "[email protected]", 9, 0); |
125
|
|
|
$original->setDiscordId("123"); |
126
|
|
|
$original->update(); |
127
|
|
|
// When retrieving account by discord |
128
|
|
|
$account = AbstractSmrAccount::getAccountByDiscordId($original->getDiscordId(), true); |
129
|
|
|
// Then the record is found |
130
|
|
|
$this->assertEquals($original->getAccountID(), $account->getAccountID()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function test_get_account_by_discord_returns_null_when_no_discord_provided() { |
134
|
|
|
// When retrieving account by null discord |
135
|
|
|
$account = AbstractSmrAccount::getAccountByDiscordId(null); |
136
|
|
|
// Then the record is null |
137
|
|
|
$this->assertNull($account); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function test_get_account_by_discord_returns_null_when_no_record_found() { |
141
|
|
|
// Given no record exists |
142
|
|
|
// When retrieving account by discord |
143
|
|
|
$account = AbstractSmrAccount::getAccountByDiscordId("any"); |
144
|
|
|
// Then the record is null |
145
|
|
|
$this->assertNull($account); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function test_get_account_by_irc_happy_path() { |
149
|
|
|
// Given a record exists |
150
|
|
|
// Given the database has been set up with a user |
151
|
|
|
$original = AbstractSmrAccount::createAccount("test", "test", "[email protected]", 9, 0); |
152
|
|
|
$original->setIrcNick("nick"); |
153
|
|
|
$original->update(); |
154
|
|
|
// When retrieving account by irc |
155
|
|
|
$account = AbstractSmrAccount::getAccountByIrcNick($original->getIrcNick(), true); |
156
|
|
|
// Then the record is found |
157
|
|
|
$this->assertEquals($original->getAccountID(), $account->getAccountID()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function test_get_account_by_irc_returns_null_when_no_irc_provided() { |
161
|
|
|
// When retrieving account by null irc |
162
|
|
|
$account = AbstractSmrAccount::getAccountByIrcNick(null); |
163
|
|
|
// Then the record is null |
164
|
|
|
$this->assertNull($account); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function test_get_account_by_irc_returns_null_when_no_record_found() { |
168
|
|
|
// Given no record exists |
169
|
|
|
// When retrieving account by irc |
170
|
|
|
$account = AbstractSmrAccount::getAccountByIrcNick("any"); |
171
|
|
|
// Then the record is null |
172
|
|
|
$this->assertNull($account); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function test_get_account_by_social_happy_path() { |
176
|
|
|
// Given a record exists |
177
|
|
|
$orignal = AbstractSmrAccount::createAccount("test", "test", "[email protected]", 9, 0); |
178
|
|
|
$orignal->addAuthMethod("facebook", $orignal->getAccountID()); |
179
|
|
|
// And a valid social login |
180
|
|
|
$socialLogin = m::mock(Facebook::class)->shouldIgnoreMissing(); |
181
|
|
|
$socialLogin |
182
|
|
|
->expects() |
183
|
|
|
->isValid() |
184
|
|
|
->andReturns(true); |
185
|
|
|
$socialLogin |
186
|
|
|
->expects() |
187
|
|
|
->getLoginType() |
188
|
|
|
->andReturns("facebook"); |
189
|
|
|
$socialLogin |
190
|
|
|
->expects() |
191
|
|
|
->getUserId() |
192
|
|
|
->andReturns($orignal->getAccountID()); |
193
|
|
|
// When retrieving account by social |
194
|
|
|
$account = AbstractSmrAccount::getAccountBySocialLogin($socialLogin, true); |
|
|
|
|
195
|
|
|
// Then the record is found |
196
|
|
|
$this->assertEquals($orignal->getAccountID(), $account->getAccountID()); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function test_get_account_by_social_returns_null_when_social_invalid() { |
200
|
|
|
// Given an invalid social login |
201
|
|
|
$socialLogin = m::mock(SocialLogin::class)->shouldIgnoreMissing(); |
202
|
|
|
$socialLogin |
203
|
|
|
->expects() |
204
|
|
|
->isValid() |
205
|
|
|
->andReturns(false); |
206
|
|
|
// When retrieving account by null social |
207
|
|
|
$account = AbstractSmrAccount::getAccountBySocialLogin($socialLogin); |
|
|
|
|
208
|
|
|
// Then the record is null |
209
|
|
|
$this->assertNull($account); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function test_get_account_by_social_returns_null_when_no_record_found() { |
213
|
|
|
// Given no record exists |
214
|
|
|
// And a valid social login |
215
|
|
|
$socialLogin = m::mock(SocialLogin::class)->shouldIgnoreMissing(); |
216
|
|
|
$socialLogin |
217
|
|
|
->expects() |
218
|
|
|
->isValid() |
219
|
|
|
->andReturns(true); |
220
|
|
|
$socialLogin |
221
|
|
|
->expects() |
222
|
|
|
->getLoginType() |
223
|
|
|
->andReturns("facebook"); |
224
|
|
|
$socialLogin |
225
|
|
|
->expects() |
226
|
|
|
->getUserId() |
227
|
|
|
->andReturns(123); |
228
|
|
|
// When retrieving account by social |
229
|
|
|
$account = AbstractSmrAccount::getAccountBySocialLogin($socialLogin); |
|
|
|
|
230
|
|
|
// Then the record is null |
231
|
|
|
$this->assertNull($account); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|