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 Psr\Container\ContainerInterface; |
16
|
|
|
use Spiral\Security\Exception\RuleException; |
17
|
|
|
use Spiral\Security\RuleInterface; |
18
|
|
|
use Spiral\Security\RuleManager; |
19
|
|
|
use Spiral\Security\Rule\CallableRule; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class RuleManagerTest |
23
|
|
|
* |
24
|
|
|
* @package Spiral\Tests\Security |
25
|
|
|
*/ |
26
|
|
|
class RuleManagerTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
public const RULE_NAME = 'test'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ContainerInterface |
32
|
|
|
*/ |
33
|
|
|
private $container; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var RuleInterface |
37
|
|
|
*/ |
38
|
|
|
private $rule; |
39
|
|
|
|
40
|
|
|
public function setUp(): void |
41
|
|
|
{ |
42
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
43
|
|
|
$this->rule = $this->createMock(RuleInterface::class); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testFlow(): void |
47
|
|
|
{ |
48
|
|
|
$ruleClass = get_class($this->rule); |
49
|
|
|
|
50
|
|
|
$this->container->expects($this->once())->method('get') |
|
|
|
|
51
|
|
|
->with($ruleClass)->willReturn($this->rule); |
52
|
|
|
|
53
|
|
|
$manager = new RuleManager($this->container); |
54
|
|
|
|
55
|
|
|
$this->assertEquals($manager, $manager->set(self::RULE_NAME, $ruleClass)); |
56
|
|
|
$this->assertTrue($manager->has(self::RULE_NAME)); |
57
|
|
|
$this->assertEquals($this->rule, $manager->get(self::RULE_NAME)); |
58
|
|
|
$this->assertEquals($manager, $manager->remove(self::RULE_NAME)); |
59
|
|
|
|
60
|
|
|
// other rule types |
61
|
|
|
$manager->set('RuleInterface', $this->rule); |
62
|
|
|
$this->assertEquals($this->rule, $manager->get('RuleInterface')); |
63
|
|
|
$manager->set('Closure', function () { |
64
|
|
|
return true; |
65
|
|
|
}); |
66
|
|
|
$this->assertTrue($manager->get('Closure') instanceof CallableRule); |
67
|
|
|
$manager->set('Array', [$this, 'testFlow']); |
68
|
|
|
$this->assertTrue($manager->get('Array') instanceof CallableRule); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testHasWithNotRegisteredClass(): void |
72
|
|
|
{ |
73
|
|
|
$ruleClass = get_class($this->rule); |
74
|
|
|
$manager = new RuleManager($this->container); |
75
|
|
|
|
76
|
|
|
$this->assertTrue($manager->has($ruleClass)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testSetRuleException(): void |
80
|
|
|
{ |
81
|
|
|
$manager = new RuleManager($this->container); |
82
|
|
|
|
83
|
|
|
$this->expectException(RuleException::class); |
84
|
|
|
$manager->set(self::RULE_NAME); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testRemoveException(): void |
88
|
|
|
{ |
89
|
|
|
$this->container->method('has')->with(self::RULE_NAME)->willReturn(false); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$manager = new RuleManager($this->container); |
92
|
|
|
|
93
|
|
|
$this->expectException(RuleException::class); |
94
|
|
|
$manager->remove(self::RULE_NAME); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testGetWithUndefinedRule(): void |
98
|
|
|
{ |
99
|
|
|
$this->container->method('has')->with(self::RULE_NAME)->willReturn(false); |
100
|
|
|
|
101
|
|
|
$manager = new RuleManager($this->container); |
102
|
|
|
|
103
|
|
|
$this->expectException(RuleException::class); |
104
|
|
|
$manager->get(static::RULE_NAME); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testGetWithSomethingOtherThanRule(): void |
108
|
|
|
{ |
109
|
|
|
$ruleClass = \stdClass::class; |
110
|
|
|
$this->container->method('has')->with(self::RULE_NAME)->willReturn(true); |
111
|
|
|
|
112
|
|
|
$manager = new RuleManager($this->container); |
113
|
|
|
|
114
|
|
|
$this->expectException(RuleException::class); |
115
|
|
|
$manager->get($ruleClass); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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..