RbacManagerTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 55
dl 0
loc 180
rs 10
c 1
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testCheckAccessFailureCases() 0 3 1
A testCheckAddingSameChildRoleDoubleTime() 0 15 1
A testCheckDoubleAssigningPermissionToSameRole() 0 15 1
A testCheckAddingNonUniqueRole() 0 4 1
A successCasesProvider() 0 6 1
A testCheckCyclicException() 0 8 1
A setUp() 0 17 1
A failCasesProvider() 0 7 1
A testCheckAccessSuccessCases() 0 3 1
A testCheckCreatingPermission() 0 7 1
A testCheckAddingNotUniquePermission() 0 4 1
A testCheckCreatingRole() 0 7 1
1
<?php
2
3
namespace Tests\Unit;
4
5
use Doctrine\ORM\ORMException;
6
use Doctrine\ORM\Query\QueryException;
7
use Potievdev\SlimRbac\Exception\CyclicException;
8
use Potievdev\SlimRbac\Exception\DatabaseException;
9
use Potievdev\SlimRbac\Exception\NotUniqueException;
10
use Potievdev\SlimRbac\Models\Entity\Permission;
11
use Potievdev\SlimRbac\Models\Entity\Role;
12
13
/**
14
 * Class RbacManagerTest
15
 * @package Tests\Unit
16
 */
17
class RbacManagerTest extends BaseTestCase
18
{
19
    /**
20
     * @throws CyclicException
21
     * @throws DatabaseException
22
     * @throws NotUniqueException
23
     * @throws QueryException|ORMException
24
     */
25
    public function setUp(): void
26
    {
27
        parent::setUp();
28
29
        $edit = $this->rbacManager->createPermission('edit');
30
        $write = $this->rbacManager->createPermission('write');
31
32
        $moderator = $this->rbacManager->createRole('moderator');
33
        $admin = $this->rbacManager->createRole('admin');
34
35
        $this->rbacManager->attachPermission($moderator, $edit);
36
        $this->rbacManager->attachPermission($admin, $write);
37
38
        $this->rbacManager->attachChildRole($admin, $moderator);
39
40
        $this->rbacManager->assignRoleToUser($moderator, self::MODERATOR_USER_ID);
41
        $this->rbacManager->assignRoleToUser($admin, self::ADMIN_USER_ID);
42
    }
43
44
    public function successCasesProvider(): array
45
    {
46
        return [
47
            'moderator can edit' => [self::MODERATOR_USER_ID, 'edit'],
48
            'admin can edit' => [self::ADMIN_USER_ID, 'edit'],
49
            'admin can write' => [self::ADMIN_USER_ID, 'write'],
50
        ];
51
    }
52
53
    /**
54
     * Testing has permission cases.
55
     * @param integer $userId user id
56
     * @param string $roleOrPermission role or permission name
57
     * @throws QueryException
58
     * @dataProvider successCasesProvider
59
     */
60
    public function testCheckAccessSuccessCases(int $userId, string $roleOrPermission): void
61
    {
62
        $this->assertTrue($this->accessChecker->hasAccess($userId, $roleOrPermission));
63
    }
64
65
    /**
66
     * @return array
67
     */
68
    public function failCasesProvider(): array
69
    {
70
        return [
71
            'moderator has no write permission' => [self::MODERATOR_USER_ID, 'write'],
72
            'not existing permission' => [self::ADMIN_USER_ID, 'none_permission'],
73
            'not existing user id not has permission' => [self::NOT_USER_ID, 'edit'],
74
            'not existing user id not has role' => [self::NOT_USER_ID, 'admin']
75
        ];
76
    }
77
78
    /**
79
     * Testing not have permission cases
80
     * @param integer $userId user id
81
     * @param string $roleOrPermission role or permission name
82
     * @throws QueryException
83
     * @dataProvider failCasesProvider
84
     */
85
    public function testCheckAccessFailureCases(int $userId, string $roleOrPermission): void
86
    {
87
        $this->assertFalse($this->accessChecker->hasAccess($userId, $roleOrPermission));
88
    }
89
90
    /**
91
     * Testing adding not unique permission
92
     *
93
     * @throws DatabaseException
94
     * @throws NotUniqueException|ORMException
95
     */
96
    public function testCheckAddingNotUniquePermission()
97
    {
98
        $this->expectException(NotUniqueException::class);
99
        $this->rbacManager->createPermission('edit');
100
    }
101
102
    /**
103
     * Testing adding not unique role
104
     *
105
     * @throws DatabaseException
106
     * @throws NotUniqueException|ORMException
107
     */
108
    public function testCheckAddingNonUniqueRole()
109
    {
110
        $this->expectException(NotUniqueException::class);
111
         $this->rbacManager->createRole('moderator');
112
    }
113
114
    /**
115
     *
116
     * @throws CyclicException
117
     * @throws DatabaseException
118
     * @throws NotUniqueException
119
     * @throws QueryException|ORMException
120
     */
121
    public function testCheckCyclicException()
122
    {
123
        $this->expectException(CyclicException::class);
124
        $a = $this->rbacManager->createRole('a');
125
        $b = $this->rbacManager->createRole('b');
126
127
        $this->rbacManager->attachChildRole($a, $b);
128
        $this->rbacManager->attachChildRole($b, $a);
129
    }
130
131
    /**
132
     * Testing creating permission
133
     */
134
    public function testCheckCreatingPermission()
135
    {
136
        $permission = $this->repositoryRegistry
137
            ->getPermissionRepository()
138
            ->findOneBy(['name' => 'edit']);
139
140
        $this->assertTrue($permission instanceof Permission);
141
    }
142
143
    /**
144
     * Testing creating role
145
     */
146
    public function testCheckCreatingRole()
147
    {
148
        $role = $this->repositoryRegistry
149
            ->getRoleRepository()
150
            ->findOneBy(['name' => 'admin']);
151
152
        $this->assertTrue($role instanceof Role);
153
    }
154
155
    /**
156
     * @throws DatabaseException|ORMException
157
     */
158
    public function testCheckDoubleAssigningPermissionToSameRole()
159
    {
160
        $this->expectException(NotUniqueException::class);
161
162
        /** @var Role $role */
163
        $role = $this->repositoryRegistry
164
            ->getRoleRepository()
165
            ->findOneBy(['name' => 'admin']);
166
167
        /** @var Permission $permission */
168
        $permission = $this->repositoryRegistry
169
            ->getPermissionRepository()
170
            ->findOneBy(['name' => 'write']);
171
172
        $this->rbacManager->attachPermission($role, $permission);
173
    }
174
175
    /**
176
     * @throws QueryException
177
     * @throws CyclicException
178
     * @throws DatabaseException
179
     * @throws NotUniqueException|ORMException
180
     *
181
     */
182
    public function testCheckAddingSameChildRoleDoubleTime()
183
    {
184
        $this->expectException(NotUniqueException::class);
185
186
        /** @var Role $parent */
187
        $parent = $this->repositoryRegistry
188
            ->getRoleRepository()
189
            ->findOneBy(['name' => 'admin']);
190
191
        /** @var Role $child */
192
        $child = $this->repositoryRegistry
193
            ->getRoleRepository()
194
            ->findOneBy(['name' => 'moderator']);
195
196
        $this->rbacManager->attachChildRole($parent, $child);
197
    }
198
}
199