|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Authorization; |
|
6
|
|
|
|
|
7
|
|
|
use Opulence\Cache\ICacheBridge; |
|
8
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
|
|
11
|
|
|
class CacheManagerTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var CacheManager - System Under Test */ |
|
14
|
|
|
protected CacheManager $sut; |
|
15
|
|
|
|
|
16
|
|
|
/** @var ICacheBridge|MockObject */ |
|
17
|
|
|
protected $cacheBridgeMock; |
|
18
|
|
|
|
|
19
|
|
|
public function setUp(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$this->cacheBridgeMock = $this->createMock(ICacheBridge::class); |
|
22
|
|
|
|
|
23
|
|
|
$this->sut = new CacheManager($this->cacheBridgeMock); |
|
24
|
|
|
|
|
25
|
|
|
parent::setUp(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testStoreAll(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$data = ['foo' => 'bar']; |
|
31
|
|
|
$payload = '{"foo":"bar"}'; |
|
32
|
|
|
|
|
33
|
|
|
$this->cacheBridgeMock |
|
34
|
|
|
->expects($this->once()) |
|
35
|
|
|
->method('set') |
|
36
|
|
|
->with('casbin_auth_collection', $payload, PHP_INT_MAX); |
|
37
|
|
|
|
|
38
|
|
|
$this->sut->storeAll($data); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testGetAllReturnsNullIfCacheBridgeThrowsException(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$this->cacheBridgeMock |
|
44
|
|
|
->expects($this->once()) |
|
45
|
|
|
->method('get') |
|
46
|
|
|
->willThrowException(new \Exception()); |
|
47
|
|
|
|
|
48
|
|
|
$this->sut->getAll(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getAllProvider(): array |
|
55
|
|
|
{ |
|
56
|
|
|
return [ |
|
57
|
|
|
['', null], |
|
58
|
|
|
[123, null], |
|
59
|
|
|
['[]', []], |
|
60
|
|
|
['{}', []], |
|
61
|
|
|
['{"foo":"bar"}', ['foo' => 'bar']], |
|
62
|
|
|
['0', [0]], |
|
63
|
|
|
['"0', []], |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @dataProvider getAllProvider |
|
69
|
|
|
* |
|
70
|
|
|
* @param mixed $payload |
|
71
|
|
|
* @param mixed $expectedResult |
|
72
|
|
|
*/ |
|
73
|
|
|
public function testGetAll($payload, $expectedResult): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->cacheBridgeMock |
|
76
|
|
|
->expects($this->any()) |
|
77
|
|
|
->method('get') |
|
78
|
|
|
->willReturn($payload); |
|
79
|
|
|
|
|
80
|
|
|
$actualResult = $this->sut->getAll(); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertSame($expectedResult, $actualResult); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function testClearAllCallsCacheBridge(): void |
|
86
|
|
|
{ |
|
87
|
|
|
$this->cacheBridgeMock |
|
88
|
|
|
->expects($this->once()) |
|
89
|
|
|
->method('delete'); |
|
90
|
|
|
|
|
91
|
|
|
$this->sut->clearAll(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|