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\Traits; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Psr\Container\ContainerInterface; |
16
|
|
|
use Spiral\Core\Container; |
17
|
|
|
use Spiral\Core\ContainerScope; |
18
|
|
|
use Spiral\Core\Exception\ScopeException; |
19
|
|
|
use Spiral\Security\GuardInterface; |
20
|
|
|
use Spiral\Tests\Security\Traits\Fixtures\Guarded; |
21
|
|
|
use Spiral\Tests\Security\Traits\Fixtures\GuardedWithNamespace; |
22
|
|
|
use Spiral\Security\Traits\GuardedTrait; |
23
|
|
|
|
24
|
|
|
class GuardedTraitTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
public const OPERATION = 'test'; |
27
|
|
|
public const CONTEXT = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|GuardedTrait |
31
|
|
|
*/ |
32
|
|
|
private $trait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|GuardInterface |
36
|
|
|
*/ |
37
|
|
|
private $guard; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ContainerInterface |
41
|
|
|
*/ |
42
|
|
|
private $container; |
43
|
|
|
|
44
|
|
|
public function setUp(): void |
45
|
|
|
{ |
46
|
|
|
$this->trait = $this->getMockForTrait(GuardedTrait::class); |
|
|
|
|
47
|
|
|
$this->guard = $this->createMock(GuardInterface::class); |
|
|
|
|
48
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testGetGuardFromContainer(): void |
52
|
|
|
{ |
53
|
|
|
$this->container->method('has')->willReturn(true); |
|
|
|
|
54
|
|
|
$this->container->method('get')->will($this->returnValue($this->guard)); |
55
|
|
|
|
56
|
|
|
ContainerScope::runScope($this->container, function (): void { |
57
|
|
|
$this->assertEquals($this->guard, $this->trait->getGuard()); |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testGuardScopeException(): void |
62
|
|
|
{ |
63
|
|
|
$this->expectException(ScopeException::class); |
64
|
|
|
|
65
|
|
|
$this->container->method('has')->willReturn(false); |
66
|
|
|
|
67
|
|
|
ContainerScope::runScope($this->container, function (): void { |
68
|
|
|
$this->assertEquals($this->guard, $this->trait->getGuard()); |
69
|
|
|
}); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGuardScopeException2(): void |
73
|
|
|
{ |
74
|
|
|
$this->expectException(ScopeException::class); |
75
|
|
|
|
76
|
|
|
$this->assertEquals($this->guard, $this->trait->getGuard()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testAllows(): void |
80
|
|
|
{ |
81
|
|
|
$this->guard->method('allows') |
|
|
|
|
82
|
|
|
->with(static::OPERATION, static::CONTEXT) |
83
|
|
|
->will($this->returnValue(true)) |
84
|
|
|
; |
85
|
|
|
|
86
|
|
|
$guarded = new Guarded(); |
87
|
|
|
|
88
|
|
|
$container = new Container(); |
89
|
|
|
$container->bind(GuardInterface::class, $this->guard); |
90
|
|
|
|
91
|
|
|
ContainerScope::runScope($container, function () use ($guarded): void { |
92
|
|
|
$this->assertTrue($guarded->allows(static::OPERATION, static::CONTEXT)); |
93
|
|
|
$this->assertFalse($guarded->denies(static::OPERATION, static::CONTEXT)); |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testResolvePermission(): void |
98
|
|
|
{ |
99
|
|
|
$guarded = new Guarded(); |
100
|
|
|
$this->assertEquals(static::OPERATION, $guarded->resolvePermission(static::OPERATION)); |
101
|
|
|
|
102
|
|
|
$guarded = new GuardedWithNamespace(); |
103
|
|
|
$resolvedPermission = GuardedWithNamespace::GUARD_NAMESPACE . '.' . static::OPERATION; |
104
|
|
|
$this->assertEquals($resolvedPermission, $guarded->resolvePermission(static::OPERATION)); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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..