Passed
Push — master ( 95ec1e...702df7 )
by Petr
02:32
created

DirFlatTest::testRead2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 19
rs 9.8333
c 0
b 0
f 0
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 DirFlatTest 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->getDirFlatLib();
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->getStorageFlatLib();
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
        clearstatcache();
90
91
        $subList = $lib->readDir(['tmp']);
92
        usort($subList, [$this, 'sortingPaths']);
93
94
        $entry = reset($subList);
95
        /** @var Node $entry */
96
        $this->assertEquals([], $entry->getPath());
97
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
98
        $this->assertTrue($entry->isDir());
99
        $this->assertFalse($entry->isFile());
100
101
        $entry = next($subList);
102
        $this->assertEquals(['other'], $entry->getPath());
103
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
104
        $this->assertTrue($entry->isDir());
105
        $this->assertFalse($entry->isFile());
106
107
        $entry = next($subList);
108
        $this->assertEquals(['red'], $entry->getPath());
109
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
110
        $this->assertFalse($entry->isDir());
111
        $this->assertTrue($entry->isFile());
112
113
        $this->assertFalse(next($subList));
114
115
        $subList = $lib->readDir(['tmp', 'other'], true);
116
        usort($subList, [$this, 'sortingPaths']);
117
118
        $entry = reset($subList);
119
        /** @var Node $entry */
120
        $this->assertEquals([], $entry->getPath());
121
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
122
123
        $entry = next($subList);
124
        $this->assertEquals(['amogus'], $entry->getPath());
125
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
126
127
        $entry = next($subList);
128
        $this->assertEquals(['sus'], $entry->getPath());
129
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
130
131
        $this->assertFalse(next($subList));
132
    }
133
134
    /**
135
     * @throws FilesException
136
     * @throws PathsException
137
     */
138
    public function testRead1(): void
139
    {
140
        $lib = $this->getDirFlatLib();
141
        $subList = $lib->readDir([], false, true);
142
        usort($subList, [$this, 'sortingPaths']);
143
144
        $entry = reset($subList);
145
        /** @var Node $entry */
146
        $this->assertEquals([], $entry->getPath());
147
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
148
        $this->assertTrue($entry->isDir());
149
        $this->assertFalse($entry->isFile());
150
151
        $entry = next($subList);
152
        $this->assertEquals(['dummy1.txt'], $entry->getPath());
153
        $this->assertEquals(36, $entry->getSize());
154
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
155
        $this->assertFalse($entry->isDir());
156
        $this->assertTrue($entry->isFile());
157
158
        $entry = next($subList);
159
        $this->assertEquals(['dummy2.txt'], $entry->getPath());
160
        $this->assertEquals(36, $entry->getSize());
161
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
162
163
        $entry = next($subList);
164
        $this->assertEquals(['last_one'], $entry->getPath());
165
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
166
167
        $entry = next($subList);
168
        $this->assertEquals(['next_one'], $entry->getPath());
169
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
170
171
        $entry = next($subList);
172
        $this->assertEquals(['other1.txt'], $entry->getPath());
173
        $this->assertEquals(36, $entry->getSize());
174
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
175
176
        $entry = next($subList);
177
        $this->assertEquals(['other2.txt'], $entry->getPath());
178
        $this->assertEquals(36, $entry->getSize());
179
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
180
181
        $entry = next($subList);
182
        $this->assertEquals(['sub'], $entry->getPath());
183
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
184
185
        $this->assertFalse(next($subList));
186
    }
187
188
    /**
189
     * @throws FilesException
190
     * @throws PathsException
191
     */
192
    public function testRead2(): void
193
    {
194
        $lib = $this->getDirFlatLib();
195
        $subList = $lib->readDir(['next_one'], true);
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(['sub_one'], $entry->getPath());
203
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
204
205
        $entry = next($subList);
206
        $this->assertEquals(['sub_one', '.gitkeep'], $entry->getPath());
207
        $this->assertEquals(0, $entry->getSize());
208
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
209
210
        $this->assertFalse(next($subList));
211
    }
212
213
    /**
214
     * @throws FilesException
215
     * @throws PathsException
216
     */
217
    public function testRead3(): void
218
    {
219
        $lib = $this->getDirFlatLib();
220
        $subList = $lib->readDir(['last_one'], false);
221
        $entry = reset($subList);
222
        /** @var Node $entry */
223
        $this->assertEquals([], $entry->getPath());
224
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
225
226
        $entry = next($subList);
227
        $this->assertEquals(['.gitkeep'], $entry->getPath());
228
        $this->assertEquals(0, $entry->getSize());
229
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
230
231
        $this->assertFalse(next($subList));
232
    }
233
234
    /**
235
     * @throws FilesException
236
     * @throws PathsException
237
     */
238
    public function testReadFail(): void
239
    {
240
        $lib = $this->getDirFlatLib();
241
        $this->expectException(FilesException::class);
242
        $lib->readDir(['dummy2.txt']);
243
    }
244
245
    /**
246
     * @throws FilesException
247
     * @throws PathsException
248
     */
249
    public function testCopyMoveDelete(): void
250
    {
251
        $lib = $this->getDirFlatLib();
252
        $this->assertTrue($lib->copyDir(['next_one'], ['more']));
253
        $this->assertTrue($lib->moveDir(['more'], ['another']));
254
        $this->assertTrue($lib->deleteDir(['another'], true));
255
        $this->assertFalse($lib->deleteDir(['another']));
256
    }
257
}
258