Skip to content

Commit b7c5542

Browse files
committed
WIP coverage
1 parent bbbfa6f commit b7c5542

File tree

3 files changed

+225
-0
lines changed

3 files changed

+225
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\Module\oidc\unit\Admin;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\MockObject\MockObject;
9+
use PHPUnit\Framework\TestCase;
10+
use SimpleSAML\Error\Exception;
11+
use SimpleSAML\Module\oidc\Admin\Authorization;
12+
use SimpleSAML\Module\oidc\Bridges\SspBridge;
13+
use SimpleSAML\Module\oidc\Bridges\SspBridge\Utils;
14+
use SimpleSAML\Module\oidc\Exceptions\AuthorizationException;
15+
use SimpleSAML\Module\oidc\Services\AuthContextService;
16+
use SimpleSAML\Utils\Auth;
17+
18+
#[CoversClass(Authorization::class)]
19+
class AuthorizationTest extends TestCase
20+
{
21+
protected MockObject $sspBridgeMock;
22+
protected MockObject $sspBridgeUtilsMock;
23+
protected MockObject $sspBridgeUtilsAuthMock;
24+
protected MockObject $authContextServiceMock;
25+
26+
protected function setUp(): void
27+
{
28+
$this->sspBridgeMock = $this->createMock(SspBridge::class);
29+
$this->sspBridgeUtilsMock = $this->createMock(Utils::class);
30+
$this->sspBridgeMock->method('utils')->willReturn($this->sspBridgeUtilsMock);
31+
$this->sspBridgeUtilsAuthMock = $this->createMock(Auth::class);
32+
$this->sspBridgeUtilsMock->method('auth')->willReturn($this->sspBridgeUtilsAuthMock);
33+
34+
$this->authContextServiceMock = $this->createMock(AuthContextService::class);
35+
}
36+
37+
protected function sut(
38+
?SspBridge $sspBridge = null,
39+
?AuthContextService $authContextService = null,
40+
): Authorization {
41+
$sspBridge ??= $this->sspBridgeMock;
42+
$authContextService ??= $this->authContextServiceMock;
43+
44+
return new Authorization($sspBridge, $authContextService);
45+
}
46+
47+
public function testCanCreateInstance(): void
48+
{
49+
$this->assertInstanceOf(Authorization::class, $this->sut());
50+
}
51+
52+
public function testCanCheckIsAdmin(): void
53+
{
54+
$this->assertFalse($this->sut()->isAdmin());
55+
$this->sspBridgeUtilsAuthMock->method('isAdmin')->willReturn(true);
56+
$this->assertTrue($this->sut()->isAdmin());
57+
}
58+
59+
public function testCanRequireAdmin(): void
60+
{
61+
$this->expectException(AuthorizationException::class);
62+
$this->expectExceptionMessage('admin');
63+
64+
$this->sspBridgeUtilsAuthMock->method('isAdmin')->willReturn(false);
65+
66+
$this->sut()->requireAdmin();
67+
}
68+
69+
public function testCanForceRequireAdmin(): void
70+
{
71+
$this->sspBridgeUtilsAuthMock->expects($this->once())->method('requireAdmin');
72+
$this->sspBridgeUtilsAuthMock->expects($this->once())->method('isAdmin')->willReturn(true);
73+
74+
$this->sut()->requireAdmin(true);
75+
}
76+
77+
public function testThrowsOnForceRequireAdminError(): void
78+
{
79+
$this->sspBridgeUtilsAuthMock->expects($this->once())->method('requireAdmin')
80+
->willThrowException(new Exception('error'));
81+
82+
$this->expectException(AuthorizationException::class);
83+
$this->expectExceptionMessage('admin');
84+
85+
$this->sut()->requireAdmin(true);
86+
}
87+
88+
public function testRequireAdminOrUserWithPermissionReturnsIfAdmin(): void
89+
{
90+
$this->sspBridgeUtilsAuthMock->expects($this->once())->method('isAdmin')->willReturn(true);
91+
$this->authContextServiceMock->expects($this->never())->method('requirePermission');
92+
93+
$this->sut()->requireAdminOrUserWithPermission('permission');
94+
}
95+
96+
public function testRequireAdminOrUserWithPermissionReturnsIfUser(): void
97+
{
98+
$this->sspBridgeUtilsAuthMock->expects($this->once())->method('isAdmin')->willReturn(false);
99+
$this->authContextServiceMock->expects($this->once())->method('requirePermission');
100+
101+
$this->sut()->requireAdminOrUserWithPermission('permission');
102+
}
103+
104+
public function testRequireUserWithPermissionThrowsIfUserNotAuthorized(): void
105+
{
106+
$this->expectException(AuthorizationException::class);
107+
$this->expectExceptionMessage('not authorized');
108+
109+
$this->sspBridgeUtilsAuthMock->expects($this->once())->method('isAdmin')->willReturn(false);
110+
$this->authContextServiceMock->expects($this->once())->method('requirePermission')
111+
->willThrowException(new Exception('error'));
112+
113+
$this->sut()->requireAdminOrUserWithPermission('permission');
114+
}
115+
116+
public function testCanGetUserId(): void
117+
{
118+
$this->authContextServiceMock->expects($this->once())->method('getAuthUserId')->willReturn('id');
119+
120+
$this->assertSame('id', $this->sut()->getUserId());
121+
}
122+
}
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\Admin\Menu;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\Module\oidc\Admin\Menu\Item;
10+
11+
#[CoversClass(Item::class)]
12+
class ItemTest extends TestCase
13+
{
14+
protected string $hrefPath;
15+
protected string $label;
16+
protected string $iconAssetPath;
17+
18+
protected function setUp(): void
19+
{
20+
$this->hrefPath = 'path';
21+
$this->label = 'label';
22+
$this->iconAssetPath = 'icon-path';
23+
}
24+
25+
protected function sut(
26+
?string $hrefPath = null,
27+
?string $label = null,
28+
?string $iconAssetPath = null,
29+
): Item {
30+
$hrefPath ??= $this->hrefPath;
31+
$label ??= $this->label;
32+
$iconAssetPath ??= $this->iconAssetPath;
33+
34+
return new Item($hrefPath, $label, $iconAssetPath);
35+
}
36+
37+
public function testCanCreateInstance(): void
38+
{
39+
$sut = $this->sut();
40+
$this->assertInstanceOf(Item::class, $sut);
41+
42+
$this->assertSame($sut->getHrefPath(), $this->hrefPath);
43+
$this->assertSame($sut->getLabel(), $this->label);
44+
$this->assertSame($sut->getIconAssetPath(), $this->iconAssetPath);
45+
}
46+
}

