DirTest   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 300
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 153
c 2
b 0
f 0
dl 0
loc 300
rs 10
wmc 19

10 Methods

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