|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace VolumeTests; |
|
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 AVolume |
|
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); |
|
36
|
|
|
usort($subList, [$this, 'sortingPaths']); |
|
37
|
|
|
|
|
38
|
|
|
$entry = reset($subList); |
|
39
|
|
|
/** @var Node $entry */ |
|
40
|
|
|
$this->assertEquals([], $entry->getPath()); |
|
41
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
|
42
|
|
|
$this->assertTrue($entry->isDir()); |
|
43
|
|
|
$this->assertFalse($entry->isFile()); |
|
44
|
|
|
|
|
45
|
|
|
$entry = next($subList); |
|
46
|
|
|
$this->assertEquals(['dummy1.txt'], $entry->getPath()); |
|
47
|
|
|
$this->assertEquals(36, $entry->getSize()); |
|
48
|
|
|
$this->assertEquals(ITypes::TYPE_FILE, $entry->getType()); |
|
49
|
|
|
$this->assertFalse($entry->isDir()); |
|
50
|
|
|
$this->assertTrue($entry->isFile()); |
|
51
|
|
|
|
|
52
|
|
|
$entry = next($subList); |
|
53
|
|
|
$this->assertEquals(['dummy2.txt'], $entry->getPath()); |
|
54
|
|
|
$this->assertEquals(36, $entry->getSize()); |
|
55
|
|
|
$this->assertEquals(ITypes::TYPE_FILE, $entry->getType()); |
|
56
|
|
|
|
|
57
|
|
|
$entry = next($subList); |
|
58
|
|
|
$this->assertEquals(['last_one'], $entry->getPath()); |
|
59
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
|
60
|
|
|
|
|
61
|
|
|
$entry = next($subList); |
|
62
|
|
|
$this->assertEquals(['next_one'], $entry->getPath()); |
|
63
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
|
64
|
|
|
|
|
65
|
|
|
$entry = next($subList); |
|
66
|
|
|
$this->assertEquals(['other1.txt'], $entry->getPath()); |
|
67
|
|
|
$this->assertEquals(36, $entry->getSize()); |
|
68
|
|
|
$this->assertEquals(ITypes::TYPE_FILE, $entry->getType()); |
|
69
|
|
|
|
|
70
|
|
|
$entry = next($subList); |
|
71
|
|
|
$this->assertEquals(['other2.txt'], $entry->getPath()); |
|
72
|
|
|
$this->assertEquals(36, $entry->getSize()); |
|
73
|
|
|
$this->assertEquals(ITypes::TYPE_FILE, $entry->getType()); |
|
74
|
|
|
|
|
75
|
|
|
$entry = next($subList); |
|
76
|
|
|
$this->assertEquals(['sub'], $entry->getPath()); |
|
77
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertFalse(next($subList)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @throws FilesException |
|
84
|
|
|
* @throws PathsException |
|
85
|
|
|
*/ |
|
86
|
|
|
public function testRead2(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$lib = $this->getDirLib(); |
|
89
|
|
|
$subList = $lib->readDir(['next_one'], true); |
|
90
|
|
|
usort($subList, [$this, 'sortingPaths']); |
|
91
|
|
|
|
|
92
|
|
|
$entry = reset($subList); |
|
93
|
|
|
/** @var Node $entry */ |
|
94
|
|
|
$this->assertEquals([], $entry->getPath()); |
|
95
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
|
96
|
|
|
|
|
97
|
|
|
$entry = next($subList); |
|
98
|
|
|
$this->assertEquals(['sub_one'], $entry->getPath()); |
|
99
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $entry->getType()); |
|
100
|
|
|
|
|
101
|
|
|
$entry = next($subList); |
|
102
|
|
|
$this->assertEquals(['sub_one', '.gitkeep'], $entry->getPath()); |
|
103
|
|
|
$this->assertEquals(0, $entry->getSize()); |
|
104
|
|
|
$this->assertEquals(ITypes::TYPE_FILE, $entry->getType()); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertFalse(next($subList)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @throws FilesException |
|
111
|
|
|
* @throws PathsException |
|
112
|
|
|
*/ |
|
113
|
|
|
public function testRead3(): void |
|
114
|
|
|
{ |
|
115
|
|
|
$lib = $this->getDirLib(); |
|
116
|
|
|
$subList = $lib->readDir(['last_one'], false); |
|
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(['.gitkeep'], $entry->getPath()); |
|
124
|
|
|
$this->assertEquals(0, $entry->getSize()); |
|
125
|
|
|
$this->assertEquals(ITypes::TYPE_FILE, $entry->getType()); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertFalse(next($subList)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @throws FilesException |
|
132
|
|
|
* @throws PathsException |
|
133
|
|
|
*/ |
|
134
|
|
|
public function testReadFail(): void |
|
135
|
|
|
{ |
|
136
|
|
|
$lib = $this->getDirLib(); |
|
137
|
|
|
$this->expectException(FilesException::class); |
|
138
|
|
|
$lib->readDir(['dummy2.txt']); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @throws FilesException |
|
143
|
|
|
* @throws PathsException |
|
144
|
|
|
*/ |
|
145
|
|
|
public function testCopyMoveDelete(): void |
|
146
|
|
|
{ |
|
147
|
|
|
$lib = $this->getDirLib(); |
|
148
|
|
|
$this->assertTrue($lib->copyDir(['next_one'], ['more'])); |
|
149
|
|
|
$this->assertTrue($lib->moveDir(['more'], ['another'])); |
|
150
|
|
|
$this->assertTrue($lib->deleteDir(['another'], true)); |
|
151
|
|
|
$this->assertFalse($lib->deleteDir(['another'])); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @throws FilesException |
|
156
|
|
|
* @throws PathsException |
|
157
|
|
|
*/ |
|
158
|
|
|
public function testDeleteShallow(): void |
|
159
|
|
|
{ |
|
160
|
|
|
$lib = $this->getDirLib(); |
|
161
|
|
|
$this->assertTrue($lib->createDir(['another'])); |
|
162
|
|
|
$this->assertTrue($lib->deleteDir(['another'])); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @throws FilesException |
|
167
|
|
|
* @throws PathsException |
|
168
|
|
|
*/ |
|
169
|
|
|
public function testDeleteDeep(): void |
|
170
|
|
|
{ |
|
171
|
|
|
$lib = $this->getDirLib(); |
|
172
|
|
|
$this->assertTrue($lib->createDir(['another', 'sub_one'], true)); |
|
173
|
|
|
$this->assertFalse($lib->deleteDir(['another'])); |
|
174
|
|
|
$this->assertTrue($lib->deleteDir(['another'], true)); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|