Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#1046)
by Dan
05:18
created

AbstractSmrAccountIntegrationTest   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 103
c 4
b 0
f 1
dl 0
loc 241
rs 10
wmc 21

21 Methods

Rating   Name   Duplication   Size   Complexity  
A test_get_account_by_account_id() 0 15 1
A test_get_account_by_email_returns_null_when_no_email_provided() 0 5 1
A test_get_account_by_irc_returns_null_when_no_record_found() 0 6 1
A test_createAccount() 0 11 1
A test_get_account_by_name_returns_null_when_no_account_name_provided() 0 5 1
A test_createAccount_throws_if_referrer_does_not_exist() 0 4 1
A test_get_account_by_irc_happy_path() 0 10 1
A test_get_account_by_account_id_no_account_found_throws_exception() 0 5 1
A test_get_account_by_social_happy_path() 0 28 1
A test_get_account_by_name_returns_null_when_no_record_found() 0 6 1
A setUp() 0 2 1
A test_get_account_by_social_returns_null_when_no_record_found() 0 24 1
A test_get_account_by_discord_returns_null_when_no_discord_provided() 0 5 1
A test_get_account_by_irc_returns_null_when_no_irc_provided() 0 5 1
A test_get_account_by_email_returns_null_when_no_record_found() 0 6 1
A test_get_account_by_social_returns_null_when_social_invalid() 0 11 1
A test_get_account_by_discord_happy_path() 0 10 1
A test_get_account_by_email_happy_path() 0 7 1
A test_get_account_by_discord_returns_null_when_no_record_found() 0 6 1
A test_get_account_by_name_happy_path() 0 7 1
A test_date_format_methods() 0 21 1
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 Smr\SocialLogin\Facebook;
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() {
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() {
33
		$this->expectException(AccountNotFoundException::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() {
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() {
56
		$this->expectException(AccountNotFoundException::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() {
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_returns_null_when_no_account_name_provided() {
72
		// When retrieving account by empty string name
73
		$account = AbstractSmrAccount::getAccountByName('');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $account is correct as AbstractSmrAccount::getAccountByName('') targeting AbstractSmrAccount::getAccountByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
74
		// Then the record is null
75
		$this->assertNull($account);
76
	}
77
78
	public function test_get_account_by_name_returns_null_when_no_record_found() {
79
		// Given no record exists
80
		// When retrieving account by name
81
		$account = AbstractSmrAccount::getAccountByName('any');
82
		// Then the record is null
83
		$this->assertNull($account);
84
	}
85
86
	public function test_get_account_by_email_happy_path() {
87
		// Given a record exists
88
		$original = AbstractSmrAccount::createAccount('test', 'test', '[email protected]', 9, 0);
89
		// When retrieving account by email
90
		$account = AbstractSmrAccount::getAccountByEmail($original->getEmail());
91
		// Then the record is found
92
		$this->assertSame($original, $account);
93
	}
94
95
	public function test_get_account_by_email_returns_null_when_no_email_provided() {
96
		// When retrieving account by null email
97
		$account = AbstractSmrAccount::getAccountByEmail(null);
98
		// Then the record is null
99
		$this->assertNull($account);
100
	}
101
102
	public function test_get_account_by_email_returns_null_when_no_record_found() {
103
		// Given no record exists
104
		// When retrieving account by email
105
		$account = AbstractSmrAccount::getAccountByEmail('any');
106
		// Then the record is null
107
		$this->assertNull($account);
108
	}
109
110
	public function test_get_account_by_discord_happy_path() {
111
		// Given a record exists
112
		// Given the database has been set up with a user
113
		$original = AbstractSmrAccount::createAccount('test', 'test', '[email protected]', 9, 0);
114
		$original->setDiscordId('123');
115
		$original->update();
116
		// When retrieving account by discord
117
		$account = AbstractSmrAccount::getAccountByDiscordId($original->getDiscordId(), true);
118
		// Then the record is found
119
		$this->assertSame($original->getAccountID(), $account->getAccountID());
120
	}
121
122
	public function test_get_account_by_discord_returns_null_when_no_discord_provided() {
123
		// When retrieving account by null discord
124
		$account = AbstractSmrAccount::getAccountByDiscordId(null);
125
		// Then the record is null
126
		$this->assertNull($account);
127
	}
128
129
	public function test_get_account_by_discord_returns_null_when_no_record_found() {
130
		// Given no record exists
131
		// When retrieving account by discord
132
		$account = AbstractSmrAccount::getAccountByDiscordId('any');
133
		// Then the record is null
134
		$this->assertNull($account);
135
	}
136
137
	public function test_get_account_by_irc_happy_path() {
138
		// Given a record exists
139
		// Given the database has been set up with a user
140
		$original = AbstractSmrAccount::createAccount('test', 'test', '[email protected]', 9, 0);
141
		$original->setIrcNick('nick');
142
		$original->update();
143
		// When retrieving account by irc
144
		$account = AbstractSmrAccount::getAccountByIrcNick($original->getIrcNick(), true);
145
		// Then the record is found
146
		$this->assertSame($original->getAccountID(), $account->getAccountID());
147
	}
148
149
	public function test_get_account_by_irc_returns_null_when_no_irc_provided() {
150
		// When retrieving account by null irc
151
		$account = AbstractSmrAccount::getAccountByIrcNick(null);
152
		// Then the record is null
153
		$this->assertNull($account);
154
	}
155
156
	public function test_get_account_by_irc_returns_null_when_no_record_found() {
157
		// Given no record exists
158
		// When retrieving account by irc
159
		$account = AbstractSmrAccount::getAccountByIrcNick('any');
160
		// Then the record is null
161
		$this->assertNull($account);
162
	}
163
164
	public function test_get_account_by_social_happy_path() {
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, array($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_returns_null_when_social_invalid() {
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 null social
202
		$account = AbstractSmrAccount::getAccountBySocialLogin($socialLogin);
203
		// Then the record is null
204
		$this->assertNull($account);
205
	}
206
207
	public function test_get_account_by_social_returns_null_when_no_record_found() {
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, array($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
228
		$account = AbstractSmrAccount::getAccountBySocialLogin($socialLogin);
229
		// Then the record is null
230
		$this->assertNull($account);
231
	}
232
233
	public function test_date_format_methods() {
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('24/4/2021', date($account->getDateFormat(), $epoch));
241
		self::assertSame('1:39:51 AM', date($account->getTimeFormat(), $epoch));
242
243
		// Test combined formats
244
		self::assertSame('24/4/2021 1:39:51 AM', date($account->getDateTimeFormat(), $epoch));
245
		self::assertSame('24/4/2021<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