Skip to content

Commit 775aef7

Browse files
committed
Add coverage
1 parent 9a7f463 commit 775aef7

File tree

6 files changed

+135
-14
lines changed

6 files changed

+135
-14
lines changed

src/Repositories/UserRepository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ public function getUserEntityByIdentifier(string $identifier): ?UserEntity
7676

7777
$row = current($rows);
7878

79+
// @codeCoverageIgnoreStart
7980
if (!is_array($row)) {
8081
return null;
8182
}
83+
// @codeCoverageIgnoreEnd
8284

8385
$userEntity = $this->userEntityFactory->fromState($row);
8486

tests/unit/src/Repositories/ClientRepositoryTest.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -277,24 +277,25 @@ public function testFindPaginationWithEmptyList()
277277
*/
278278
public function testUpdate(): void
279279
{
280-
$client = self::getClient('clientid');
280+
$client = self::getClient(id: 'clientId', entityId: 'entityId');
281281
$this->repository->add($client);
282282

283283
$client = new ClientEntity(
284-
'clientid',
285-
'newclientsecret',
286-
'Client',
287-
'Description',
288-
['http://localhost/redirect'],
289-
['openid'],
290-
true,
291-
false,
292-
'admin',
284+
identifier: 'clientId',
285+
secret: 'newclientsecret',
286+
name: 'Client',
287+
description: 'Description',
288+
redirectUri: ['http://localhost/redirect'],
289+
scopes: ['openid'],
290+
isEnabled: true,
291+
isConfidential: false,
292+
authSource: 'admin',
293+
entityIdentifier: 'newEntityId',
293294
);
294295

295296
$this->repository->update($client);
296297
$this->clientEntityFactoryMock->expects($this->once())->method('fromState')->willReturn($client);
297-
$foundClient = $this->repository->findById('clientid');
298+
$foundClient = $this->repository->findById('clientId');
298299

299300
$this->assertEquals($client, $foundClient);
300301
}
@@ -305,13 +306,13 @@ public function testUpdate(): void
305306
*/
306307
public function testDelete(): void
307308
{
308-
$client = self::getClient('clientid');
309+
$client = self::getClient(id: 'clientId', entityId: 'entityId');
309310
$this->repository->add($client);
310311

311312
$this->clientEntityFactoryMock->expects($this->once())->method('fromState')->willReturn($client);
312-
$client = $this->repository->findById('clientid');
313+
$client = $this->repository->findById('clientId');
313314
$this->repository->delete($client);
314-
$foundClient = $this->repository->findById('clientid');
315+
$foundClient = $this->repository->findById('clientId');
315316

316317
$this->assertNull($foundClient);
317318
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\Module\oidc\unit\Repositories;
6+
7+
use League\OAuth2\Server\CodeChallengeVerifiers\CodeChallengeVerifierInterface;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\TestCase;
10+
use SimpleSAML\Module\oidc\Repositories\CodeChallengeVerifiersRepository;
11+
12+
#[CoversClass(CodeChallengeVerifiersRepository::class)]
13+
class CodeChallengeVerifiersRepositoryTest extends TestCase
14+
{
15+
protected function sut(): CodeChallengeVerifiersRepository
16+
{
17+
return new CodeChallengeVerifiersRepository();
18+
}
19+
20+
public function testCanCreateInstance(): void
21+
{
22+
$this->assertInstanceOf(CodeChallengeVerifiersRepository::class, $this->sut());
23+
}
24+
25+
public function testCanGetCodeChallengeVerifier(): void
26+
{
27+
$this->assertInstanceOf(
28+
CodeChallengeVerifierInterface::class,
29+
$this->sut()->get('S256'),
30+
);
31+
$this->assertTrue($this->sut()->has('S256'));
32+
33+
$this->assertInstanceOf(
34+
CodeChallengeVerifierInterface::class,
35+
$this->sut()->get('plain'),
36+
);
37+
$this->assertTrue($this->sut()->has('plain'));
38+
39+
$this->assertNotEmpty($this->sut()->getAll());
40+
}
41+
42+
public function testReturnsNullForUnsuportedVerifier(): void
43+
{
44+
$this->assertNull($this->sut()->get('unsuported'));
45+
}
46+
}

tests/unit/src/Repositories/RefreshTokenRepositoryTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
use DateTimeImmutable;
1919
use DateTimeZone;
20+
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
21+
use League\OAuth2\Server\Exception\OAuthServerException;
2022
use PHPUnit\Framework\MockObject\MockObject;
2123
use PHPUnit\Framework\TestCase;
2224
use RuntimeException;
@@ -40,6 +42,7 @@ class RefreshTokenRepositoryTest extends TestCase
4042
final public const USER_ID = 'refresh_token_user_id';
4143
final public const ACCESS_TOKEN_ID = 'refresh_token_access_token_id';
4244
final public const REFRESH_TOKEN_ID = 'refresh_token_id';
45+
final public const AUTH_CODE_ID = 'auth_code_id';
4346

4447
protected RefreshTokenRepository $repository;
4548
protected MockObject $accessTokenMock;
@@ -184,4 +187,45 @@ public function testRemoveExpired(): void
184187

185188
$this->assertNull($notFoundRefreshToken);
186189
}
190+
191+
public function testGetNewRefreshTokenThrows(): void
192+
{
193+
$this->expectException(RuntimeException::class);
194+
$this->expectExceptionMessage('Not implemented');
195+
196+
$this->repository->getNewRefreshToken();
197+
}
198+
199+
public function testPersistNewRefreshTokenThrowsIfNotRefreshTokenEntity(): void
200+
{
201+
$this->expectException(OAuthServerException::class);
202+
203+
$oAuthRefreshTokenEntity = $this->createMock(RefreshTokenEntityInterface::class);
204+
205+
$this->repository->persistNewRefreshToken($oAuthRefreshTokenEntity);
206+
}
207+
208+
public function testCanRevokeByAuthCodeId(): void
209+
{
210+
$refreshToken = new RefreshTokenEntity(
211+
self::REFRESH_TOKEN_ID,
212+
new DateTimeImmutable('tomorrow', new DateTimeZone('UTC')),
213+
$this->accessTokenMock,
214+
self::AUTH_CODE_ID,
215+
);
216+
217+
$this->repository->persistNewRefreshToken($refreshToken);
218+
219+
$this->refreshTokenEntityFactoryMock->expects($this->once())
220+
->method('fromState')
221+
->with($this->callback(function (array $state): bool {
222+
return $state['id'] === self::REFRESH_TOKEN_ID;
223+
}))->willReturn($this->refreshTokenEntityMock);
224+
225+
$this->accessTokenRepositoryMock->method('findById')->willReturn($this->accessTokenMock);
226+
227+
$this->refreshTokenEntityMock->expects($this->once())->method('revoke');
228+
229+
$this->repository->revokeByAuthCodeId(self::AUTH_CODE_ID);
230+
}
187231
}

