1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SourcesTests; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use CommonTestClass; |
7
|
|
|
use kalanis\kw_files\Access\CompositeAdapter; |
8
|
|
|
use kalanis\kw_files\Extended\Config; |
9
|
|
|
use kalanis\kw_files\FilesException; |
10
|
|
|
use kalanis\kw_files\Processing\Storage; |
11
|
|
|
use kalanis\kw_images\Sources; |
12
|
|
|
use kalanis\kw_paths\PathsException; |
13
|
|
|
use kalanis\kw_storage\Storage\Key; |
14
|
|
|
use kalanis\kw_storage\Storage\Target; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class OperationsTest extends CommonTestClass |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @throws FilesException |
21
|
|
|
* @throws PathsException |
22
|
|
|
*/ |
23
|
|
|
public function testCopyPass(): void |
24
|
|
|
{ |
25
|
|
|
$lib = $this->getFilesLib(); |
26
|
|
|
$src = ['testimage.png']; |
27
|
|
|
$tgt = ['testtree', 'tstimg.png']; |
28
|
|
|
|
29
|
|
|
$lib->xSet($src, static::TEST_STRING); |
30
|
|
|
// okay |
31
|
|
|
$this->assertTrue($lib->xDataCopy($src, $tgt, false, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx')); |
32
|
|
|
// already exists |
33
|
|
|
$this->expectExceptionMessage('tgtAlEx'); |
34
|
|
|
$this->expectException(FilesException::class); |
35
|
|
|
$lib->xDataCopy($src, $tgt, false, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @throws FilesException |
40
|
|
|
* @throws PathsException |
41
|
|
|
*/ |
42
|
|
|
public function testCopyNotExists(): void |
43
|
|
|
{ |
44
|
|
|
$lib = $this->getFilesLib(); |
45
|
|
|
$src = ['testimate.png']; // not exists |
46
|
|
|
$tgt = ['testtree', 'tstimg.png']; |
47
|
|
|
|
48
|
|
|
$this->expectExceptionMessage('tgtNtEx'); |
49
|
|
|
$this->expectException(FilesException::class); |
50
|
|
|
$lib->xDataCopy($src, $tgt, false, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @throws FilesException |
55
|
|
|
* @throws PathsException |
56
|
|
|
*/ |
57
|
|
|
public function testCopyOverPass(): void |
58
|
|
|
{ |
59
|
|
|
$lib = $this->getFilesLib(); |
60
|
|
|
|
61
|
|
|
$src = ['testimage.png']; |
62
|
|
|
$tgt = ['testtree', 'tstimg.png']; |
63
|
|
|
|
64
|
|
|
$lib->xSet($src, static::TEST_STRING); |
65
|
|
|
|
66
|
|
|
$this->assertTrue($lib->xDataCopy($src, $tgt, false, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx')); |
67
|
|
|
$this->assertTrue($lib->xDataCopy($src, $tgt, true, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx')); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @throws FilesException |
72
|
|
|
* @throws PathsException |
73
|
|
|
*/ |
74
|
|
|
public function testCopyOverCleanupFail(): void |
75
|
|
|
{ |
76
|
|
|
$lib = $this->getFilesFailCleanupLib(); |
77
|
|
|
|
78
|
|
|
$src = ['testimage.png']; |
79
|
|
|
$tgt = ['testtree', 'tstimg.png']; |
80
|
|
|
|
81
|
|
|
$lib->xSet($src, static::TEST_STRING); |
82
|
|
|
|
83
|
|
|
$this->assertTrue($lib->xDataCopy($src, $tgt, true, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx')); |
84
|
|
|
$this->expectExceptionMessage('unEx'); |
85
|
|
|
$this->expectException(FilesException::class); |
86
|
|
|
$lib->xDataCopy($src, ['shall_end'], true, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @throws FilesException |
91
|
|
|
* @throws PathsException |
92
|
|
|
*/ |
93
|
|
|
public function testCopyOverActionFail(): void |
94
|
|
|
{ |
95
|
|
|
$lib = $this->getFilesFailActionLib(); |
96
|
|
|
|
97
|
|
|
$src = ['testimage.png']; |
98
|
|
|
$tgt = ['testtree', 'tstimg.png']; |
99
|
|
|
|
100
|
|
|
$lib->xSet($src, static::TEST_STRING); |
101
|
|
|
|
102
|
|
|
$this->assertTrue($lib->xDataCopy($src, $tgt, true, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx')); |
103
|
|
|
$this->expectExceptionMessage('cpEx'); |
104
|
|
|
$this->expectException(FilesException::class); |
105
|
|
|
$lib->xDataCopy($src, ['shall_end'], true, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @throws FilesException |
110
|
|
|
* @throws PathsException |
111
|
|
|
*/ |
112
|
|
|
public function testMovePass(): void |
113
|
|
|
{ |
114
|
|
|
$lib = $this->getFilesLib(); |
115
|
|
|
|
116
|
|
|
$src = ['testimage.png']; |
117
|
|
|
$tgt1 = ['testtree', 'tstimg1.png']; |
118
|
|
|
$tgt2 = ['testtree', 'tstimg2.png']; |
119
|
|
|
|
120
|
|
|
$lib->xSet($src, static::TEST_STRING); |
121
|
|
|
// now data |
122
|
|
|
$this->assertTrue($lib->xDataCopy($src, $tgt1, false, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx')); |
123
|
|
|
$this->assertTrue($lib->xDataRename($tgt1, $tgt2, false, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx')); |
124
|
|
|
// already exists |
125
|
|
|
$this->assertTrue($lib->xDataCopy($src, $tgt1, false, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx')); |
126
|
|
|
$this->expectExceptionMessage('tgtAlEx'); |
127
|
|
|
$this->expectException(FilesException::class); |
128
|
|
|
$lib->xDataRename($tgt1, $tgt2, false, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @throws FilesException |
133
|
|
|
* @throws PathsException |
134
|
|
|
*/ |
135
|
|
|
public function testMoveNotExists(): void |
136
|
|
|
{ |
137
|
|
|
$lib = $this->getFilesLib(); |
138
|
|
|
|
139
|
|
|
$src = ['testimate.png']; // not exists |
140
|
|
|
$tgt = ['testtree', 'tstimg1.png']; |
141
|
|
|
|
142
|
|
|
$this->expectExceptionMessage('tgtNtEx'); |
143
|
|
|
$this->expectException(FilesException::class); |
144
|
|
|
$lib->xDataRename($src, $tgt, false, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @throws FilesException |
149
|
|
|
* @throws PathsException |
150
|
|
|
*/ |
151
|
|
|
public function testMoveOverPass(): void |
152
|
|
|
{ |
153
|
|
|
$lib = $this->getFilesLib(); |
154
|
|
|
|
155
|
|
|
$src = ['testimage.png']; |
156
|
|
|
$tgt1 = ['testtree', 'tstimg1.png']; |
157
|
|
|
$tgt2 = ['testtree', 'tstimg2.png']; |
158
|
|
|
|
159
|
|
|
$lib->xSet($src, static::TEST_STRING); |
160
|
|
|
|
161
|
|
|
// now data |
162
|
|
|
$this->assertTrue($lib->xDataCopy($src, $tgt1, false, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx')); |
163
|
|
|
$this->assertTrue($lib->xDataRename($tgt1, $tgt2, false, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx')); |
164
|
|
|
// overwrite |
165
|
|
|
$this->assertTrue($lib->xDataCopy($src, $tgt1, false, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx')); |
166
|
|
|
$this->assertTrue($lib->xDataRename($tgt1, $tgt2, true, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx')); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @throws FilesException |
171
|
|
|
* @throws PathsException |
172
|
|
|
*/ |
173
|
|
|
public function testMoveOverCleanupFail(): void |
174
|
|
|
{ |
175
|
|
|
$lib = $this->getFilesFailCleanupLib(); |
176
|
|
|
|
177
|
|
|
$src = ['testimage.png']; |
178
|
|
|
$tgt = ['testtree', 'tstimg.png']; |
179
|
|
|
|
180
|
|
|
$lib->xSet($src, static::TEST_STRING); |
181
|
|
|
|
182
|
|
|
$this->assertTrue($lib->xDataCopy($src, $tgt, true, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx')); |
183
|
|
|
$this->expectExceptionMessage('unEx'); |
184
|
|
|
$this->expectException(FilesException::class); |
185
|
|
|
$lib->xDataRename($src, [], true, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @throws FilesException |
190
|
|
|
* @throws PathsException |
191
|
|
|
*/ |
192
|
|
|
public function testMoveOverActionFail(): void |
193
|
|
|
{ |
194
|
|
|
$lib = $this->getFilesFailActionLib(); |
195
|
|
|
|
196
|
|
|
$src = ['testimage.png']; |
197
|
|
|
$tgt = ['testtree', 'tstimg.png']; |
198
|
|
|
|
199
|
|
|
$lib->xSet($src, static::TEST_STRING); |
200
|
|
|
|
201
|
|
|
$this->assertTrue($lib->xDataCopy($src, $tgt, true, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx')); |
202
|
|
|
$this->expectExceptionMessage('cpEx'); |
203
|
|
|
$this->expectException(FilesException::class); |
204
|
|
|
$lib->xDataRename($src, [], true, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @throws FilesException |
209
|
|
|
* @throws PathsException |
210
|
|
|
*/ |
211
|
|
|
public function testRemove(): void |
212
|
|
|
{ |
213
|
|
|
$lib = $this->getFilesLib(); |
214
|
|
|
|
215
|
|
|
$src = ['testimage.png']; |
216
|
|
|
$tgt = ['testtree', 'tstimg.png']; |
217
|
|
|
|
218
|
|
|
$lib->xSet($src, static::TEST_STRING); |
219
|
|
|
// now data |
220
|
|
|
$this->assertTrue($lib->xDataCopy($src, $tgt, false, 'tgtNtEx', 'tgtAlEx', 'unEx', 'cpEx')); |
221
|
|
|
// already exists |
222
|
|
|
$this->assertTrue($lib->xDataRemove($tgt, 'tgtNtEx')); |
223
|
|
|
// not exists |
224
|
|
|
$this->assertTrue($lib->xDataRemove($tgt, 'tgtNtEx')); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @throws FilesException |
229
|
|
|
* @throws PathsException |
230
|
|
|
*/ |
231
|
|
|
public function testRemoveCleanupFail(): void |
232
|
|
|
{ |
233
|
|
|
$lib = $this->getFilesFailCleanupLib(); |
234
|
|
|
|
235
|
|
|
$this->expectExceptionMessage('unEx'); |
236
|
|
|
$this->expectException(FilesException::class); |
237
|
|
|
$lib->xDataRemove([], 'unEx'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
protected function getFilesLib(array $params = []) |
241
|
|
|
{ |
242
|
|
|
$storage = new \kalanis\kw_storage\Storage\Storage(new Key\DefaultKey(), new Target\Memory()); |
243
|
|
|
return new XSourcesFiles( |
244
|
|
|
new CompositeAdapter( |
245
|
|
|
new Storage\ProcessNode($storage), |
246
|
|
|
new Storage\ProcessDir($storage), |
247
|
|
|
new Storage\ProcessFile($storage), |
248
|
|
|
), |
249
|
|
|
(new Config())->setData($params) |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
protected function getFilesFailCleanupLib(array $params = []) |
254
|
|
|
{ |
255
|
|
|
$storage = new \kalanis\kw_storage\Storage\Storage(new Key\DefaultKey(), new Target\Memory()); |
256
|
|
|
return new XSourcesFiles( |
257
|
|
|
new CompositeAdapter( |
258
|
|
|
new XProcessNodePass($storage), |
259
|
|
|
new Storage\ProcessDir($storage), |
260
|
|
|
new XProcessFileCleanupFail($storage) |
261
|
|
|
), |
262
|
|
|
(new Config())->setData($params) |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
protected function getFilesFailActionLib(array $params = []) |
267
|
|
|
{ |
268
|
|
|
$storage = new \kalanis\kw_storage\Storage\Storage(new Key\DefaultKey(), new Target\Memory()); |
269
|
|
|
return new XSourcesFiles( |
270
|
|
|
new CompositeAdapter( |
271
|
|
|
new XProcessNodePass($storage), |
272
|
|
|
new Storage\ProcessDir($storage), |
273
|
|
|
new XProcessFileActionFail($storage) |
274
|
|
|
), |
275
|
|
|
(new Config())->setData($params) |
276
|
|
|
); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Class XFiles |
283
|
|
|
* Intentionally call protected methods, can test exceptions |
284
|
|
|
* @package SourcesTests |
285
|
|
|
*/ |
286
|
|
|
class XSourcesFiles extends Sources\AFiles |
287
|
|
|
{ |
288
|
|
|
/** |
289
|
|
|
* For store work copy of desired file |
290
|
|
|
* @param string[] $path |
291
|
|
|
* @param mixed $content |
292
|
|
|
* @throws FilesException |
293
|
|
|
* @throws PathsException |
294
|
|
|
* @return bool |
295
|
|
|
*/ |
296
|
|
|
public function xSet(array $path, $content): bool |
297
|
|
|
{ |
298
|
|
|
return $this->lib->saveFile($this->getPath($path), $content); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
public function getPath(array $path): array |
302
|
|
|
{ |
303
|
|
|
return $path; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @param string[] $source |
308
|
|
|
* @param string[] $target |
309
|
|
|
* @param bool $overwrite |
310
|
|
|
* @param string $sourceFileNotExistsErr |
311
|
|
|
* @param string $targetFileExistsErr |
312
|
|
|
* @param string $unlinkErr |
313
|
|
|
* @param string $copyErr |
314
|
|
|
* @throws FilesException |
315
|
|
|
* @throws PathsException |
316
|
|
|
* @return bool |
317
|
|
|
*/ |
318
|
|
|
public function xDataCopy( |
319
|
|
|
array $source, array $target, bool $overwrite, string $sourceFileNotExistsErr, string $targetFileExistsErr, string $unlinkErr, string $copyErr |
320
|
|
|
): bool |
321
|
|
|
{ |
322
|
|
|
return $this->dataCopy($source, $target, $overwrite, $sourceFileNotExistsErr, $targetFileExistsErr, $unlinkErr, $copyErr); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @param string[] $source |
327
|
|
|
* @param string[] $target |
328
|
|
|
* @param bool $overwrite |
329
|
|
|
* @param string $sourceFileNotExistsErr |
330
|
|
|
* @param string $targetFileExistsErr |
331
|
|
|
* @param string $unlinkErr |
332
|
|
|
* @param string $copyErr |
333
|
|
|
* @throws FilesException |
334
|
|
|
* @throws PathsException |
335
|
|
|
* @return bool |
336
|
|
|
*/ |
337
|
|
|
public function xDataRename( |
338
|
|
|
array $source, array $target, bool $overwrite, string $sourceFileNotExistsErr, string $targetFileExistsErr, string $unlinkErr, string $copyErr |
339
|
|
|
): bool |
340
|
|
|
{ |
341
|
|
|
return $this->dataRename($source, $target, $overwrite, $sourceFileNotExistsErr, $targetFileExistsErr, $unlinkErr, $copyErr); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @param string[] $source |
346
|
|
|
* @param string $unlinkErrDesc |
347
|
|
|
* @throws FilesException |
348
|
|
|
* @throws PathsException |
349
|
|
|
* @return bool |
350
|
|
|
*/ |
351
|
|
|
public function xDataRemove(array $source, string $unlinkErrDesc): bool |
352
|
|
|
{ |
353
|
|
|
return $this->dataRemove($source, $unlinkErrDesc); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
|
358
|
|
|
class XProcessNodePass extends Storage\ProcessNode |
359
|
|
|
{ |
360
|
|
|
public function isFile(array $entry): bool |
361
|
|
|
{ |
362
|
|
|
return true; |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
|
367
|
|
|
class XProcessFileActionFail extends Storage\ProcessFile |
368
|
|
|
{ |
369
|
|
|
public function copyFile(array $source, array $dest): bool |
370
|
|
|
{ |
371
|
|
|
$last = end($dest); |
372
|
|
|
$do = empty($last) || ('shall_end' === $last); |
373
|
|
|
return $do ? false : parent::copyFile($source, $dest); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
public function moveFile(array $source, array $dest): bool |
377
|
|
|
{ |
378
|
|
|
$last = end($dest); |
379
|
|
|
$die = empty($last) || ('shall_end' === $last); |
380
|
|
|
return $die ? false : parent::moveFile($source, $dest); |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
|
385
|
|
|
class XProcessFileCleanupFail extends Storage\ProcessFile |
386
|
|
|
{ |
387
|
|
|
public function deleteFile(array $entry): bool |
388
|
|
|
{ |
389
|
|
|
$last = end($entry); |
390
|
|
|
$die = empty($last) || ('shall_end' === $last); |
391
|
|
|
return $die ? false : parent::deleteFile($entry); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|