Passed
Push — master ( d39fa8...6094eb )
by Petr
10:57
created

XCrashExistenceStorage::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace InfoStorageTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_storage\Interfaces\ITarget;
8
use kalanis\kw_storage\Storage\Key\DefaultKey;
9
use kalanis\kw_storage\Storage;
10
use kalanis\kw_storage\StorageException;
11
use kalanis\UploadPerPartes\Exceptions\UploadException;
12
use kalanis\UploadPerPartes\InfoStorage;
13
use kalanis\UploadPerPartes\Uploader\Translations;
14
15
16
class StorageTest extends CommonTestClass
17
{
18
    /**
19
     * @throws UploadException
20
     */
21
    public function testThru(): void
22
    {
23
        $file = $this->mockTestFile();
24
        $storage = $this->mockStorage();
25
        $storage->save($file, 'abcdefghijklmnopqrstuvwxyz');
26
        $this->assertTrue($storage->exists($file));
27
        $storage->load($file);
28
        $storage->remove($file);
29
        $this->assertFalse($storage->exists($file));
30
    }
31
32
    /**
33
     * @throws UploadException
34
     */
35
    public function testUnreadable(): void
36
    {
37
        $file = $this->mockTestFile();
38
        $storage = $this->mockStorageFail();
39
        $this->expectException(UploadException::class);
40
        $storage->load($file);
41
        $this->expectExceptionMessageMatches('CANNOT READ DRIVEFILE');
42
    }
43
44
    /**
45
     * @throws UploadException
46
     */
47
    public function testUnreadable2(): void
48
    {
49
        $file = $this->mockTestFile();
50
        $storage = $this->mockStorageCrash();
51
        $this->expectException(UploadException::class);
52
        $storage->load($file);
53
        $this->expectExceptionMessageMatches('CANNOT READ DRIVEFILE');
54
    }
55
56
    /**
57
     * @throws UploadException
58
     */
59
    public function testUnwrittable(): void
60
    {
61
        $file = $this->mockTestFile();
62
        $storage = $this->mockStorageFail();
63
        $this->expectException(UploadException::class);
64
        $storage->save($file, 'abcdefghijklmnopqrstuvwxyz');
65
        $this->expectExceptionMessageMatches('CANNOT WRITE DRIVEFILE');
66
    }
67
68
    /**
69
     * @throws UploadException
70
     */
71
    public function testUnwrittable2(): void
72
    {
73
        $file = $this->mockTestFile();
74
        $storage = $this->mockStorageCrash();
75
        $this->expectException(UploadException::class);
76
        $storage->save($file, 'abcdefghijklmnopqrstuvwxyz');
77
        $this->expectExceptionMessageMatches('CANNOT WRITE DRIVEFILE');
78
    }
79
80
    /**
81
     * @throws UploadException
82
     */
83
    public function testDeleted(): void
84
    {
85
        $file = $this->mockTestFile();
86
        $storage = $this->mockStorageFail();
87
        $this->expectException(UploadException::class);
88
        $storage->remove($file); // dies here
89
        $this->expectExceptionMessageMatches('DRIVEFILE CANNOT BE REMOVED');
90
    }
91
92
    /**
93
     * @throws UploadException
94
     */
95
    public function testDeleted2(): void
96
    {
97
        $file = $this->mockTestFile();
98
        $storage = $this->mockStorageCrash();
99
        $this->expectException(UploadException::class);
100
        $storage->remove($file); // dies here
101
        $this->expectExceptionMessageMatches('DRIVEFILE CANNOT BE REMOVED');
102
    }
103
104
    /**
105
     * @throws UploadException
106
     */
107
    public function testExistsDied(): void
108
    {
109
        $file = $this->mockTestFile();
110
        $storage = $this->mockStorageCrashExist();
111
        $this->expectException(UploadException::class);
112
        $storage->exists($file); // dies here
113
        $this->expectExceptionMessageMatches('CANNOT READ DRIVEFILE');
114
    }
115
116
    protected function mockStorage(): InfoStorage\AStorage
117
    {
118
        return new InfoStorage\Storage(new Translations(), new Storage\Storage(new DefaultKey(), new XRemStorage()));
119
    }
120
121
    protected function mockStorageFail(): InfoStorage\AStorage
122
    {
123
        return new InfoStorage\Storage(new Translations(), new Storage\Storage(new DefaultKey(), new XFailStorage()));
124
    }
125
126
    protected function mockStorageCrash(): InfoStorage\AStorage
127
    {
128
        return new InfoStorage\Storage(new Translations(), new Storage\Storage(new DefaultKey(), new XCrashStorage()));
129
    }
130
131
    protected function mockStorageCrashExist(): InfoStorage\AStorage
132
    {
133
        return new InfoStorage\Storage(new Translations(), new Storage\Storage(new DefaultKey(), new XCrashExistenceStorage()));
134
    }
135
}
136
137
138
class XRemStorage implements ITarget
139
{
140
    protected $data = [];
141
142
    public function check(string $key): bool
143
    {
144
        return true;
145
    }
146
147
    public function exists(string $key): bool
148
    {
149
        return isset($this->data[$key]);
150
    }
151
152
    public function load(string $key)
153
    {
154
        return $this->exists($key) ? $this->data[$key] : null ;
155
    }
156
157
    public function save(string $key, $data, ?int $timeout = null): bool
158
    {
159
        $this->data[$key] = $data;
160
        return true;
161
    }
162
163
    public function remove(string $key): bool
164
    {
165
        if ($this->exists($key)) {
166
            unset($this->data[$key]);
167
        }
168
        return true;
169
    }
170
171
    public function lookup(string $key): \Traversable
172
    {
173
        yield from $this->data;
174
    }
175
176
    public function increment(string $key): bool
177
    {
178
        $this->save($key, $this->exists($key) ? $this->load($key) + 1 : 1);
179
        return true;
180
    }
181
182
    public function decrement(string $key): bool
183
    {
184
        $this->save($key, $this->exists($key) ? $this->load($key) - 1 : 0);
185
        return true;
186
    }
187
188
    public function removeMulti(array $keys): array
189
    {
190
        return [];
191
    }
192
}
193
194
195
class XFailStorage implements ITarget
196
{
197
    protected $data = [];
198
199
    public function check(string $key): bool
200
    {
201
        return true;
202
    }
203
204
    public function exists(string $key): bool
205
    {
206
        return true;
207
    }
208
209
    public function load(string $key)
210
    {
211
        throw new StorageException('not load');
212
    }
213
214
    public function save(string $key, $data, ?int $timeout = null): bool
215
    {
216
        return false;
217
    }
218
219
    public function remove(string $key): bool
220
    {
221
        return false;
222
    }
223
224
    public function lookup(string $key): \Traversable
225
    {
226
        yield from [];
227
    }
228
229
    public function increment(string $key): bool
230
    {
231
        return false;
232
    }
233
234
    public function decrement(string $key): bool
235
    {
236
        return false;
237
    }
238
239
    public function removeMulti(array $keys): array
240
    {
241
        return [];
242
    }
243
}
244
245
246
class XCrashStorage implements ITarget
247
{
248
    protected $data = [];
249
250
    public function check(string $key): bool
251
    {
252
        return true;
253
    }
254
255
    public function exists(string $key): bool
256
    {
257
        return true;
258
    }
259
260
    public function load(string $key)
261
    {
262
        throw new StorageException('not load');
263
    }
264
265
    public function save(string $key, $data, ?int $timeout = null): bool
266
    {
267
        throw new StorageException('not save');
268
    }
269
270
    public function remove(string $key): bool
271
    {
272
        throw new StorageException('not del');
273
    }
274
275
    public function lookup(string $key): \Traversable
276
    {
277
        throw new StorageException('not look');
278
    }
279
280
    public function increment(string $key): bool
281
    {
282
        throw new StorageException('not incr');
283
    }
284
285
    public function decrement(string $key): bool
286
    {
287
        throw new StorageException('not decr');
288
    }
289
290
    public function removeMulti(array $keys): array
291
    {
292
        return [];
293
    }
294
}
295
296
297
class XCrashExistenceStorage extends XCrashStorage
298
{
299
    public function exists(string $key): bool
300
    {
301
        throw new StorageException('not available');
302
    }
303
}
304