Skip to content

Commit 64142b9

Browse files
Have test classes use concrete interface to make it clearer to phpstan what we're doing
1 parent 26aabd0 commit 64142b9

11 files changed

Lines changed: 101 additions & 81 deletions

phpstan-baseline-dev.neon

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
11
parameters:
22
ignoreErrors:
33
-
4-
message: '#^Parameter \#1 \$array of function asort expects array, string given\.$#'
5-
identifier: argument.type
6-
count: 4
7-
path: tests/src/Auth/Source/PasswordVerifyTest.php
8-
9-
-
10-
message: '#^Property SimpleSAML\\Test\\Module\\sqlauth\\Auth\\Source\\PasswordVerifyTest\:\:\$config \(array\<string, string\|null\>\) does not accept array\<string, list\<string\>\|string\|null\>\.$#'
11-
identifier: assign.propertyType
12-
count: 4
13-
path: tests/src/Auth/Source/PasswordVerifyTest.php
14-
15-
-
16-
message: '#^Parameter \#1 \$array of function asort expects array, mixed given\.$#'
17-
identifier: argument.type
18-
count: 4
19-
path: tests/src/Auth/Source/SQLTest.php
20-
21-
-
22-
message: '#^Property SimpleSAML\\Test\\Module\\sqlauth\\Auth\\Source\\SQLTest\:\:\$config \(array\<string, string\|null\>\) does not accept array\<string, list\<string\>\|string\|null\>\.$#'
23-
identifier: assign.propertyType
24-
count: 4
25-
path: tests/src/Auth/Source/SQLTest.php
26-
27-
-
28-
message: "#Property SimpleSAML\\\\Test\\\\Module\\\\sqlauth\\\\Auth\\\\Source\\\\SQL2SimpleTest\\:\\:\\$config type has no value type specified in iterable type array\\.#"
4+
message: '#^Property SimpleSAML\\Test\\Module\\sqlauth\\Auth\\Source\\SQL2MultipleAuthTest\:\:\$config type has no value type specified in iterable type array\.$#'
5+
identifier: missingType.iterableValue
296
count: 1
30-
path: tests/src/Auth/Source/SQL2SimpleTest.php
7+
path: tests/src/Auth/Source/SQL2MultipleAuthTest.php
318

329
-
33-
message: "#Property SimpleSAML\\\\Test\\\\Module\\\\sqlauth\\\\Auth\\\\Source\\\\SQL2MultipleAuthTest\\:\\:\\$config type has no value type specified in iterable type array\\.#"
10+
message: '#^Property SimpleSAML\\Test\\Module\\sqlauth\\Auth\\Source\\SQL2SimpleTest\:\:\$config type has no value type specified in iterable type array\.$#'
11+
identifier: missingType.iterableValue
3412
count: 1
35-
path: tests/src/Auth/Source/SQL2MultipleAuthTest.php
13+
path: tests/src/Auth/Source/SQL2SimpleTest.php

tests/src/Auth/Source/PasswordVerify1CompatTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,12 @@
77
#CoversClass(SimpleSAML\Module\sqlauth\Auth\Source\PasswordVerify1Compat::class)
88
class PasswordVerify1CompatTest extends PasswordVerifyTest
99
{
10-
protected string $wrapperClassName = '\SimpleSAML\Test\Module\sqlauth\Auth\Source\PasswordVerify1CompatWrapper';
10+
/**
11+
* @param array<mixed> $info
12+
* @param array<mixed> $config
13+
*/
14+
protected function createWrapper(array $info, array $config): WrapperInterface
15+
{
16+
return new PasswordVerify1CompatWrapper($info, $config);
17+
}
1118
}

tests/src/Auth/Source/PasswordVerify1CompatWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* method in SQL.php
1414
*/
1515

16-
class PasswordVerify1CompatWrapper extends PasswordVerify1Compat
16+
class PasswordVerify1CompatWrapper extends PasswordVerify1Compat implements WrapperInterface
1717
{
1818
/**
1919
* @param array<mixed> $info

tests/src/Auth/Source/PasswordVerifyTest.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
#CoversClass(\SimpleSAML\Module\sqlauth\Auth\Source\PasswordVerify::class)
1111
class PasswordVerifyTest extends TestCase
1212
{
13-
// Subclasses can override this to test other wrapper classes
14-
protected string $wrapperClassName = '\SimpleSAML\Test\Module\sqlauth\Auth\Source\PasswordVerifyWrapper';
15-
1613
/** @var array<string, string> */
1714
private array $info = ['AuthId' => 'testAuthId'];
1815

@@ -80,11 +77,21 @@ public static function setUpBeforeClass(): void
8077
}
8178

8279

