Passed
Push — master ( 1b6475...610123 )
by Petr
10:18
created

XCrashStorage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

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