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

Failed Conditions
Pull Request — master (#1017)
by Dan
04:28
created

AbstractSmrAccountIntegrationTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
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
}
234