1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StorageBasicTests; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
7
|
|
|
use kalanis\kw_files\Interfaces\ITypes; |
8
|
|
|
use kalanis\kw_files\Node; |
9
|
|
|
use kalanis\kw_files\Processing; |
10
|
|
|
use kalanis\kw_paths\PathsException; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class DirTest extends AStorageTest |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @throws FilesException |
17
|
|
|
* @throws PathsException |
18
|
|
|
*/ |
19
|
|
|
public function testCreate(): void |
20
|
|
|
{ |
21
|
|
|
$lib = $this->getDirLib(); |
22
|
|
|
$this->assertTrue($lib->createDir(['another'], false)); |
23
|
|
|
$this->assertTrue($lib->createDir(['sub', 'added'], false)); // not exists in sub dir |
24
|
|
|
$this->assertFalse($lib->createDir(['sub', 'added'], true)); // already exists in sub dir |
25
|
|
|
$this->assertFalse($lib->createDir(['more', 'added'], false)); // not exists both dirs, cannot deep |
26
|
|
|
$this->assertTrue($lib->createDir(['more', 'added'], true)); // not exists both dirs, can deep |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @throws FilesException |
31
|
|
|
* @throws PathsException |
32
|
|
|
*/ |
33
|
|
|
public function testRead0(): void |
34
|
|
|
{ |
35
|
|
|
// fill with own tree, then getting that tree |
36
|
|
|
$storage = $this->getStorageLib(); |
37
|
|
|
$procFile = new Processing\Storage\ProcessFile($storage); |
38
|
|
|
$lib = new Processing\Storage\ProcessDir($storage); |
39
|
|
|
|
40
|
|
|
$lib->createDir([]); |
41
|
|
|
$lib->createDir(['other', 'amogus'], true); |
42
|
|
|
$procFile->saveFile(['other', 'sus'], 'abcdef123456'); |
43
|
|
|
$procFile->saveFile(['red'], '123456789abcdefghi'); |
44
|
|
|
|
45
|
|
|
$subList = $lib->readDir([]); |
46
|
|
|
usort($subList, [$this, 'sortingPaths']); |
47
|
|
|
|
48
|
|
|
$entry = reset($subList); |
49
|
|
|
/** @var Node $entry */ |
50
|
|
|
$this->assertEquals([], $entry->getPath()); |
51
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
52
|
|
|
$this->assertTrue($entry->isDir()); |
53
|
|
|
$this->assertFalse($entry->isFile()); |
54
|
|
|
|
55
|
|
|
$entry = next($subList); |
56
|
|
|
$this->assertEquals(['other'], $entry->getPath()); |
57
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
58
|
|
|
$this->assertTrue($entry->isDir()); |
59
|
|
|
$this->assertFalse($entry->isFile()); |
60
|
|
|
|
61
|
|
|
$entry = next($subList); |
62
|
|
|
$this->assertEquals(['red'], $entry->getPath()); |
63
|
|
|
$this->assertEquals(ITypes::TYPE_FILE, $entry->getType()); |
64
|
|
|
$this->assertFalse($entry->isDir()); |
65
|
|
|
$this->assertTrue($entry->isFile()); |
66
|
|
|
|
67
|
|
|
$this->assertFalse(next($subList)); |
68
|
|
|
|
69
|
|
|
$subList = $lib->readDir(['other'], true); |
70
|
|
|
usort($subList, [$this, 'sortingPaths']); |
71
|
|
|
|
72
|
|
|
$entry = reset($subList); |
73
|
|
|
/** @var Node $entry */ |
74
|
|
|
$this->assertEquals([], $entry->getPath()); |
75
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
76
|
|
|
|
77
|
|
|
$entry = next($subList); |
78
|
|
|
$this->assertEquals(['amogus'], $entry->getPath()); |
79
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
80
|
|
|
|
81
|
|
|
$entry = next($subList); |
82
|
|
|
$this->assertEquals(['sus'], $entry->getPath()); |
83
|
|
|
$this->assertEquals(ITypes::TYPE_FILE, $entry->getType()); |
84
|
|
|
|
85
|
|
|
$this->assertFalse(next($subList)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @throws FilesException |
90
|
|
|
* @throws PathsException |
91
|
|
|
*/ |
92
|
|
|
public function testRead1(): void |
93
|
|
|
{ |
94
|
|
|
$lib = $this->getDirLib(); |
95
|
|
|
$subList = $lib->readDir([], false, true); |
96
|
|
|
usort($subList, [$this, 'sortingPaths']); |
97
|
|
|
|
98
|
|
|
$entry = reset($subList); |
99
|
|
|
/** @var Node $entry */ |
100
|
|
|
$this->assertEquals([], $entry->getPath()); |
101
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
102
|
|
|
$this->assertTrue($entry->isDir()); |
103
|
|
|
$this->assertFalse($entry->isFile()); |
104
|
|
|
|
105
|
|
|
$entry = next($subList); |
106
|
|
|
$this->assertEquals(['dummy1.txt'], $entry->getPath()); |
107
|
|
|
$this->assertEquals(36, $entry->getSize()); |
108
|
|
|
$this->assertEquals(ITypes::TYPE_FILE, $entry->getType()); |
109
|
|
|
$this->assertFalse($entry->isDir()); |
110
|
|
|
$this->assertTrue($entry->isFile()); |
111
|
|
|
|
112
|
|
|
$entry = next($subList); |
113
|
|
|
$this->assertEquals(['dummy2.txt'], $entry->getPath()); |
114
|
|
|
$this->assertEquals(36, $entry->getSize()); |
115
|
|
|
$this->assertEquals(ITypes::TYPE_FILE, $entry->getType()); |
116
|
|
|
|
117
|
|
|
$entry = next($subList); |
118
|
|
|
$this->assertEquals(['last_one'], $entry->getPath()); |
119
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
120
|
|
|
|
121
|
|
|
$entry = next($subList); |
122
|
|
|
$this->assertEquals(['next_one'], $entry->getPath()); |
123
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
124
|
|
|
|
125
|
|
|
$entry = next($subList); |
126
|
|
|
$this->assertEquals(['other1.txt'], $entry->getPath()); |
127
|
|
|
$this->assertEquals(36, $entry->getSize()); |
128
|
|
|
$this->assertEquals(ITypes::TYPE_FILE, $entry->getType()); |
129
|
|
|
|
130
|
|
|
$entry = next($subList); |
131
|
|
|
$this->assertEquals(['other2.txt'], $entry->getPath()); |
132
|
|
|
$this->assertEquals(36, $entry->getSize()); |
133
|
|
|
$this->assertEquals(ITypes::TYPE_FILE, $entry->getType()); |
134
|
|
|
|
135
|
|
|
$entry = next($subList); |
136
|
|
|
$this->assertEquals(['sub'], $entry->getPath()); |
137
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
138
|
|
|
|
139
|
|
|
$this->assertFalse(next($subList)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @throws FilesException |
144
|
|
|
* @throws PathsException |
145
|
|
|
*/ |
146
|
|
|
public function testRead2(): void |
147
|
|
|
{ |
148
|
|
|
$lib = $this->getDirLib(); |
149
|
|
|
$subList = $lib->readDir(['next_one'], true); |
150
|
|
|
$entry = reset($subList); |
151
|
|
|
/** @var Node $entry */ |
152
|
|
|
$this->assertEquals([], $entry->getPath()); |
153
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
154
|
|
|
|
155
|
|
|
$entry = next($subList); |
156
|
|
|
$this->assertEquals(['sub_one'], $entry->getPath()); |
157
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
158
|
|
|
|
159
|
|
|
$entry = next($subList); |
160
|
|
|
$this->assertEquals(['sub_one', '.gitkeep'], $entry->getPath()); |
161
|
|
|
$this->assertEquals(0, $entry->getSize()); |
162
|
|
|
$this->assertEquals(ITypes::TYPE_FILE, $entry->getType()); |
163
|
|
|
|
164
|
|
|
$this->assertFalse(next($subList)); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @throws FilesException |
169
|
|
|
* @throws PathsException |
170
|
|
|
*/ |
171
|
|
|
public function testRead3(): void |
172
|
|
|
{ |
173
|
|
|
$lib = $this->getDirLib(); |
174
|
|
|
$subList = $lib->readDir(['last_one'], false); |
175
|
|
|
$entry = reset($subList); |
176
|
|
|
/** @var Node $entry */ |
177
|
|
|
$this->assertEquals([], $entry->getPath()); |
178
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
179
|
|
|
|
180
|
|
|
$entry = next($subList); |
181
|
|
|
$this->assertEquals(['.gitkeep'], $entry->getPath()); |
182
|
|
|
$this->assertEquals(0, $entry->getSize()); |
183
|
|
|
$this->assertEquals(ITypes::TYPE_FILE, $entry->getType()); |
184
|
|
|
|
185
|
|
|
$this->assertFalse(next($subList)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @throws FilesException |
190
|
|
|
* @throws PathsException |
191
|
|
|
*/ |
192
|
|
|
public function testRead4(): void |
193
|
|
|
{ |
194
|
|
|
$lib = $this->getDirTreeLib(); |
195
|
|
|
$subList = $lib->readDir([], false); |
196
|
|
|
$entry = reset($subList); |
197
|
|
|
/** @var Node $entry */ |
198
|
|
|
$this->assertEquals([], $entry->getPath()); |
199
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
200
|
|
|
|
201
|
|
|
$entry = next($subList); |
202
|
|
|
$this->assertEquals(['data'], $entry->getPath()); |
203
|
|
|
$this->assertEquals(0, $entry->getSize()); |
204
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
205
|
|
|
|
206
|
|
|
$this->assertFalse(next($subList)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @throws FilesException |
211
|
|
|
* @throws PathsException |
212
|
|
|
*/ |
213
|
|
|
public function testReadFail(): void |
214
|
|
|
{ |
215
|
|
|
$lib = $this->getDirLib(); |
216
|
|
|
$this->expectException(FilesException::class); |
217
|
|
|
$lib->readDir(['dummy2.txt']); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @throws FilesException |
222
|
|
|
* @throws PathsException |
223
|
|
|
*/ |
224
|
|
|
public function testCopyMoveDelete(): void |
225
|
|
|
{ |
226
|
|
|
$lib = $this->getDirLib(); |
227
|
|
|
$this->assertTrue($lib->copyDir(['next_one'], ['more'])); |
228
|
|
|
$this->assertTrue($lib->moveDir(['more'], ['another'])); |
229
|
|
|
$this->assertTrue($lib->deleteDir(['another'], true)); |
230
|
|
|
$this->assertFalse($lib->deleteDir(['another'])); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @throws FilesException |
235
|
|
|
* @throws PathsException |
236
|
|
|
*/ |
237
|
|
|
public function testCopyFail(): void |
238
|
|
|
{ |
239
|
|
|
$lib = $this->getDirLib(); |
240
|
|
|
$this->assertFalse($lib->copyDir(['next_one'], ['other2.txt'])); // dest exists |
241
|
|
|
$this->assertFalse($lib->copyDir(['more'], ['another'])); // source is not exists |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @throws FilesException |
246
|
|
|
* @throws PathsException |
247
|
|
|
*/ |
248
|
|
|
public function testMoveFail(): void |
249
|
|
|
{ |
250
|
|
|
$lib = $this->getDirLib(); |
251
|
|
|
$this->assertFalse($lib->moveDir(['next_one'], ['other2.txt'])); // dest exists |
252
|
|
|
$this->assertFalse($lib->moveDir(['more'], ['another'])); // source not exists |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @throws FilesException |
257
|
|
|
* @throws PathsException |
258
|
|
|
*/ |
259
|
|
|
public function testDeleteFail(): void |
260
|
|
|
{ |
261
|
|
|
$lib = $this->getDirLib(); |
262
|
|
|
$this->assertFalse($lib->deleteDir(['other2.txt'])); |
263
|
|
|
$this->assertFalse($lib->deleteDir(['more'])); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @throws FilesException |
268
|
|
|
* @throws PathsException |
269
|
|
|
*/ |
270
|
|
|
public function testDeepDeleteFail(): void |
271
|
|
|
{ |
272
|
|
|
$lib = $this->getDirLib(); |
273
|
|
|
$this->assertTrue($lib->createDir(['some', 'more'], true)); |
274
|
|
|
$this->assertFalse($lib->deleteDir(['some'])); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|