80+
/**
81+
* @param array<mixed> $info
82+
* @param array<mixed> $config
83+
*/
84+
protected function createWrapper(array $info, array $config): WrapperInterface
85+
{
86+
return new PasswordVerifyWrapper($info, $config);
87+
}
88+
89+
8390
public function testBasicSingleSuccess(): void
8491
{
8592
// Correct username/password
8693
$this->config['query'] = "select givenName, email, passwordhash from users where uid=:username";
87-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('bob', 'password1');
94+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('bob', 'password1');
8895
asort($ret);
8996
$this->assertCount(2, $ret);
9097
$this->assertEquals($ret, [
@@ -99,7 +106,7 @@ public function testBasicSingleFailedLogin(): void
99106
$this->expectException(\SimpleSAML\Error\Error::class);
100107
// Wrong username/password
101108
$this->config['query'] = "select givenName, email, passwordhash from users where uid=:username";
102-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('alice', 'wrong');
109+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('alice', 'wrong');
103110
$this->assertCount(0, $ret);
104111
}
105112

@@ -109,7 +116,7 @@ public function testBasicSingleFailedLoginNonExisting(): void
109116
$this->expectException(\SimpleSAML\Error\Error::class);
110117
// Wrong username/password
111118
$this->config['query'] = "select givenName, email, passwordhash from users where uid=:username";
112-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('henry', 'boo');
119+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('henry', 'boo');
113120
$this->assertCount(0, $ret);
114121
}
115122

@@ -119,7 +126,7 @@ public function testBasicSingleFailedLoginNonExistingNoPassword(): void
119126
$this->expectException(\SimpleSAML\Error\Error::class);
120127
// Wrong username/password
121128
$this->config['query'] = "select givenName, email, passwordhash from users where uid=:username";
122-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('alice2', '');
129+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('alice2', '');
123130
$this->assertCount(0, $ret);
124131
}
125132

