Passed
Push — master ( cb2d88...e8a992 )
by Petr
08:25
created

DirTest::testRead4()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 17
rs 9.8666
c 0
b 0
f 0
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_paths\PathsException;
10
11
12
class DirTest extends AStorageTest
13
{
14
    /**
15
     * @throws FilesException
16
     * @throws PathsException
17
     */
18
    public function testCreate(): void
19
    {
20
        $lib = $this->getDirLib();
21
        $this->assertTrue($lib->createDir(['another'], false));
22
        $this->assertTrue($lib->createDir(['sub', 'added'], false)); // not exists in sub dir
23
        $this->assertFalse($lib->createDir(['sub', 'added'], true)); // already exists in sub dir
24
        $this->assertFalse($lib->createDir(['more', 'added'], false)); // not exists both dirs, cannot deep
25
        $this->assertTrue($lib->createDir(['more', 'added'], true)); // not exists both dirs, can deep
26
    }
27
28
    /**
29
     * @throws FilesException
30
     * @throws PathsException
31
     */
32
    public function testRead1(): void
33
    {
34
        $lib = $this->getDirLib();
35
        $subList = $lib->readDir([''], false, true);
36
        usort($subList, [$this, 'sortingPaths']);
37
38
        $entry = reset($subList);
39
        /** @var Node $entry */
40
        $name = array_slice($entry->getPath(), -1);
41
        $this->assertEquals('', reset($name));
42
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
43
        $this->assertTrue($entry->isDir());
44
        $this->assertFalse($entry->isFile());
45
46
        $entry = next($subList);
47
        $name = array_slice($entry->getPath(), -1);
48
        $this->assertEquals('dummy1.txt', reset($name));
49
        $this->assertEquals(36, $entry->getSize());
50
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
51
        $this->assertFalse($entry->isDir());
52
        $this->assertTrue($entry->isFile());
53
54
        $entry = next($subList);
55
        $name = array_slice($entry->getPath(), -1);
56
        $this->assertEquals('dummy2.txt', reset($name));
57
        $this->assertEquals(36, $entry->getSize());
58
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
59
60
        $entry = next($subList);
61
        $name = array_slice($entry->getPath(), -1);
62
        $this->assertEquals('last_one', reset($name));
63
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
64
65
        $entry = next($subList);
66
        $name = array_slice($entry->getPath(), -1);
67
        $this->assertEquals('next_one', reset($name));
68
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
69
70
        $entry = next($subList);
71
        $name = array_slice($entry->getPath(), -1);
72
        $this->assertEquals('other1.txt', reset($name));
73
        $this->assertEquals(36, $entry->getSize());
74
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
75
76
        $entry = next($subList);
77
        $name = array_slice($entry->getPath(), -1);
78
        $this->assertEquals('other2.txt', reset($name));
79
        $this->assertEquals(36, $entry->getSize());
80
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
81
82
        $entry = next($subList);
83
        $name = array_slice($entry->getPath(), -1);
84
        $this->assertEquals('sub', reset($name));
85
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
86
87
        $this->assertFalse(next($subList));
88
    }
89
90
    /**
91
     * @throws FilesException
92
     * @throws PathsException
93
     */
94
    public function testRead2(): void
95
    {
96
        $lib = $this->getDirLib();
97
        $subList = $lib->readDir(['next_one'], true);
98
        $entry = reset($subList);
99
        /** @var Node $entry */
100
        $name = array_slice($entry->getPath(), -1);
101
        $this->assertEquals('next_one', reset($name));
102
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
103
104
        $entry = next($subList);
105
        $name = array_slice($entry->getPath(), -1);
106
        $this->assertEquals('sub_one', reset($name));
107
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
108
109
        $entry = next($subList);
110
        $name = array_slice($entry->getPath(), -1);
111
        $this->assertEquals('.gitkeep', reset($name));
112
        $this->assertEquals(0, $entry->getSize());
113
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
114
115
        $this->assertFalse(next($subList));
116
    }
117
118
    /**
119
     * @throws FilesException
120
     * @throws PathsException
121
     */
122
    public function testRead3(): void
123
    {
124
        $lib = $this->getDirLib();
125
        $subList = $lib->readDir(['last_one'], false);
126
        $entry = reset($subList);
127
        /** @var Node $entry */
128
        $name = array_slice($entry->getPath(), -1);
129
        $this->assertEquals('last_one', reset($name));
130
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
131
132
        $entry = next($subList);
133
        $name = array_slice($entry->getPath(), -1);
134
        $this->assertEquals('.gitkeep', reset($name));
135
        $this->assertEquals(0, $entry->getSize());
136
        $this->assertEquals(ITypes::TYPE_FILE, $entry->getType());
137
138
        $this->assertFalse(next($subList));
139
    }
140
141
    /**
142
     * @throws FilesException
143
     * @throws PathsException
144
     */
145
    public function testRead4(): void
146
    {
147
        $lib = $this->getDirTreeLib();
148
        $subList = $lib->readDir([], false);
149
        $entry = reset($subList);
150
        /** @var Node $entry */
151
        $name = array_slice($entry->getPath(), -1);
152
        $this->assertEquals('', reset($name));
153
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
154
155
        $entry = next($subList);
156
        $name = array_slice($entry->getPath(), -1);
157
        $this->assertEquals('data', reset($name));
158
        $this->assertEquals(0, $entry->getSize());
159
        $this->assertEquals(ITypes::TYPE_DIR, $entry->getType());
160
161
        $this->assertFalse(next($subList));
162
    }
163
164
    /**
165
     * @throws FilesException
166
     * @throws PathsException
167
     */
168
    public function testReadFail(): void
169
    {
170
        $lib = $this->getDirLib();
171
        $this->expectException(FilesException::class);
172
        $lib->readDir(['dummy2.txt']);
173
    }
174
175
    /**
176
     * @throws FilesException
177
     * @throws PathsException
178
     */
179
    public function testCopyMoveDelete(): void
180
    {
181
        $lib = $this->getDirLib();
182
        $this->assertTrue($lib->copyDir(['next_one'], ['more']));
183
        $this->assertTrue($lib->moveDir(['more'], ['another']));
184
        $this->assertTrue($lib->deleteDir(['another'], true));
185
        $this->assertFalse($lib->deleteDir(['another']));
186
    }
187
188
    /**
189
     * @throws FilesException
190
     * @throws PathsException
191
     */
192
    public function testCopyFail(): void
193
    {
194
        $lib = $this->getDirLib();
195
        $this->assertFalse($lib->copyDir(['next_one'], ['other2.txt'])); // dest exists
196
        $this->assertFalse($lib->copyDir(['more'], ['another'])); // source is not exists
197
    }
198
199
    /**
200
     * @throws FilesException
201
     * @throws PathsException
202
     */
203
    public function testMoveFail(): void
204
    {
205
        $lib = $this->getDirLib();
206
        $this->assertFalse($lib->moveDir(['next_one'], ['other2.txt'])); // dest exists
207
        $this->assertFalse($lib->moveDir(['more'], ['another'])); // source not exists
208
    }
209
210
    /**
211
     * @throws FilesException
212
     * @throws PathsException
213
     */
214
    public function testDeleteFail(): void
215
    {
216
        $lib = $this->getDirLib();
217
        $this->assertFalse($lib->deleteDir(['other2.txt']));
218
        $this->assertFalse($lib->deleteDir(['more']));
219
    }
220
221
    /**
222
     * @throws FilesException
223
     * @throws PathsException
224
     */
225
    public function testDeepDeleteFail(): void
226
    {
227
        $lib = $this->getDirLib();
228
        $this->assertTrue($lib->createDir(['some', 'more'], true));
229
        $this->assertFalse($lib->deleteDir(['some']));
230
    }
231
}
232