Passed
Push — master ( 79545d...728467 )
by Petr
02:28
created

DirTest::testMoveFail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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
        usort($subList, [$this, 'sortingPaths']);
151
152
        $entry = reset($subList);
153
        /** @var Node $entry */
154
        $this->assertEquals([], $entry->getPath());
155
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
156
157
        $entry = next($subList);
158
        $this->assertEquals(['sub_one'], $entry->getPath());
159
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
160
161
        $entry = next($subList);
162
        $this->assertEquals(['sub_one', '.gitkeep'], $entry->getPath());
163
        $this->assertEquals(0, $entry->getSize());
164
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
165
166
        $this->assertFalse(next($subList));
167
    }
168
169
    /**
170
     * @throws FilesException
171
     * @throws PathsException
172
     */
173
    public function testRead3(): void
174
    {
175
        $lib = $this->getDirLib();
176
        $subList = $lib->readDir(['last_one'], false);
177
        usort($subList, [$this, 'sortingPaths']);
178
179
        $entry = reset($subList);
180
        /** @var Node $entry */
181
        $this->assertEquals([], $entry->getPath());
182
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
183
184
        $entry = next($subList);
185
        $this->assertEquals(['.gitkeep'], $entry->getPath());
186
        $this->assertEquals(0, $entry->getSize());
187
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
188
189
        $this->assertFalse(next($subList));
190
    }
191
192
    /**
193
     * @throws FilesException
194
     * @throws PathsException
195
     */
196
    public function testRead4(): void
197
    {
198
        $lib = $this->getDirTreeLib();
199
        $subList = $lib->readDir([], false);
200
        $entry = reset($subList);
201
        usort($subList, [$this, 'sortingPaths']);
202
203
        /** @var Node $entry */
204
        $this->assertEquals([], $entry->getPath());
205
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
206
207
        $entry = next($subList);
208
        $this->assertEquals(['data'], $entry->getPath());
209
        $this->assertEquals(0, $entry->getSize());
210
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
211
212
        $this->assertFalse(next($subList));
213
    }
214
215
    /**
216
     * @throws FilesException
217
     * @throws PathsException
218
     */
219
    public function testReadFail(): void
220
    {
221
        $lib = $this->getDirLib();
222
        $this->expectException(FilesException::class);
223
        $lib->readDir(['dummy2.txt']);
224
    }
225
226
    /**
227
     * @throws FilesException
228
     * @throws PathsException
229
     */
230
    public function testCopyMoveDelete(): void
231
    {
232
        $lib = $this->getDirLib();
233
        $this->assertTrue($lib->copyDir(['next_one'], ['more']));
234
        $this->assertTrue($lib->moveDir(['more'], ['another']));
235
        $this->assertTrue($lib->deleteDir(['another'], true));
236
        $this->assertFalse($lib->deleteDir(['another']));
237
    }
238
239
    /**
240
     * @throws FilesException
241
     * @throws PathsException
242
     */
243
    public function testDeleteShallow(): void
244
    {
245
        $lib = $this->getDirLib();
246
        $this->assertTrue($lib->createDir(['another']));
247
        $this->assertTrue($lib->deleteDir(['another']));
248
    }
249
250
    /**
251
     * @throws FilesException
252
     * @throws PathsException
253
     */
254
    public function testDeleteDeep(): void
255
    {
256
        $lib = $this->getDirLib();
257
        $this->assertTrue($lib->deleteDir(['next_one'], true));
258
    }
259
}
260