@@ -130,10 +137,10 @@ public function testJoinSingleSuccess(): void
130137
$this->config['query'] = "
131138
select u.givenName, u.email, ug.groupname, passwordhash
132139
from users u left join usergroups ug on (u.uid=ug.uid)
133-
where u.uid=:username ";
134-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('bob', 'password1');
140+
where u.uid=:username
141+
order by ug.groupname";
142+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('bob', 'password1');
135143
asort($ret);
136-
asort($ret['groupname']);
137144
$this->assertCount(3, $ret);
138145
$this->assertEquals($ret, [
139146
'email' => ['bob@example.com'],
@@ -151,7 +158,7 @@ public function testJoinSingleFailedLogin(): void
151158
select u.givenName, u.email, ug.groupname, passwordhash
152159
from users u left join usergroups ug on (u.uid=ug.uid)
153160
where u.uid=:username";
154-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('alice', 'wrong');
161+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('alice', 'wrong');
155162
$this->assertCount(0, $ret);
156163
}
157164

@@ -161,11 +168,10 @@ public function testMultiQuerySuccess(): void
161168
// Correct username/password
162169
$this->config['query'] = [
163170
"select givenName, email, passwordhash from users where uid=:username",
164-
"select groupname from usergroups where uid=:username",
171+
"select groupname from usergroups where uid=:username order by groupname",
165172
];
166-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('bob', 'password1');
173+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('bob', 'password1');
167174
asort($ret);
168-
asort($ret['groupname']);
169175
$this->assertCount(3, $ret);
170176
$this->assertEquals($ret, [
171177
'email' => ['bob@example.com'],
@@ -183,7 +189,7 @@ public function testMultiQueryFailedLogin(): void
183189
"select givenName, email, passwordhash from users where uid=:username",
184190
"select groupname from usergroups where uid=:username",
185191
];
186-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('alice', 'wrong');
192+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('alice', 'wrong');
187193
$this->assertCount(0, $ret);
188194
}
189195

@@ -194,11 +200,10 @@ public function testMultiQuerySubsequentNoRowsSuccess(): void
194200
$this->config['query'] = [
195201
"select givenName, email, passwordhash from users where uid=:username",
196202
"select groupname from usergroups where uid=:username and groupname like '%nomatch%'",
197-
"select groupname from usergroups where uid=:username and groupname like 'stud%'",
203+
"select groupname from usergroups where uid=:username and groupname like 'stud%' order by groupname",
198204
];
199-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('bob', 'password1');
205+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('bob', 'password1');
200206
asort($ret);
201-
asort($ret['groupname']);
202207
$this->assertCount(3, $ret);
203208
$this->assertEquals($ret, [
204209
'email' => ['bob@example.com'],
@@ -213,12 +218,11 @@ public function testMultiQuerySubsequentAppendSuccess(): void
213218
// Correct username/password. Second query returns a row, third query appends one row
214219
$this->config['query'] = [
215220
"select givenName, email, passwordhash from users where uid=:username",
216-
"select groupname from usergroups where uid=:username and groupname like 'stud%'",
217-
"select groupname from usergroups where uid=:username and groupname like '%sers'",
221+
"select groupname from usergroups where uid=:username and groupname like 'stud%' order by groupname",
222+
"select groupname from usergroups where uid=:username and groupname like '%sers' order by groupname",
218223
];
219-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('bob', 'password1');
224+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('bob', 'password1');
220225
asort($ret);
221-
asort($ret['groupname']);
222226
$this->assertCount(3, $ret);
223227
$this->assertEquals($ret, [
224228
'email' => ['bob@example.com'],

tests/src/Auth/Source/PasswordVerifyWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* method in PasswordVerify.php
1414
*/
1515

16-
class PasswordVerifyWrapper extends PasswordVerify
16+
class PasswordVerifyWrapper extends PasswordVerify implements WrapperInterface
1717
{
1818
/**
1919
* @param array<mixed> $info

tests/src/Auth/Source/SQL1CompatTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,12 @@
77
#CoversClass(SimpleSAML\Module\sqlauth\Auth\Source\SQL1Compat::class)
88
class SQL1CompatTest extends SQLTest
99
{
10-
protected string $wrapperClassName = '\SimpleSAML\Test\Module\sqlauth\Auth\Source\SQL1CompatWrapper';
10+
/**
11+
* @param array<mixed> $info
12+
* @param array<mixed> $config
13+
*/
14+
protected function createWrapper(array $info, array $config): WrapperInterface
15+
{
16+
return new SQL1CompatWrapper($info, $config);
17+
}
1118
}

tests/src/Auth/Source/SQL1CompatWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* method in SQL.php
1414
*/
1515

16-
class SQL1CompatWrapper extends SQL1Compat
16+
class SQL1CompatWrapper extends SQL1Compat implements WrapperInterface
1717
{
1818
/**
1919
* @param array<mixed> $info

tests/src/Auth/Source/SQL2Wrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* method in SQL.php
1414
*/
1515

16-
class SQL2Wrapper extends SQL2
16+
class SQL2Wrapper extends SQL2 implements WrapperInterface
1717
{
1818
/**
1919
* @param array<mixed> $info

tests/src/Auth/Source/SQLTest.php

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
#CoversClass(SimpleSAML\Module\sqlauth\Auth\Source\SQL::class)
1414
class SQLTest extends TestCase
1515
{
16-
// Subclasses can override this to test other wrapper classes
17-
protected string $wrapperClassName = '\SimpleSAML\Test\Module\sqlauth\Auth\Source\SQLWrapper';
18-
1916
/** @var array<string, string> */
2017
private array $info = ['AuthId' => 'testAuthId'];
2118

@@ -80,11 +77,21 @@ public static function setUpBeforeClass(): void
8077
}
8178

8279

80+
/**
81+
* @param array<mixed> $info
82+
* @param array<mixed> $config
83+
*/
84+
protected function createWrapper(array $info, array $config): WrapperInterface
85+
{
86+
return new SQLWrapper($info, $config);
87+
}
88+
89+
8390
public function testBasicSingleSuccess(): void
8491
{
8592
// Correct username/password
8693
$this->config['query'] = "select givenName, email from users where uid=:username and password=:password";
87-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('bob', 'password');
94+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('bob', 'password');
8895
asort($ret);
8996
$this->assertCount(2, $ret);
9097
$this->assertEquals($ret, [
@@ -99,7 +106,7 @@ public function testBasicSingleUsernameRegexSuccess(): void
99106
// Correct username/password
100107
$this->config['query'] = "select givenName, email from users where uid=:username and password=:password";
101108
$this->config['username_regex'] = '/^[a-z]+$/'; // Username must be a single lower case word
102-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('bob', 'password');
109+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('bob', 'password');
103110
asort($ret);
104111
$this->assertCount(2, $ret);
105112
$this->assertEquals($ret, [
@@ -115,7 +122,7 @@ public function testBasicSingleUsernameRegexFailedLogin(): void
115122
// Correct username/password, but doesn't match the username regex
116123
$this->config['query'] = "select givenName, email from users where uid=:username and password=:password";
117124
$this->config['username_regex'] = '/^\d+$/'; // Username must be a non-negative integer
118-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('bob', 'password');
125+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('bob', 'password');
119126
asort($ret);
120127
$this->assertCount(0, $ret);
121128
}
@@ -127,7 +134,7 @@ public function testBasicSingleUsernameRegexFailedLoginNonExistingUser(): void
127134
// Correct username/password, but doesn't match the username regex
128135
$this->config['query'] = "select givenName, email from users where uid=:username and password=:password";
129136
$this->config['username_regex'] = '/^\d+$/'; // Username must be a non-negative integer
130-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('henry', 'password');
137+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('henry', 'password');
131138
asort($ret);
132139
$this->assertCount(0, $ret);
133140
}
@@ -138,7 +145,7 @@ public function testBasicSingleFailedLogin(): void
138145
$this->expectException(\SimpleSAML\Error\Error::class);
139146
// Wrong username/password
140147
$this->config['query'] = "select givenName, email from users where uid=:username and password=:password";
141-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('alice', 'wrong');
148+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('alice', 'wrong');
142149
$this->assertCount(0, $ret);
143150
}
144151

@@ -149,10 +156,10 @@ public function testJoinSingleSuccess(): void
149156
$this->config['query'] = "
150157
select u.givenName, u.email, ug.groupname
151158
from users u left join usergroups ug on (u.uid=ug.uid)
152-
where u.uid=:username and u.password=:password";
153-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('bob', 'password');
159+
where u.uid=:username and u.password=:password
160+
order by ug.groupname";
161+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('bob', 'password');
154162
asort($ret);
155-
asort($ret['groupname']);
156163
$this->assertCount(3, $ret);
157164
$this->assertEquals($ret, [
158165
'email' => ['bob@example.com'],
@@ -170,7 +177,7 @@ public function testJoinSingleFailedLogin(): void
170177
select u.givenName, u.email, ug.groupname
171178
from users u left join usergroups ug on (u.uid=ug.uid)
172179
where u.uid=:username and u.password=:password";
173-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('alice', 'wrong');
180+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('alice', 'wrong');
174181
$this->assertCount(0, $ret);
175182
}
176183

@@ -180,11 +187,10 @@ public function testMultiQuerySuccess(): void
180187
// Correct username/password
181188
$this->config['query'] = [
182189
"select givenName, email from users where uid=:username and password=:password",
183-
"select groupname from usergroups where uid=:username",
190+
"select groupname from usergroups where uid=:username order by groupname",
184191
];
185-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('bob', 'password');
192+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('bob', 'password');
186193
asort($ret);
187-
asort($ret['groupname']);
188194
$this->assertCount(3, $ret);
189195
$this->assertEquals($ret, [
190196
'email' => ['bob@example.com'],
@@ -202,7 +208,7 @@ public function testMultiQueryFailedLogin(): void
202208
"select givenName, email from users where uid=:username and password=:password",
203209
"select groupname from usergroups where uid=:username",
204210
];
205-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('alice', 'wrong');
211+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('alice', 'wrong');
206212
$this->assertCount(0, $ret);
207213
}
208214

@@ -212,12 +218,11 @@ public function testMultiQuerySubsequentNoRowsSuccess(): void
212218
// Correct username/password. Second query returns no rows, third query returns just one row
213219
$this->config['query'] = [
214220
"select givenName, email from users where uid=:username and password=:password",
215-
"select groupname from usergroups where uid=:username and groupname like '%nomatch%'",
216-
"select groupname from usergroups where uid=:username and groupname like 'stud%'",
221+
"select groupname from usergroups where uid=:username and groupname like '%nomatch%' order by groupname",
222+
"select groupname from usergroups where uid=:username and groupname like 'stud%' order by groupname",
217223
];
218-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('bob', 'password');
224+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('bob', 'password');
219225
asort($ret);
220-
asort($ret['groupname']);
221226
$this->assertCount(3, $ret);
222227
$this->assertEquals($ret, [
223228
'email' => ['bob@example.com'],
@@ -232,12 +237,11 @@ public function testMultiQuerySubsequentAppendSuccess(): void
232237
// Correct username/password. Second query returns a row, third query appends one row
233238
$this->config['query'] = [
234239
"select givenName, email from users where uid=:username and password=:password",
235-
"select groupname from usergroups where uid=:username and groupname like 'stud%'",
236-
"select groupname from usergroups where uid=:username and groupname like '%sers'",
240+
"select groupname from usergroups where uid=:username and groupname like 'stud%' order by groupname",
241+
"select groupname from usergroups where uid=:username and groupname like '%sers' order by groupname",
237242
];
238-
$ret = (new $this->wrapperClassName($this->info, $this->config))->callLogin('bob', 'password');
243+
$ret = $this->createWrapper($this->info, $this->config)->callLogin('bob', 'password');
239244
asort($ret);
240-
asort($ret['groupname']);
241245
$this->assertCount(3, $ret);
242246
$this->assertEquals($ret, [
243247
'email' => ['bob@example.com'],

tests/src/Auth/Source/SQLWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* method in SQL.php
1414
*/
1515

16-
class SQLWrapper extends SQL
16+
class SQLWrapper extends SQL implements WrapperInterface
1717
{
1818
/**
1919
* @param array<mixed> $info

0 commit comments

Comments
 (0)