|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BasicTests; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use CommonTestClass; |
|
7
|
|
|
use kalanis\kw_locks\Interfaces\IPassedKey; |
|
8
|
|
|
use kalanis\kw_locks\LockException; |
|
9
|
|
|
use kalanis\kw_locks\Methods\StorageLock; |
|
10
|
|
|
use kalanis\kw_storage\Interfaces\IStorage; |
|
11
|
|
|
use kalanis\kw_storage\StorageException; |
|
12
|
|
|
use Traversable; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class StorageTest extends CommonTestClass |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @throws LockException |
|
19
|
|
|
*/ |
|
20
|
|
|
public function testInit(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$lib = $this->getPassLib(); |
|
23
|
|
|
$lib->setKey('current'); |
|
24
|
|
|
$this->assertFalse($lib->has()); // nothing now |
|
25
|
|
|
$this->assertTrue($lib->create()); // new one |
|
26
|
|
|
$this->assertFalse($lib->create()); // already has |
|
27
|
|
|
$this->assertTrue($lib->has()); |
|
28
|
|
|
$this->assertTrue($lib->delete()); // delete current |
|
29
|
|
|
$this->assertFalse($lib->has()); // nothing here |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @throws LockException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function testLockAnother(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$lib = $this->getPassLib(); |
|
38
|
|
|
$lib->setKey('current'); |
|
39
|
|
|
$this->assertFalse($lib->has()); // nothing now |
|
40
|
|
|
$this->assertTrue($lib->create()); // new one |
|
41
|
|
|
$this->assertFalse($lib->create()); // already has |
|
42
|
|
|
$lib->setKey('current', 'other value'); |
|
43
|
|
|
$this->expectException(LockException::class); |
|
44
|
|
|
$lib->has(); // will fail - different value to compare |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @throws LockException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testCreateFail(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$lib = $this->getFailLib(); |
|
53
|
|
|
$lib->setKey('will fail'); |
|
54
|
|
|
$this->expectException(LockException::class); |
|
55
|
|
|
$lib->create(true); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @throws LockException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function testDeleteFail(): void |
|
62
|
|
|
{ |
|
63
|
|
|
$lib = $this->getFailLib(); |
|
64
|
|
|
$lib->setKey('will fail'); |
|
65
|
|
|
$this->expectException(LockException::class); |
|
66
|
|
|
$lib->delete(true); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @throws LockException |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testDestructFail(): void |
|
73
|
|
|
{ |
|
74
|
|
|
$lib = $this->getFailLib(); |
|
75
|
|
|
$lib->setKey('will fail'); |
|
76
|
|
|
$this->assertTrue(true); // just for pass test and do not shout something about risky |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function getPassLib(): IPassedKey |
|
80
|
|
|
{ |
|
81
|
|
|
return new StorageLock(new \XStorage()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function getFailLib(): IPassedKey |
|
85
|
|
|
{ |
|
86
|
|
|
return new StorageLock(new XFallStorage()); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
class XFallStorage implements IStorage |
|
92
|
|
|
{ |
|
93
|
|
|
public function canUse(): bool |
|
94
|
|
|
{ |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function write(string $sharedKey, $data, ?int $timeout = null): bool |
|
99
|
|
|
{ |
|
100
|
|
|
throw new StorageException('mock'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function read(string $sharedKey) |
|
104
|
|
|
{ |
|
105
|
|
|
throw new StorageException('mock'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function remove(string $sharedKey): bool |
|
109
|
|
|
{ |
|
110
|
|
|
throw new StorageException('mock'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function exists(string $sharedKey): bool |
|
114
|
|
|
{ |
|
115
|
|
|
throw new StorageException('mock'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function lookup(string $mask): Traversable |
|
119
|
|
|
{ |
|
120
|
|
|
throw new StorageException('mock'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function increment(string $key): bool |
|
124
|
|
|
{ |
|
125
|
|
|
throw new StorageException('mock'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function decrement(string $key): bool |
|
129
|
|
|
{ |
|
130
|
|
|
throw new StorageException('mock'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function removeMulti(array $keys): array |
|
134
|
|
|
{ |
|
135
|
|
|
throw new StorageException('mock'); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|