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\Core\ResolverInterface; |
16
|
|
|
use Spiral\Security\ActorInterface; |
17
|
|
|
use Spiral\Security\Exception\RuleException; |
18
|
|
|
use Spiral\Security\Rule; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class RuleTest |
22
|
|
|
* |
23
|
|
|
* @package Spiral\Tests\Security |
24
|
|
|
*/ |
25
|
|
|
class RuleTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
public const OPERATION = 'test'; |
28
|
|
|
public const CONTEXT = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ActorInterface |
32
|
|
|
*/ |
33
|
|
|
private $actor; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ResolverInterface |
37
|
|
|
*/ |
38
|
|
|
private $resolver; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|Rule |
42
|
|
|
*/ |
43
|
|
|
private $rule; |
44
|
|
|
|
45
|
|
|
protected function setUp(): void |
46
|
|
|
{ |
47
|
|
|
$this->actor = $this->createMock(ActorInterface::class); |
|
|
|
|
48
|
|
|
$this->resolver = $this->createMock(ResolverInterface::class); |
|
|
|
|
49
|
|
|
$this->rule = $this->getMockBuilder(Rule::class) |
|
|
|
|
50
|
|
|
->setConstructorArgs([$this->resolver]) |
51
|
|
|
->setMethods(['check'])->getMock(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $permission |
56
|
|
|
* @param $context |
57
|
|
|
* @param $allowed |
58
|
|
|
* |
59
|
|
|
* @dataProvider allowsProvider |
60
|
|
|
*/ |
61
|
|
|
public function testAllows($permission, $context, $allowed): void |
62
|
|
|
{ |
63
|
|
|
$parameters = [ |
64
|
|
|
'actor' => $this->actor, |
65
|
|
|
'user' => $this->actor, |
66
|
|
|
'permission' => $permission, |
67
|
|
|
'context' => $context, |
68
|
|
|
] + $context; |
69
|
|
|
|
70
|
|
|
$method = new \ReflectionMethod($this->rule, 'check'); |
71
|
|
|
$this->resolver |
72
|
|
|
->expects($this->once()) |
|
|
|
|
73
|
|
|
->method('resolveArguments') |
74
|
|
|
->with($method, $parameters) |
75
|
|
|
->willReturn([$parameters]); |
76
|
|
|
|
77
|
|
|
$this->rule |
78
|
|
|
->expects($this->once()) |
|
|
|
|
79
|
|
|
->method('check') |
80
|
|
|
->with($parameters) |
81
|
|
|
->willReturn($allowed); |
82
|
|
|
|
83
|
|
|
$this->assertEquals($allowed, $this->rule->allows($this->actor, $permission, $context)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testAllowsException(): void |
87
|
|
|
{ |
88
|
|
|
$this->expectException(RuleException::class); |
89
|
|
|
$this->rule->allows($this->actor, static::OPERATION, static::CONTEXT); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function allowsProvider() |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
['test.create', [], false], |
99
|
|
|
['test.create', [], true], |
100
|
|
|
['test.create', ['a' => 'b'], false], |
101
|
|
|
['test.create', ['a' => 'b'], true], |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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..