|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use kalanis\kw_semaphore\SemaphoreException; |
|
4
|
|
|
use kalanis\kw_storage\Interfaces\ITarget; |
|
5
|
|
|
use kalanis\kw_storage\StorageException; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class CommonTestClass |
|
11
|
|
|
* The structure for mocking and configuration seems so complicated, but it's necessary to let it be totally idiot-proof |
|
12
|
|
|
*/ |
|
13
|
|
|
class CommonTestClass extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
const TESTING_CONTENT = 'random content from somewhere'; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class MockStorage implements ITarget |
|
20
|
|
|
{ |
|
21
|
|
|
protected $data = []; |
|
22
|
|
|
|
|
23
|
|
|
public function check(string $key): bool |
|
24
|
|
|
{ |
|
25
|
|
|
return true; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function exists(string $key): bool |
|
29
|
|
|
{ |
|
30
|
|
|
return isset($this->data[$key]); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function load(string $key) |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->exists($key) ? $this->data[$key] : null ; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function save(string $key, $data, ?int $timeout = null): bool |
|
39
|
|
|
{ |
|
40
|
|
|
$this->data[$key] = $data; |
|
41
|
|
|
return true; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function remove(string $key): bool |
|
45
|
|
|
{ |
|
46
|
|
|
if ($this->exists($key)) { |
|
47
|
|
|
unset($this->data[$key]); |
|
48
|
|
|
} |
|
49
|
|
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function lookup(string $key): Traversable |
|
53
|
|
|
{ |
|
54
|
|
|
yield from []; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function increment(string $key): bool |
|
58
|
|
|
{ |
|
59
|
|
|
$this->data[$key] = $this->exists($key) ? $this->data[$key] + 1 : 1 ; |
|
60
|
|
|
return true; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function decrement(string $key): bool |
|
64
|
|
|
{ |
|
65
|
|
|
$this->data[$key] = $this->exists($key) ? $this->data[$key] - 1 : 0 ; |
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function removeMulti(array $keys): array |
|
70
|
|
|
{ |
|
71
|
|
|
$result = []; |
|
72
|
|
|
foreach ($keys as $index => $key) { |
|
73
|
|
|
$result[$index] = $this->remove($key); |
|
74
|
|
|
} |
|
75
|
|
|
return $result; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
class MockFailedStorage implements ITarget |
|
81
|
|
|
{ |
|
82
|
|
|
public function check(string $key): bool |
|
83
|
|
|
{ |
|
84
|
|
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function exists(string $key): bool |
|
88
|
|
|
{ |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function load(string $key) |
|
93
|
|
|
{ |
|
94
|
|
|
return null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function save(string $key, $data, ?int $timeout = null): bool |
|
98
|
|
|
{ |
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function remove(string $key): bool |
|
103
|
|
|
{ |
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function lookup(string $key): Traversable |
|
108
|
|
|
{ |
|
109
|
|
|
yield from []; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function increment(string $key): bool |
|
113
|
|
|
{ |
|
114
|
|
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function decrement(string $key): bool |
|
118
|
|
|
{ |
|
119
|
|
|
return false; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function removeMulti(array $keys): array |
|
123
|
|
|
{ |
|
124
|
|
|
return []; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
class MockKillingStorage implements ITarget |
|
130
|
|
|
{ |
|
131
|
|
|
public function check(string $key): bool |
|
132
|
|
|
{ |
|
133
|
|
|
return false; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param string $key |
|
138
|
|
|
* @throws SemaphoreException |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
public function exists(string $key): bool |
|
142
|
|
|
{ |
|
143
|
|
|
// intentionally Semaphore |
|
144
|
|
|
throw new SemaphoreException('mock fail'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function load(string $key) |
|
148
|
|
|
{ |
|
149
|
|
|
throw new StorageException('mock fail'); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function save(string $key, $data, ?int $timeout = null): bool |
|
153
|
|
|
{ |
|
154
|
|
|
throw new StorageException('mock fail'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function remove(string $key): bool |
|
158
|
|
|
{ |
|
159
|
|
|
throw new StorageException('mock fail'); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function lookup(string $key): Traversable |
|
163
|
|
|
{ |
|
164
|
|
|
throw new StorageException('mock fail'); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function increment(string $key): bool |
|
168
|
|
|
{ |
|
169
|
|
|
throw new StorageException('mock fail'); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function decrement(string $key): bool |
|
173
|
|
|
{ |
|
174
|
|
|
throw new StorageException('mock fail'); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function removeMulti(array $keys): array |
|
178
|
|
|
{ |
|
179
|
|
|
throw new StorageException('mock fail'); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
class MockKillingStorage2 extends MockKillingStorage |
|
185
|
|
|
{ |
|
186
|
|
|
public function exists(string $key): bool |
|
187
|
|
|
{ |
|
188
|
|
|
return true; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
class MockKillingStorage3 extends MockKillingStorage |
|
194
|
|
|
{ |
|
195
|
|
|
public function exists(string $key): bool |
|
196
|
|
|
{ |
|
197
|
|
|
throw new StorageException('mock fail'); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
class MockSemaphore implements \kalanis\kw_semaphore\Interfaces\ISemaphore |
|
203
|
|
|
{ |
|
204
|
|
|
public function want(): bool |
|
205
|
|
|
{ |
|
206
|
|
|
return false; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public function has(): bool |
|
210
|
|
|
{ |
|
211
|
|
|
return false; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function remove(): bool |
|
215
|
|
|
{ |
|
216
|
|
|
return true; |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|