tests/unit/src/Repositories/ScopeRepositoryTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
namespace SimpleSAML\Test\Module\oidc\unit\Repositories;
1717

18+
use League\OAuth2\Server\Entities\ClientEntityInterface;
1819
use PHPUnit\Framework\TestCase;
1920
use SimpleSAML\Configuration;
2021
use SimpleSAML\Module\oidc\Entities\ScopeEntity;
@@ -90,4 +91,17 @@ public function testFinalizeScopes(): void
9091
];
9192
$this->assertEquals($expectedScopes, $finalizedScopes);
9293
}
94+
95+
public function testFinalizeScopesReturnsEmptyIfNotClientEntity(): void
96+
{
97+
$scopeRepository = new ScopeRepository(new ModuleConfig(), new ScopeEntityFactory());
98+
$scopes = [
99+
new ScopeEntity('openid'),
100+
new ScopeEntity('basic'),
101+
];
102+
103+
$clientMock = $this->createMock(ClientEntityInterface::class);
104+
105+
$this->assertEmpty($scopeRepository->finalizeScopes($scopes, 'any', $clientMock));
106+
}
93107
}

tests/unit/src/Repositories/UserRepositoryTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use PHPUnit\Framework\TestCase;
2323
use SimpleSAML\Configuration;
2424
use SimpleSAML\Database;
25+
use SimpleSAML\Module\oidc\Entities\Interfaces\ClientEntityInterface;
2526
use SimpleSAML\Module\oidc\Entities\UserEntity;
2627
use SimpleSAML\Module\oidc\Factories\Entities\UserEntityFactory;
2728
use SimpleSAML\Module\oidc\Helpers;
@@ -307,4 +308,17 @@ public function testWillDeleteFromDatabaseAndCache(): void
307308
protocolCache: $this->protocolCacheMock,
308309
)->delete($this->userEntityMock);
309310
}
311+
312+
public function testGetUserEntityByUserCredentialsThrows(): void
313+
{
314+
$this->expectException(\Exception::class);
315+
$this->expectExceptionMessage('Not supported');
316+
317+
$this->mock()->getUserEntityByUserCredentials(
318+
'username',
319+
'password',
320+
'grantType',
321+
$this->createMock(ClientEntityInterface::class),
322+
);
323+
}
310324
}

0 commit comments

Comments
 (0)