1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StorageTests; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use CommonTestClass; |
7
|
|
|
use kalanis\kw_storage\Interfaces\IPassDirs; |
8
|
|
|
use kalanis\kw_storage\Storage\Key\DirKey; |
9
|
|
|
use kalanis\kw_storage\Storage\StorageDirs; |
10
|
|
|
use kalanis\kw_storage\Storage\Target; |
11
|
|
|
use kalanis\kw_storage\StorageException; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class VolumeStorageTest extends CommonTestClass |
15
|
|
|
{ |
16
|
|
|
public function tearDown(): void |
17
|
|
|
{ |
18
|
|
|
$this->rmFile($this->mockTestFile('', false)); |
19
|
|
|
$this->rmFile($this->mockTestFile('2', false)); |
20
|
|
|
$this->rmFile($this->mockTestFile('3', false)); |
21
|
|
|
$this->rmFile($this->mockTestFile('4', false)); |
22
|
|
|
$this->rmFile($this->mockTestFile('5', false)); |
23
|
|
|
$this->rmDir($this->mockTestFile('', false)); |
24
|
|
|
$this->rmDir('some'); |
25
|
|
|
parent::tearDown(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @throws StorageException |
30
|
|
|
*/ |
31
|
|
|
public function testDir(): void |
32
|
|
|
{ |
33
|
|
|
$volume = $this->getLib(); |
34
|
|
|
$this->assertTrue($volume->isDir('.')); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @throws StorageException |
39
|
|
|
*/ |
40
|
|
|
public function testExists(): void |
41
|
|
|
{ |
42
|
|
|
$volume = $this->getLib(); |
43
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile('', false))); |
44
|
|
|
$this->assertFalse($volume->isReadable($this->mockTestFile('', false))); |
45
|
|
|
$this->assertFalse($volume->isWritable($this->mockTestFile('', false))); |
46
|
|
|
$this->assertFalse($volume->isDir($this->mockTestFile('', false))); |
47
|
|
|
$this->expectException(StorageException::class); |
48
|
|
|
$volume->read($this->mockTestFile('', false)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @throws StorageException |
53
|
|
|
*/ |
54
|
|
|
public function testOperations(): void |
55
|
|
|
{ |
56
|
|
|
$volume = $this->getLib(); |
57
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile('', false))); |
58
|
|
|
$this->assertFalse($volume->isDir($this->mockTestFile('', false))); |
59
|
|
|
$this->assertFalse($volume->isReadable($this->mockTestFile('', false))); |
60
|
|
|
$this->assertFalse($volume->isWritable($this->mockTestFile('', false))); |
61
|
|
|
$this->assertTrue($volume->write($this->mockTestFile('', false), 'asdfghjklpoiuztrewqyxcvbnm')); |
62
|
|
|
$this->assertTrue($volume->exists($this->mockTestFile('', false))); |
63
|
|
|
$this->assertTrue($volume->isReadable($this->mockTestFile('', false))); |
64
|
|
|
$this->assertTrue($volume->isWritable($this->mockTestFile('', false))); |
65
|
|
|
$this->assertFalse($volume->isDir($this->mockTestFile('', false))); |
66
|
|
|
$this->assertTrue($volume->isFile($this->mockTestFile('', false))); |
67
|
|
|
$this->assertEquals(26, $volume->size($this->mockTestFile('', false))); |
68
|
|
|
$created = $volume->created($this->mockTestFile('', false)); |
69
|
|
|
$now = time(); |
70
|
|
|
$this->assertTrue($created > $now - 10); |
71
|
|
|
$this->assertTrue($created < $now + 10); |
72
|
|
|
$this->assertEquals('asdfghjklpoiuztrewqyxcvbnm', $volume->read($this->mockTestFile('', false))); |
73
|
|
|
$this->assertTrue($volume->copy($this->mockTestFile('', false), $this->mockTestFile('2', false))); |
74
|
|
|
$this->assertTrue($volume->move($this->mockTestFile('', false), $this->mockTestFile('3', false))); |
75
|
|
|
$this->assertFalse($volume->copy($this->mockTestFile('', false), $this->mockTestFile('4', false))); |
76
|
|
|
$this->assertFalse($volume->move($this->mockTestFile('', false), $this->mockTestFile('5', false))); |
77
|
|
|
$this->assertFalse($volume->remove($this->mockTestFile('', false))); |
78
|
|
|
$this->assertTrue($volume->remove($this->mockTestFile('2', false))); |
79
|
|
|
$this->assertTrue($volume->remove($this->mockTestFile('3', false))); |
80
|
|
|
$this->assertFalse($volume->remove($this->mockTestFile('4', false))); |
81
|
|
|
$this->assertFalse($volume->remove($this->mockTestFile('5', false))); |
82
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile('', false))); |
83
|
|
|
$this->assertFalse($volume->isReadable($this->mockTestFile('', false))); |
84
|
|
|
$this->assertFalse($volume->isWritable($this->mockTestFile('', false))); |
85
|
|
|
$this->assertFalse($volume->isDir($this->mockTestFile('', false))); |
86
|
|
|
$this->assertNull($volume->size($this->mockTestFile('', false))); |
87
|
|
|
$this->assertNull($volume->created($this->mockTestFile('', false))); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @throws StorageException |
92
|
|
|
*/ |
93
|
|
|
public function testDirsRecursive(): void |
94
|
|
|
{ |
95
|
|
|
$volume = $this->getLib(); |
96
|
|
|
$this->assertTrue($volume->mkDir(implode(DIRECTORY_SEPARATOR, ['some', 'none', 'hoo']), true)); |
97
|
|
|
$this->assertTrue($volume->exists('some')); |
98
|
|
|
$this->assertFalse($volume->exists('some' . DIRECTORY_SEPARATOR . 'hint.none')); |
99
|
|
|
$this->assertTrue($volume->write('some' . DIRECTORY_SEPARATOR . 'hint.none', 'asdfghjklpoiuztrewqyxcvbnm')); |
100
|
|
|
$this->assertTrue($volume->rmDir('some', true)); |
101
|
|
|
$this->assertFalse($volume->isDir('some')); |
102
|
|
|
$this->assertFalse($volume->exists('some')); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @throws StorageException |
107
|
|
|
*/ |
108
|
|
|
public function testLookup(): void |
109
|
|
|
{ |
110
|
|
|
$volume = $this->getLib(); |
111
|
|
|
$testFiles = [ |
112
|
|
|
'dummyFile.tst' => 'dummyFile.tst', |
113
|
|
|
'dummyFile.0.tst' => 'dummyFile.0.tst', |
114
|
|
|
'dummyFile.1.tst' => 'dummyFile.1.tst', |
115
|
|
|
'dummyFile.2.tst' => 'dummyFile.2.tst', |
116
|
|
|
]; |
117
|
|
|
$removal = $volume->removeMulti($testFiles); |
118
|
|
|
$this->assertEquals([ |
119
|
|
|
'dummyFile.tst' => false, |
120
|
|
|
'dummyFile.0.tst' => false, |
121
|
|
|
'dummyFile.1.tst' => false, |
122
|
|
|
'dummyFile.2.tst' => false, |
123
|
|
|
], $removal); |
124
|
|
|
|
125
|
|
|
file_put_contents($this->getTestDir() . 'dummyFile.tst', 'asdfghjklqwertzuiopyxcvbnm'); |
126
|
|
|
file_put_contents($this->getTestDir() . 'dummyFile.0.tst', 'asdfghjklqwertzuiopyxcvbnm'); |
127
|
|
|
file_put_contents($this->getTestDir() . 'dummyFile.1.tst', 'asdfghjklqwertzuiopyxcvbnm'); |
128
|
|
|
file_put_contents($this->getTestDir() . 'dummyFile.2.tst', 'asdfghjklqwertzuiopyxcvbnm'); |
129
|
|
|
|
130
|
|
|
// non-existent path |
131
|
|
|
$this->assertEquals(0, count(array_filter(array_filter(iterator_to_array($volume->lookup('this path does not exists'))), [$this, 'dotDirs']))); |
132
|
|
|
|
133
|
|
|
// empty path - must show everything |
134
|
|
|
// 5 -> + gitkeep |
135
|
|
|
$this->assertEquals(5, count(array_filter(array_filter(iterator_to_array($volume->lookup(''))), [$this, 'dotDirs']))); |
136
|
|
|
|
137
|
|
|
$files = iterator_to_array($volume->lookup('')); |
138
|
|
|
sort($files); |
139
|
|
|
$files = array_filter(array_filter($files), [$this, 'dotDirs']); |
140
|
|
|
|
141
|
|
|
$this->assertEquals(count($testFiles) + 1, count($files)); // because gitkeep |
142
|
|
|
$this->assertEquals(DIRECTORY_SEPARATOR . '.gitkeep', reset($files)); |
143
|
|
|
$this->assertEquals(DIRECTORY_SEPARATOR . 'dummyFile.0.tst', next($files)); |
144
|
|
|
$this->assertEquals(DIRECTORY_SEPARATOR . 'dummyFile.1.tst', next($files)); |
145
|
|
|
$this->assertEquals(DIRECTORY_SEPARATOR . 'dummyFile.2.tst', next($files)); |
146
|
|
|
$this->assertEquals(DIRECTORY_SEPARATOR . 'dummyFile.tst', next($files)); |
147
|
|
|
|
148
|
|
|
$removal = $volume->removeMulti($testFiles); |
149
|
|
|
$this->assertFalse($volume->exists('dummyFile.tst')); |
150
|
|
|
$this->assertFalse($volume->exists('dummyFile.0.tst')); |
151
|
|
|
$this->assertFalse($volume->exists('dummyFile.1.tst')); |
152
|
|
|
$this->assertFalse($volume->exists('dummyFile.2.tst')); |
153
|
|
|
|
154
|
|
|
$this->assertEquals([ |
155
|
|
|
'dummyFile.tst' => true, |
156
|
|
|
'dummyFile.0.tst' => true, |
157
|
|
|
'dummyFile.1.tst' => true, |
158
|
|
|
'dummyFile.2.tst' => true, |
159
|
|
|
], $removal); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function dotDirs(string $path): bool |
163
|
|
|
{ |
164
|
|
|
return !in_array($path, ['.', '..', DIRECTORY_SEPARATOR . '.', DIRECTORY_SEPARATOR . '..']); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @throws StorageException |
169
|
|
|
*/ |
170
|
|
|
public function testSimpleCounter(): void |
171
|
|
|
{ |
172
|
|
|
$volume = $this->getLib(); |
173
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile('', false))); |
174
|
|
|
$this->assertTrue($volume->write($this->mockTestFile('', false), 15)); |
175
|
|
|
$this->assertTrue($volume->decrement($this->mockTestFile('', false))); |
176
|
|
|
$this->assertTrue($volume->decrement($this->mockTestFile('', false))); |
177
|
|
|
$this->assertTrue($volume->increment($this->mockTestFile('', false))); |
178
|
|
|
$this->assertEquals(14, $volume->read($this->mockTestFile('', false))); |
179
|
|
|
$this->assertTrue($volume->remove($this->mockTestFile('', false))); |
180
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile('', false))); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @throws StorageException |
185
|
|
|
*/ |
186
|
|
|
public function testHarderCounter(): void |
187
|
|
|
{ |
188
|
|
|
$volume = $this->getLib(); |
189
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile('', false))); |
190
|
|
|
$this->assertTrue($volume->decrement($this->mockTestFile('', false))); |
191
|
|
|
$this->assertTrue($volume->increment($this->mockTestFile('', false))); |
192
|
|
|
$this->assertEquals(1, $volume->read($this->mockTestFile('', false))); |
193
|
|
|
$this->assertTrue($volume->remove($this->mockTestFile('', false))); |
194
|
|
|
$this->assertTrue($volume->increment($this->mockTestFile('', false))); |
195
|
|
|
$this->assertTrue($volume->decrement($this->mockTestFile('', false))); |
196
|
|
|
$this->assertEquals(0, $volume->read($this->mockTestFile('', false))); |
197
|
|
|
$this->assertTrue($volume->remove($this->mockTestFile('', false))); |
198
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile('', false))); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
protected function getLib(): IPassDirs |
202
|
|
|
{ |
203
|
|
|
DirKey::setDir($this->getTestDir()); |
204
|
|
|
$volume = new Target\Volume(); |
205
|
|
|
$volume->check($this->getTestDir()); |
206
|
|
|
return new StorageDirs(new DirKey(), $volume); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|