Passed
Push — master ( 7f699f...e24ab0 )
by Kirill
03:06
created

PermissionManagerTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 64
c 1
b 0
f 0
dl 0
loc 133
rs 10
wmc 11

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testAssociatePermissionException() 0 7 1
A testAddRoleException() 0 7 1
A testRemoveRoleException() 0 6 1
A testRulesForRoleException() 0 7 1
A testRulesForRole() 0 12 1
A testAssociateRoleException() 0 6 1
A testGetRuleRoleException() 0 6 1
A testRoles() 0 13 1
A setUp() 0 3 1
A testGetFallbackRule() 0 12 1
A testAssociation() 0 23 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Security;
13
14
use PHPUnit\Framework\TestCase;
15
use Spiral\Security\Exception\PermissionException;
16
use Spiral\Security\Exception\RoleException;
17
use Spiral\Security\PermissionManager;
18
use Spiral\Security\Rule\AllowRule;
19
use Spiral\Security\Rule\ForbidRule;
20
use Spiral\Security\RuleInterface;
21
use Spiral\Security\RulesInterface;
22
23
/**
24
 * Class PermissionManagerTest
25
 *
26
 * @package Spiral\Tests\Security
27
 */
28
class PermissionManagerTest extends TestCase
29
{
30
    public const ROLE = 'test';
31
    public const PERMISSION = 'permission';
32
33
    /**
34
     * @var RulesInterface
35
     */
36
    private $rules;
37
38
    public function setUp(): void
39
    {
40
        $this->rules = $this->createMock(RulesInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Spiral...\RulesInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Spiral\Security\RulesInterface of property $rules.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
    }
42
43
    public function testRoles(): void
44
    {
45
        $manager = new PermissionManager($this->rules);
46
47
        $this->assertFalse($manager->hasRole(static::ROLE));
48
        $this->assertEquals($manager, $manager->addRole(static::ROLE));
49
        $this->assertTrue($manager->hasRole(static::ROLE));
50
        $this->assertEquals($manager, $manager->removeRole(static::ROLE));
51
        $this->assertFalse($manager->hasRole(static::ROLE));
52
53
        $manager->addRole('one');
54
        $manager->addRole('two');
55
        $this->assertEquals(['one', 'two'], $manager->getRoles());
56
    }
57
58
    public function testAddRoleException(): void
59
    {
60
        $manager = new PermissionManager($this->rules);
61
62
        $this->expectException(RoleException::class);
63
        $manager->addRole(static::ROLE);
64
        $manager->addRole(static::ROLE);
65
    }
66
67
    public function testRemoveRoleException(): void
68
    {
69
        $manager = new PermissionManager($this->rules);
70
71
        $this->expectException(RoleException::class);
72
        $manager->removeRole(static::ROLE);
73
    }
74
75
    public function testAssociation(): void
76
    {
77
        $allowRule = new AllowRule();
78
        $forbidRule = new ForbidRule();
79
80
        $this->rules->method('has')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Spiral\Security\RulesInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        $this->rules->/** @scrutinizer ignore-call */ 
81
                      method('has')->willReturn(true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
        $this->rules->method('get')
82
            ->withConsecutive([AllowRule::class], [AllowRule::class], [ForbidRule::class])
83
            ->willReturn($allowRule, $allowRule, $forbidRule);
84
85
        $manager = new PermissionManager($this->rules);
86
        $manager->addRole(static::ROLE);
87
88
        // test simple permission
89
        $this->assertEquals($manager, $manager->associate(static::ROLE, static::PERMISSION, AllowRule::class));
90
        $this->assertEquals($allowRule, $manager->getRule(static::ROLE, static::PERMISSION));
91
92
        // test pattern permission
93
        $this->assertEquals($manager, $manager->associate(static::ROLE, static::PERMISSION . '.*', AllowRule::class));
94
        $this->assertEquals($allowRule, $manager->getRule(static::ROLE, static::PERMISSION . '.' . static::PERMISSION));
95
96
        $this->assertEquals($manager, $manager->deassociate(static::ROLE, static::PERMISSION));
97
        $this->assertEquals($forbidRule, $manager->getRule(static::ROLE, static::PERMISSION));
98
    }
99
100
    public function testGetRuleRoleException(): void
101
    {
102
        $manager = new PermissionManager($this->rules);
103
104
        $this->expectException(RoleException::class);
105
        $manager->getRule(static::ROLE, static::PERMISSION);
106
    }
107
108
    public function testRulesForRoleException(): void
109
    {
110
        $this->rules->method('has')->willReturn(true);
111
        $manager = new PermissionManager($this->rules);
112
113
        $this->expectException(RoleException::class);
114
        $manager->getPermissions('admin');
115
    }
116
117
    public function testRulesForRole(): void
118
    {
119
        $this->rules->method('has')->willReturn(true);
120
121
        $manager = new PermissionManager($this->rules);
122
123
        $manager->addRole('admin');
124
        $manager->associate('admin', 'post.edit', AllowRule::class);
125
126
        $this->assertSame([
127
            'post.edit' => AllowRule::class
128
        ], $manager->getPermissions('admin'));
129
    }
130
131
    public function testGetFallbackRule(): void
132
    {
133
        $manager = new PermissionManager($this->rules);
134
        $manager->addRole(static::ROLE);
135
136
        $this->rules->method('get')
137
            ->withConsecutive([ForbidRule::class])
138
            ->willReturn(new ForbidRule());
139
140
        $this->assertInstanceOf(
141
            ForbidRule::class,
142
            $manager->getRule(static::ROLE, static::PERMISSION)
143
        );
144
    }
145
146
    public function testAssociateRoleException(): void
147
    {
148
        $manager = new PermissionManager($this->rules);
149
150
        $this->expectException(RoleException::class);
151
        $manager->associate(static::ROLE, static::PERMISSION);
152
    }
153
154
    public function testAssociatePermissionException(): void
155
    {
156
        $this->expectException(PermissionException::class);
157
158
        $manager = new PermissionManager($this->rules);
159
        $manager->addRole(static::ROLE);
160
        $manager->associate(static::ROLE, static::PERMISSION);
161
    }
162
}
163