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

GuardTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testAllowsNoActor() 0 6 1
A testAllowsPermissionsHasNoRole() 0 6 1
A testWithActor() 0 7 1
A setUp() 0 4 1
A testAllows() 0 16 1
A testWithRoles() 0 7 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\ActorInterface;
16
use Spiral\Security\Exception\GuardException;
17
use Spiral\Security\Guard;
18
use Spiral\Security\PermissionsInterface;
19
use Spiral\Security\RuleInterface;
20
21
class GuardTest extends TestCase
22
{
23
    public const OPERATION = 'test';
24
    public const CONTEXT = [];
25
26
    /**
27
     * @var \PHPUnit_Framework_MockObject_MockObject|PermissionsInterface
28
     */
29
    private $permission;
30
31
    /**
32
     * @var \PHPUnit_Framework_MockObject_MockObject|ActorInterface
33
     */
34
    private $actor;
35
36
    /**
37
     * @var array
38
     */
39
    private $roles = ['user', 'admin'];
40
41
    public function setUp(): void
42
    {
43
        $this->permission = $this->createMock(PermissionsInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Spiral...ssionsInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type PHPUnit_Framework_MockOb...ty\PermissionsInterface of property $permission.

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...
44
        $this->actor = $this->createMock(ActorInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Spiral...\ActorInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type PHPUnit_Framework_MockOb...Security\ActorInterface of property $actor.

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...
45
    }
46
47
    public function testAllows(): void
48
    {
49
        $this->permission->method('hasRole')
0 ignored issues
show
Bug introduced by
The method method() does not exist on Spiral\Security\PermissionsInterface. ( Ignorable by Annotation )

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

49
        $this->permission->/** @scrutinizer ignore-call */ 
50
                           method('hasRole')

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...
50
            ->withConsecutive(['user'], ['admin'])
51
            ->willReturnOnConsecutiveCalls(false, true);
52
53
        $rule = $this->createMock(RuleInterface::class);
54
        $rule->expects($this->once())
55
            ->method('allows')
56
            ->with($this->actor, static::OPERATION, [])->willReturn(true);
57
58
        $this->permission->method('getRule')
59
            ->willReturn($rule);
60
61
        $guard = new Guard($this->permission, $this->actor, $this->roles);
62
        $this->assertTrue($guard->allows(static::OPERATION, static::CONTEXT));
63
    }
64
65
    public function testAllowsPermissionsHasNoRole(): void
66
    {
67
        $this->permission->method('hasRole')->with($this->anything())->willReturn(false);
68
69
        $guard = new Guard($this->permission, $this->actor, $this->roles);
70
        $this->assertFalse($guard->allows(static::OPERATION, static::CONTEXT));
71
    }
72
73
    public function testAllowsNoActor(): void
74
    {
75
        $guard = new Guard($this->permission, null, $this->roles);
76
77
        $this->expectException(GuardException::class);
78
        $guard->allows(static::OPERATION, static::CONTEXT);
79
    }
80
81
    public function testWithActor(): void
82
    {
83
        $guard = new Guard($this->permission);
84
        $guardWithActor = $guard->withActor($this->actor);
85
86
        $this->assertEquals($this->actor, $guardWithActor->getActor());
87
        $this->assertNotEquals($guard, $guardWithActor);
88
    }
89
90
    public function testWithRoles(): void
91
    {
92
        $guard = new Guard($this->permission, $this->actor);
93
        $guardWithRoles = $guard->withRoles($this->roles);
94
95
        $this->assertEquals($this->roles, $guardWithRoles->getRoles());
96
        $this->assertNotEquals($guard, $guardWithRoles);
97
    }
98
}
99