|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\Acl; |
|
6
|
|
|
|
|
7
|
|
|
use Ecodev\Felix\Acl\MultipleRoles; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
class MultipleRolesTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
public function testEverything(): void |
|
13
|
|
|
{ |
|
14
|
|
|
$roles = new MultipleRoles(); |
|
15
|
|
|
self::assertSame([], $roles->getRoles(), 'must be empty'); |
|
16
|
|
|
self::assertSame('[]', (string) $roles); |
|
17
|
|
|
|
|
18
|
|
|
$roles->addRole('writer'); |
|
19
|
|
|
self::assertSame(['writer'], $roles->getRoles(), 'must be added'); |
|
20
|
|
|
self::assertTrue($roles->has('writer'), 'must be existing'); |
|
21
|
|
|
self::assertFalse($roles->has('foo'), 'must not be existing'); |
|
22
|
|
|
self::assertTrue($roles->has('foo', 'writer'), 'at least one of them'); |
|
23
|
|
|
self::assertFalse($roles->has('foo', 'bar'), 'none of of them'); |
|
24
|
|
|
self::assertFalse($roles->has()); |
|
25
|
|
|
self::assertSame('[writer]', (string) $roles); |
|
26
|
|
|
|
|
27
|
|
|
$roles->addRole('writer'); |
|
28
|
|
|
self::assertSame('[writer]', (string) $roles, 'do not duplicate roles'); |
|
29
|
|
|
|
|
30
|
|
|
$roles2 = new MultipleRoles(['anonymous', 'admin']); |
|
31
|
|
|
self::assertSame(['admin', 'anonymous'], $roles2->getRoles(), 'must be created with roles'); |
|
32
|
|
|
self::assertSame('[admin, anonymous]', (string) $roles2); |
|
33
|
|
|
|
|
34
|
|
|
foreach ($roles2->getRoles() as $role) { |
|
35
|
|
|
$roles->addRole($role); |
|
36
|
|
|
} |
|
37
|
|
|
self::assertSame(['admin', 'anonymous', 'writer'], $roles->getRoles(), 'must be merged'); |
|
38
|
|
|
self::assertSame('[admin, anonymous, writer]', (string) $roles); |
|
39
|
|
|
|
|
40
|
|
|
$roles2->addRole('contact'); |
|
41
|
|
|
$roles2->addRole('reader'); |
|
42
|
|
|
self::assertSame(['admin', 'anonymous', 'contact', 'reader'], $roles2->getRoles(), 'array can be added'); |
|
43
|
|
|
self::assertSame('[admin, anonymous, contact, reader]', (string) $roles2); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testGetRoleId(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$role = new MultipleRoles(); |
|
49
|
|
|
|
|
50
|
|
|
$this->expectExceptionMessage('This should never be called. If it is, then it means this class is not used correctly'); |
|
51
|
|
|
$role->getRoleId(); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|