|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SourcesTests; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_files\Access\Factory; |
|
7
|
|
|
use kalanis\kw_files\FilesException; |
|
8
|
|
|
use kalanis\kw_files\Interfaces\IProcessNodes; |
|
9
|
|
|
use kalanis\kw_files\Interfaces\ITypes; |
|
10
|
|
|
use kalanis\kw_files\Node; |
|
11
|
|
|
use kalanis\kw_paths\PathsException; |
|
12
|
|
|
use kalanis\kw_storage\Interfaces\ITarget; |
|
13
|
|
|
use kalanis\kw_storage\Storage\Key\DirKey; |
|
14
|
|
|
use kalanis\kw_storage\Storage\Storage; |
|
15
|
|
|
use kalanis\kw_storage\Storage\Target\Memory; |
|
16
|
|
|
use kalanis\kw_tree\Essentials\FileNode; |
|
17
|
|
|
use kalanis\kw_tree\Interfaces\ITree; |
|
18
|
|
|
use kalanis\kw_tree\DataSources\Files; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class FilesTest |
|
23
|
|
|
* @package SourcesTests |
|
24
|
|
|
*/ |
|
25
|
|
|
class FilesTest extends \CommonTestClass |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @throws FilesException |
|
29
|
|
|
* @throws PathsException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function testSimple(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$results = $this |
|
34
|
|
|
->getLib() |
|
35
|
|
|
->process() |
|
36
|
|
|
->getRoot(); |
|
37
|
|
|
$this->assertNotEmpty($results); |
|
38
|
|
|
$sub = $results->getSubNodes(); |
|
39
|
|
|
$this->assertNotEmpty($sub); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @throws FilesException |
|
44
|
|
|
* @throws PathsException |
|
45
|
|
|
*/ |
|
46
|
|
|
public function testFilesAllCallback(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$results = $this |
|
49
|
|
|
->getLib() |
|
50
|
|
|
->wantDeep(true) |
|
51
|
|
|
->setFilterCallback([$this, 'justDirsCallback']) |
|
52
|
|
|
->setOrdering(ITree::ORDER_DESC) |
|
53
|
|
|
->process() |
|
54
|
|
|
->getRoot(); |
|
55
|
|
|
$this->assertNotEmpty($results); |
|
56
|
|
|
$sub = $results->getSubNodes(); |
|
57
|
|
|
$this->assertNotEmpty($sub); |
|
58
|
|
|
|
|
59
|
|
|
$node = reset($sub); |
|
60
|
|
|
/** @var FileNode $node */ |
|
61
|
|
|
$this->assertEquals(['sub'], $node->getPath()); |
|
62
|
|
|
$this->assertEquals(ITypes::TYPE_DIR, $node->getType()); |
|
63
|
|
|
$this->assertTrue($node->isReadable()); |
|
64
|
|
|
$this->assertTrue($node->isWritable()); |
|
65
|
|
|
$this->assertEmpty($node->getSubNodes()); |
|
66
|
|
|
|
|
67
|
|
|
$node = next($sub); |
|
68
|
|
|
$this->assertEquals(['next_one'], $node->getPath()); |
|
69
|
|
|
$this->assertNotEmpty($node->getSubNodes()); |
|
70
|
|
|
|
|
71
|
|
|
$subs = $node->getSubNodes(); |
|
72
|
|
|
$node = reset($subs); |
|
73
|
|
|
$this->assertEquals(['next_one', 'sub_one'], $node->getPath()); |
|
74
|
|
|
$this->assertEmpty($node->getSubNodes()); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertFalse(next($subs)); |
|
77
|
|
|
|
|
78
|
|
|
$node = next($sub); |
|
79
|
|
|
$this->assertEquals(['last_one'], $node->getPath()); |
|
80
|
|
|
$this->assertEmpty($node->getSubNodes()); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertFalse(next($sub)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @throws FilesException |
|
87
|
|
|
* @throws PathsException |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testFilesLevel(): void |
|
90
|
|
|
{ |
|
91
|
|
|
$results = $this |
|
92
|
|
|
->getLib() |
|
93
|
|
|
->wantDeep(false) |
|
94
|
|
|
->setFilterCallback([$this, 'justFilesCallback']) |
|
95
|
|
|
->setOrdering(ITree::ORDER_ASC) |
|
96
|
|
|
->process() |
|
97
|
|
|
->getRoot(); |
|
98
|
|
|
$this->assertNotEmpty($results); |
|
99
|
|
|
$sub = $results->getSubNodes(); |
|
100
|
|
|
$this->assertNotEmpty($sub); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function justDirsCallback(Node $node): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return $node->isDir(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function justFilesCallback(Node $node): bool |
|
109
|
|
|
{ |
|
110
|
|
|
return $node->isFile(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @throws FilesException |
|
115
|
|
|
* @throws PathsException |
|
116
|
|
|
*/ |
|
117
|
|
|
public function testReversedShallow(): void |
|
118
|
|
|
{ |
|
119
|
|
|
$results = $this |
|
120
|
|
|
->getLib() |
|
121
|
|
|
->wantDeep(false) |
|
122
|
|
|
->setStartPath(['sub']) |
|
123
|
|
|
->setOrdering(ITree::ORDER_ASC) |
|
124
|
|
|
->process() |
|
125
|
|
|
->getRoot(); |
|
126
|
|
|
$this->assertNotEmpty($results); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @throws FilesException |
|
131
|
|
|
* @throws PathsException |
|
132
|
|
|
*/ |
|
133
|
|
|
public function testNothing(): void |
|
134
|
|
|
{ |
|
135
|
|
|
$lib = $this |
|
136
|
|
|
->getLib() |
|
137
|
|
|
->setStartPath(['path', 'does not', 'exists']) |
|
138
|
|
|
->setOrdering(ITree::ORDER_NONE) |
|
139
|
|
|
->process(); |
|
140
|
|
|
$this->assertEmpty($lib->getRoot()); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @throws FilesException |
|
145
|
|
|
* @throws PathsException |
|
146
|
|
|
* @return Files |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function getLib(): Files |
|
149
|
|
|
{ |
|
150
|
|
|
$compFact = new Factory(); |
|
151
|
|
|
DirKey::setDir(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree'); |
|
152
|
|
|
$storage = new Storage(new DirKey(), $this->filledMemory()); |
|
153
|
|
|
return new Files($compFact->getClass($storage)); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
protected function filledMemory(): ITarget |
|
157
|
|
|
{ |
|
158
|
|
|
$lib = new Memory(); |
|
159
|
|
|
$lib->save(DIRECTORY_SEPARATOR . 'data', IProcessNodes::STORAGE_NODE_KEY); |
|
160
|
|
|
$lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree', IProcessNodes::STORAGE_NODE_KEY); |
|
161
|
|
|
$lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'last_one', IProcessNodes::STORAGE_NODE_KEY); |
|
162
|
|
|
$lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'last_one' . DIRECTORY_SEPARATOR . '.gitkeep', ''); |
|
163
|
|
|
$lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'sub', IProcessNodes::STORAGE_NODE_KEY); |
|
164
|
|
|
$lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'sub' . DIRECTORY_SEPARATOR . 'dummy3.txt', 'qwertzuiopasdfghjklyxcvbnm0123456789'); |
|
165
|
|
|
$lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'sub' . DIRECTORY_SEPARATOR . 'dummy4.bin', false); // intentionally!!! |
|
166
|
|
|
$lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'next_one', IProcessNodes::STORAGE_NODE_KEY); |
|
167
|
|
|
$lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'next_one' . DIRECTORY_SEPARATOR . 'sub_one', IProcessNodes::STORAGE_NODE_KEY); |
|
168
|
|
|
$lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'next_one' . DIRECTORY_SEPARATOR . 'sub_one' . DIRECTORY_SEPARATOR . '.gitkeep', ''); |
|
169
|
|
|
$lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'dummy1.txt', 'qwertzuiopasdfghjklyxcvbnm0123456789'); |
|
170
|
|
|
$lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'other2.doc', 'qwertzuiopasdfghjklyxcvbnm0123456789'); |
|
171
|
|
|
$lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'other1.txt', 'qwertzuiopasdfghjklyxcvbnm0123456789'); |
|
172
|
|
|
$lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'dummy2.doc', 'qwertzuiopasdfghjklyxcvbnm0123456789'); |
|
173
|
|
|
return $lib; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|