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); |
|
|
|
|
44
|
|
|
$this->actor = $this->createMock(ActorInterface::class); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testAllows(): void |
48
|
|
|
{ |
49
|
|
|
$this->permission->method('hasRole') |
|
|
|
|
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
|
|
|
|
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..