1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
4
|
|
|
|
5
|
|
|
use AbstractSmrAccount; |
6
|
|
|
use AccountNotFoundException; |
7
|
|
|
use SmrTest\BaseIntegrationSpec; |
8
|
|
|
use SocialLogins\Facebook; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class AbstractSmrAccountTest |
12
|
|
|
* @covers AbstractSmrAccount |
13
|
|
|
*/ |
14
|
|
|
class AbstractSmrAccountIntegrationTest extends BaseIntegrationSpec { |
15
|
|
|
public function test_account_creation() { |
16
|
|
|
$account = AbstractSmrAccount::createAccount("test", "test", "[email protected]", 9, 0); |
17
|
|
|
$this->assertEquals("test", $account->getLogin()); |
18
|
|
|
$this->assertEquals("[email protected]", $account->getEmail()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/* |
22
|
|
|
* Can easily add forcerefresh tests by asserting original == account for forcerefresh = false |
23
|
|
|
* then can tests force refresh true by comparing account ids since the account detauls may get populated with |
24
|
|
|
* other stuff on refresh |
25
|
|
|
*/ |
26
|
|
|
public function test_get_account_by_account_id() { |
27
|
|
|
// Given the database has been set up with a user |
28
|
|
|
$account = AbstractSmrAccount::createAccount("test", "test", "[email protected]", 9, 0); |
29
|
|
|
// And there is no force update |
30
|
|
|
$forceUpdate = false; |
31
|
|
|
// When the account is retrieved by its ID |
32
|
|
|
$abstractSmrAccount = AbstractSmrAccount::getAccount($account->getAccountID(), $forceUpdate); |
33
|
|
|
// Then the integrity of the user is correct |
34
|
|
|
$this->assertEquals($account, $abstractSmrAccount); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function test_get_account_by_account_id_no_account_found_throws_exception() { |
38
|
|
|
$this->expectException(AccountNotFoundException::class); |
39
|
|
|
// Given there is no account record |
40
|
|
|
// When performing an account lookup by id |
41
|
|
|
AbstractSmrAccount::getAccount(123); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function test_get_account_by_name_happy_path() { |
45
|
|
|
// Given the database has been set up with a user |
46
|
|
|
$original = AbstractSmrAccount::createAccount("test", "test", "[email protected]", 9, 0); |
47
|
|
|
// When retrieving account by name |
48
|
|
|
$account = AbstractSmrAccount::getAccountByName($original->getLogin()); |
49
|
|
|
// Then the record is found |
50
|
|
|
$this->assertEquals($original, $account); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function test_get_account_by_name_returns_null_when_no_account_name_provided() { |
54
|
|
|
// When retrieving account by null name |
55
|
|
|
$account = AbstractSmrAccount::getAccountByName(null); |
56
|
|
|
// Then the record is null |
57
|
|
|
$this->assertNull($account); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function test_get_account_by_name_returns_null_when_no_record_found() { |
61
|
|
|
// Given no record exists |
62
|
|
|
// When retrieving account by name |
63
|
|
|
$account = AbstractSmrAccount::getAccountByName("any"); |
64
|
|
|
// Then the record is null |
65
|
|
|
$this->assertNull($account); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function test_get_account_by_email_happy_path() { |
69
|
|
|
// Given a record exists |
70
|
|
|
$original = AbstractSmrAccount::createAccount("test", "test", "[email protected]", 9, 0); |
71
|
|
|
// When retrieving account by email |
72
|
|
|
$account = AbstractSmrAccount::getAccountByEmail($original->getEmail()); |
73
|
|
|
// Then the record is found |
74
|
|
|
$this->assertEquals($original, $account); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function test_get_account_by_email_returns_null_when_no_email_provided() { |
78
|
|
|
// When retrieving account by null email |
79
|
|
|
$account = AbstractSmrAccount::getAccountByEmail(null); |
80
|
|
|
// Then the record is null |
81
|
|
|
$this->assertNull($account); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function test_get_account_by_email_returns_null_when_no_record_found() { |
85
|
|
|
// Given no record exists |
86
|
|
|
// When retrieving account by email |
87
|
|
|
$account = AbstractSmrAccount::getAccountByEmail("any"); |
88
|
|
|
// Then the record is null |
89
|
|
|
$this->assertNull($account); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function test_get_account_by_discord_happy_path() { |
93
|
|
|
// Given a record exists |
94
|
|
|
// Given the database has been set up with a user |
95
|
|
|
$original = AbstractSmrAccount::createAccount("test", "test", "[email protected]", 9, 0); |
96
|
|
|
$original->setDiscordId("123"); |
97
|
|
|
$original->update(); |
98
|
|
|
// When retrieving account by discord |
99
|
|
|
$account = AbstractSmrAccount::getAccountByDiscordId($original->getDiscordId(), true); |
100
|
|
|
// Then the record is found |
101
|
|
|
$this->assertEquals($original->getAccountID(), $account->getAccountID()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function test_get_account_by_discord_returns_null_when_no_discord_provided() { |
105
|
|
|
// When retrieving account by null discord |
106
|
|
|
$account = AbstractSmrAccount::getAccountByDiscordId(null); |
107
|
|
|
// Then the record is null |
108
|
|
|
$this->assertNull($account); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function test_get_account_by_discord_returns_null_when_no_record_found() { |
112
|
|
|
// Given no record exists |
113
|
|
|
// When retrieving account by discord |
114
|
|
|
$account = AbstractSmrAccount::getAccountByDiscordId("any"); |
115
|
|
|
// Then the record is null |
116
|
|
|
$this->assertNull($account); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function test_get_account_by_irc_happy_path() { |
120
|
|
|
// Given a record exists |
121
|
|
|
// Given the database has been set up with a user |
122
|
|
|
$original = AbstractSmrAccount::createAccount("test", "test", "[email protected]", 9, 0); |
123
|
|
|
$original->setIrcNick("nick"); |
124
|
|
|
$original->update(); |
125
|
|
|
// When retrieving account by irc |
126
|
|
|
$account = AbstractSmrAccount::getAccountByIrcNick($original->getIrcNick(), true); |
127
|
|
|
// Then the record is found |
128
|
|
|
$this->assertEquals($original->getAccountID(), $account->getAccountID()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function test_get_account_by_irc_returns_null_when_no_irc_provided() { |
132
|
|
|
// When retrieving account by null irc |
133
|
|
|
$account = AbstractSmrAccount::getAccountByIrcNick(null); |
134
|
|
|
// Then the record is null |
135
|
|
|
$this->assertNull($account); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function test_get_account_by_irc_returns_null_when_no_record_found() { |
139
|
|
|
// Given no record exists |
140
|
|
|
// When retrieving account by irc |
141
|
|
|
$account = AbstractSmrAccount::getAccountByIrcNick("any"); |
142
|
|
|
// Then the record is null |
143
|
|
|
$this->assertNull($account); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function test_get_account_by_social_happy_path() { |
147
|
|
|
|
148
|
|
|
// Given a record exists |
149
|
|
|
$original = AbstractSmrAccount::createAccount("test", "test", "[email protected]", 9, 0); |
150
|
|
|
$original->addAuthMethod(Facebook::getLoginType(), $original->getAccountID()); |
151
|
|
|
// And a valid social login |
152
|
|
|
/* |
153
|
|
|
* Unfortunately we cannot use the simple createMock() method, because the SocialLogin class uses |
154
|
|
|
* a static method for getLoginType(). PHPUnit cannot operate on static methods, so it throws a warning. |
155
|
|
|
* Instead we create a partial mock, and all other methods will call their default method, and prevent the warning |
156
|
|
|
* that causes PHPUnit to fail due to "warnings" or "risky" tests. |
157
|
|
|
*/ |
158
|
|
|
$isValid = "isValid"; |
159
|
|
|
$getUserId = "getUserId"; |
160
|
|
|
$socialLogin = $this->createPartialMock(Facebook::class, array($isValid, $getUserId)); |
161
|
|
|
$socialLogin |
162
|
|
|
->expects(self::once()) |
163
|
|
|
->method($isValid) |
164
|
|
|
->willReturn(true); |
165
|
|
|
$socialLogin |
166
|
|
|
->expects(self::once()) |
167
|
|
|
->method($getUserId) |
168
|
|
|
->willReturn($original->getAccountID()); |
169
|
|
|
// When retrieving account by social |
170
|
|
|
$account = AbstractSmrAccount::getAccountBySocialLogin($socialLogin, true); |
171
|
|
|
// Then the record is found |
172
|
|
|
$this->assertEquals($original->getAccountID(), $account->getAccountID()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function test_get_account_by_social_returns_null_when_social_invalid() { |
176
|
|
|
// Given an invalid social login |
177
|
|
|
$socialLogin = $this->createMock(Facebook::class); |
178
|
|
|
$socialLogin |
179
|
|
|
->expects(self::once()) |
180
|
|
|
->method("isValid") |
181
|
|
|
->willReturn(false); |
182
|
|
|
// When retrieving account by null social |
183
|
|
|
$account = AbstractSmrAccount::getAccountBySocialLogin($socialLogin); |
184
|
|
|
// Then the record is null |
185
|
|
|
$this->assertNull($account); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function test_get_account_by_social_returns_null_when_no_record_found() { |
189
|
|
|
// Given no record exists |
190
|
|
|
// And a valid social login |
191
|
|
|
/* |
192
|
|
|
* Unfortunately we cannot use the simple createMock() method, because the SocialLogin class uses |
193
|
|
|
* a static method for getLoginType(). PHPUnit cannot operate on static methods, so it throws a warning. |
194
|
|
|
* Instead we create a partial mock, and all other methods will call their default method, and prevent the warning |
195
|
|
|
* that causes PHPUnit to fail due to "warnings" or "risky" tests. |
196
|
|
|
*/ |
197
|
|
|
$isValid = "isValid"; |
198
|
|
|
$getUserId = "getUserId"; |
199
|
|
|
$socialLogin = $this->createPartialMock(Facebook::class, array($isValid, $getUserId)); |
200
|
|
|
$socialLogin |
201
|
|
|
->expects(self::once()) |
202
|
|
|
->method($isValid) |
203
|
|
|
->willReturn(true); |
204
|
|
|
$socialLogin |
205
|
|
|
->expects(self::once()) |
206
|
|
|
->method($getUserId) |
207
|
|
|
->willReturn("123"); |
208
|
|
|
// When retrieving account by social |
209
|
|
|
$account = AbstractSmrAccount::getAccountBySocialLogin($socialLogin); |
210
|
|
|
// Then the record is null |
211
|
|
|
$this->assertNull($account); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|