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