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