tests/unit/src/Admin/MenuTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\Module\oidc\unit\Admin;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\UsesClass;
9+
use PHPUnit\Framework\MockObject\MockObject;
10+
use PHPUnit\Framework\TestCase;
11+
use SimpleSAML\Module\oidc\Admin\Menu;
12+
use SimpleSAML\Module\oidc\Admin\Menu\Item;
13+
14+
#[CoversClass(Menu::class)]
15+
#[UsesClass(Item::class)]
16+
class MenuTest extends TestCase
17+
{
18+
protected MockObject $itemMock;
19+
20+
protected function setUp(): void
21+
{
22+
$this->itemMock = $this->createMock(Item::class);
23+
}
24+
25+
protected function sut(
26+
?Item ...$items,
27+
): Menu {
28+
return new Menu(...$items);
29+
}
30+
31+
public function testCanCreateInstance(): void
32+
{
33+
$this->assertInstanceOf(Menu::class, $this->sut());
34+
$this->assertInstanceOf(Menu::class, $this->sut($this->itemMock));
35+
}
36+
37+
public function testCanAddGetItem(): void
38+
{
39+
$sut = $this->sut();
40+
$this->assertEmpty($sut->getItems());
41+
$sut->addItem($this->itemMock);
42+
$this->assertCount(1, $sut->getItems());
43+
}
44+
45+
public function testCanSetGetActiveHrefPath(): void
46+
{
47+
$sut = $this->sut();
48+
$this->assertNull($sut->getActiveHrefPath());
49+
$sut->setActiveHrefPath('oidc');
50+
$this->assertSame('oidc', $sut->getActiveHrefPath());
51+
}
52+
53+
public function testCanBuildItem(): void
54+
{
55+
$this->assertInstanceOf(Item::class, $this->sut()->buildItem('oidc', 'OIDC'));
56+
}
57+
}

0 commit comments

Comments
 (0)