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

DirRecursiveTest::testRead1()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 36
nc 1
nop 0
dl 0
loc 48
rs 9.344
c 1
b 0
f 1
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
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->getDirRecursiveLib();
220
        $subList = $lib->readDir(['last_one'], false);
221
222
        $entry = reset($subList);
223
        /** @var Node $entry */
224
        $this->assertEquals([], $entry->getPath());
225
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
226
227
        $entry = next($subList);
228
        $this->assertEquals(['.gitkeep'], $entry->getPath());
229
        $this->assertEquals(0, $entry->getSize());
230
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
231
232
        $this->assertFalse(next($subList));
233
    }
234
235
    /**
236
     * @throws FilesException
237
     * @throws PathsException
238
     */
239
    public function testReadFail(): void
240
    {
241
        $lib = $this->getDirRecursiveLib();
242
        $this->expectException(FilesException::class);
243
        $lib->readDir(['dummy2.txt']);
244
    }
245
246
    /**
247
     * @throws FilesException
248
     * @throws PathsException
249
     */
250
    public function testCopyMoveDelete(): void
251
    {
252
        $lib = $this->getDirRecursiveLib();
253
        $this->assertTrue($lib->copyDir(['next_one'], ['more']));
254
        $this->assertTrue($lib->moveDir(['more'], ['another']));
255
        $this->assertTrue($lib->deleteDir(['another'], true));
256
        $this->assertFalse($lib->deleteDir(['another']));
257
    }
258
}
259