Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
69 / 69
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2
3// ╔════════════════════════════════════════════════════════════╗
4// ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║
5// ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com>     ║
6// ╚════════════════════════════════════════════════════════════╝
7
8declare(strict_types=1);
9
10use AppDemo\Shared\Infrastructure\RbacPermissionService;
11use AppDemo\User\Domain\UserGroup;
12use Phexium\Plugin\Authorization\Adapter\RbacAuthorizationService;
13
14beforeEach(function (): void {
15    $permissionsConfig = [
16        'admin' => [
17            'can.sing',
18            'can.dance',
19            'can.fly',
20        ],
21        'user' => [
22            'can.sing',
23        ],
24    ];
25
26    $authorizationService = new RbacAuthorizationService($permissionsConfig);
27    $this->rbacService = new RbacPermissionService($authorizationService);
28});
29
30test('User group has permission when permission is in config', function (): void {
31    $can = $this->rbacService->can(UserGroup::User, 'can.sing');
32
33    expect($can)->toBeTrue();
34});
35
36test('Permission check is case sensitive', function (): void {
37    $can = $this->rbacService->can(UserGroup::User, 'CAN.SING');
38
39    expect($can)->toBeFalse();
40});
41
42test('User group does not have permission when permission is not in config', function (): void {
43    $can = $this->rbacService->can(UserGroup::User, 'can.dance');
44
45    expect($can)->toBeFalse();
46});
47
48test('Returns false when checking empty permission string', function (): void {
49    $can = $this->rbacService->can(UserGroup::User, '');
50
51    expect($can)->toBeFalse();
52});
53
54test('CanAny returns true when user has at least one permission', function (): void {
55    $canAny = $this->rbacService->canAny(UserGroup::User, ['can.dance', 'can.sing', 'can.fly']);
56
57    expect($canAny)->toBeTrue();
58});
59
60test('CanAny returns false when user has none of the permissions', function (): void {
61    $canAny = $this->rbacService->canAny(UserGroup::User, ['can.dance', 'can.fly']);
62
63    expect($canAny)->toBeFalse();
64});
65
66test('CanAny returns false with empty permissions array', function (): void {
67    $canAny = $this->rbacService->canAny(UserGroup::Admin, []);
68
69    expect($canAny)->toBeFalse();
70});
71
72test('CanAll returns true when user has all permissions', function (): void {
73    $canAll = $this->rbacService->canAll(UserGroup::Admin, ['can.sing', 'can.dance']);
74
75    expect($canAll)->toBeTrue();
76});
77
78test('CanAll returns false when user is missing at least one permission', function (): void {
79    $canAll = $this->rbacService->canAll(UserGroup::User, ['can.sing', 'can.dance']);
80
81    expect($canAll)->toBeFalse();
82});
83
84test('CanAll returns true with empty permissions array', function (): void {
85    $canAll = $this->rbacService->canAll(UserGroup::User, []);
86
87    expect($canAll)->toBeTrue();
88});
89
90test('GetPermissions returns all permissions for a user group', function (): void {
91    $permissions = $this->rbacService->getPermissions(UserGroup::Admin);
92
93    expect($permissions)->toHaveCount(3)
94        ->and($permissions)->toContain('can.sing')
95        ->and($permissions)->toContain('can.dance')
96        ->and($permissions)->toContain('can.fly')
97    ;
98});
99
100test('GetPermissions returns empty array for user group with no permissions', function (): void {
101    $permissionsConfig = ['guest' => []];
102    $authorizationService = new RbacAuthorizationService($permissionsConfig);
103    $rbacService = new RbacPermissionService($authorizationService);
104
105    $permissions = $rbacService->getPermissions(UserGroup::User);
106
107    expect($permissions)->toBeEmpty();
108});