|
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
|
|
|
rmdir($mockPath); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @throws StorageException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function testExists(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$volume = new Target\Volume(); |
|
47
|
|
|
$this->assertTrue($volume->check($this->getTestDir())); |
|
48
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile())); |
|
49
|
|
|
$this->assertFalse($volume->isDir($this->mockTestFile())); |
|
50
|
|
|
$this->expectException(StorageException::class); |
|
51
|
|
|
$volume->load($this->mockTestFile()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @throws StorageException |
|
56
|
|
|
*/ |
|
57
|
|
|
public function testOperations(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$volume = new Target\Volume(); |
|
60
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile())); |
|
61
|
|
|
$this->assertFalse($volume->isDir($this->mockTestFile())); |
|
62
|
|
|
$this->assertTrue($volume->save($this->mockTestFile(), 'asdfghjklpoiuztrewqyxcvbnm')); |
|
63
|
|
|
$this->assertTrue($volume->exists($this->mockTestFile())); |
|
64
|
|
|
$this->assertFalse($volume->isDir($this->mockTestFile())); |
|
65
|
|
|
$this->assertTrue($volume->isFile($this->mockTestFile())); |
|
66
|
|
|
$this->assertEquals(26, $volume->size($this->mockTestFile())); |
|
67
|
|
|
$created = $volume->created($this->mockTestFile()); |
|
68
|
|
|
$now = time(); |
|
69
|
|
|
$this->assertTrue($created > $now - 10); |
|
70
|
|
|
$this->assertTrue($created < $now + 10); |
|
71
|
|
|
$this->assertEquals('asdfghjklpoiuztrewqyxcvbnm', $volume->load($this->mockTestFile())); |
|
72
|
|
|
$this->assertTrue($volume->copy($this->mockTestFile(), $this->mockTestFile('2'))); |
|
73
|
|
|
$this->assertTrue($volume->move($this->mockTestFile(), $this->mockTestFile('3'))); |
|
74
|
|
|
$this->assertFalse($volume->copy($this->mockTestFile(), $this->mockTestFile('4'))); |
|
75
|
|
|
$this->assertFalse($volume->move($this->mockTestFile(), $this->mockTestFile('5'))); |
|
76
|
|
|
$this->assertFalse($volume->remove($this->mockTestFile())); |
|
77
|
|
|
$this->assertTrue($volume->remove($this->mockTestFile('2'))); |
|
78
|
|
|
$this->assertTrue($volume->remove($this->mockTestFile('3'))); |
|
79
|
|
|
$this->assertFalse($volume->remove($this->mockTestFile('4'))); |
|
80
|
|
|
$this->assertFalse($volume->remove($this->mockTestFile('5'))); |
|
81
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile())); |
|
82
|
|
|
$this->assertFalse($volume->isDir($this->mockTestFile())); |
|
83
|
|
|
$this->assertNull($volume->size($this->mockTestFile())); |
|
84
|
|
|
$this->assertNull($volume->created($this->mockTestFile())); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @throws StorageException |
|
89
|
|
|
*/ |
|
90
|
|
|
public function testDirsRecursive(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$volume = new Target\Volume(); |
|
93
|
|
|
$this->assertTrue($volume->mkDir($this->getTestDir() . implode(DIRECTORY_SEPARATOR, ['some', 'none', 'hoo']), true)); |
|
94
|
|
|
$this->assertTrue($volume->exists($this->getTestDir() . 'some')); |
|
95
|
|
|
$this->assertFalse($volume->exists($this->getTestDir() . 'some' . DIRECTORY_SEPARATOR . 'hint.none')); |
|
96
|
|
|
$this->assertTrue($volume->save($this->getTestDir() . 'some' . DIRECTORY_SEPARATOR . 'hint.none', 'asdfghjklpoiuztrewqyxcvbnm')); |
|
97
|
|
|
$this->assertTrue($volume->rmDir($this->getTestDir() . 'some', true)); |
|
98
|
|
|
$this->assertFalse($volume->isDir($this->getTestDir() . 'some')); |
|
99
|
|
|
$this->assertFalse($volume->exists($this->getTestDir() . 'some')); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @throws StorageException |
|
104
|
|
|
*/ |
|
105
|
|
|
public function testLookup(): void |
|
106
|
|
|
{ |
|
107
|
|
|
$volume = new Target\Volume(); |
|
108
|
|
|
$this->assertTrue($volume->check($this->getTestDir())); |
|
109
|
|
|
$testFiles = [ |
|
110
|
|
|
'dummyFile.tst' => $this->getTestDir() . 'dummyFile.tst', |
|
111
|
|
|
'dummyFile.0.tst' => $this->getTestDir() . 'dummyFile.0.tst', |
|
112
|
|
|
'dummyFile.1.tst' => $this->getTestDir() . 'dummyFile.1.tst', |
|
113
|
|
|
'dummyFile.2.tst' => $this->getTestDir() . 'dummyFile.2.tst', |
|
114
|
|
|
]; |
|
115
|
|
|
$removal = $volume->removeMulti($testFiles); |
|
116
|
|
|
$this->assertEquals([ |
|
117
|
|
|
'dummyFile.tst' => false, |
|
118
|
|
|
'dummyFile.0.tst' => false, |
|
119
|
|
|
'dummyFile.1.tst' => false, |
|
120
|
|
|
'dummyFile.2.tst' => false, |
|
121
|
|
|
], $removal); |
|
122
|
|
|
|
|
123
|
|
|
iterator_to_array($volume->lookup('this path does not exists')); |
|
124
|
|
|
$this->assertEquals(1, count(array_filter(iterator_to_array($volume->lookup($this->getTestDir())), [$this, 'dotDirs']))); |
|
125
|
|
|
|
|
126
|
|
|
file_put_contents($this->getTestDir() . 'dummyFile.tst', 'asdfghjklqwertzuiopyxcvbnm'); |
|
127
|
|
|
file_put_contents($this->getTestDir() . 'dummyFile.0.tst', 'asdfghjklqwertzuiopyxcvbnm'); |
|
128
|
|
|
file_put_contents($this->getTestDir() . 'dummyFile.1.tst', 'asdfghjklqwertzuiopyxcvbnm'); |
|
129
|
|
|
file_put_contents($this->getTestDir() . 'dummyFile.2.tst', 'asdfghjklqwertzuiopyxcvbnm'); |
|
130
|
|
|
|
|
131
|
|
|
$files = iterator_to_array($volume->lookup($this->getTestDir())); |
|
132
|
|
|
sort($files); |
|
133
|
|
|
$files = array_filter($files, [$this, 'dotDirs']); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertEquals(count($testFiles) + 1, count($files)); // because gitkeep |
|
136
|
|
|
$this->assertEquals('.gitkeep', reset($files)); |
|
137
|
|
|
$this->assertEquals('dummyFile.0.tst', next($files)); |
|
138
|
|
|
$this->assertEquals('dummyFile.1.tst', next($files)); |
|
139
|
|
|
$this->assertEquals('dummyFile.2.tst', next($files)); |
|
140
|
|
|
$this->assertEquals('dummyFile.tst', next($files)); |
|
141
|
|
|
|
|
142
|
|
|
$removal = $volume->removeMulti($testFiles); |
|
143
|
|
|
$this->assertFalse($volume->exists($this->getTestDir() . 'dummyFile.tst')); |
|
144
|
|
|
$this->assertFalse($volume->exists($this->getTestDir() . 'dummyFile.0.tst')); |
|
145
|
|
|
$this->assertFalse($volume->exists($this->getTestDir() . 'dummyFile.1.tst')); |
|
146
|
|
|
$this->assertFalse($volume->exists($this->getTestDir() . 'dummyFile.2.tst')); |
|
147
|
|
|
|
|
148
|
|
|
$this->assertEquals([ |
|
149
|
|
|
'dummyFile.tst' => true, |
|
150
|
|
|
'dummyFile.0.tst' => true, |
|
151
|
|
|
'dummyFile.1.tst' => true, |
|
152
|
|
|
'dummyFile.2.tst' => true, |
|
153
|
|
|
], $removal); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function dotDirs(string $path): bool |
|
157
|
|
|
{ |
|
158
|
|
|
return !in_array($path, ['.', '..']); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @throws StorageException |
|
163
|
|
|
*/ |
|
164
|
|
|
public function testSimpleCounter(): void |
|
165
|
|
|
{ |
|
166
|
|
|
$volume = new Target\Volume(); |
|
167
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile())); |
|
168
|
|
|
$this->assertTrue($volume->save($this->mockTestFile(), 15)); |
|
169
|
|
|
$this->assertTrue($volume->decrement($this->mockTestFile())); |
|
170
|
|
|
$this->assertTrue($volume->decrement($this->mockTestFile())); |
|
171
|
|
|
$this->assertTrue($volume->increment($this->mockTestFile())); |
|
172
|
|
|
$this->assertEquals(14, $volume->load($this->mockTestFile())); |
|
173
|
|
|
$this->assertTrue($volume->remove($this->mockTestFile())); |
|
174
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile())); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @throws StorageException |
|
179
|
|
|
*/ |
|
180
|
|
|
public function testHarderCounter(): void |
|
181
|
|
|
{ |
|
182
|
|
|
$volume = new Target\Volume(); |
|
183
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile())); |
|
184
|
|
|
$this->assertTrue($volume->decrement($this->mockTestFile())); |
|
185
|
|
|
$this->assertTrue($volume->increment($this->mockTestFile())); |
|
186
|
|
|
$this->assertEquals(1, $volume->load($this->mockTestFile())); |
|
187
|
|
|
$this->assertTrue($volume->remove($this->mockTestFile())); |
|
188
|
|
|
$this->assertTrue($volume->increment($this->mockTestFile())); |
|
189
|
|
|
$this->assertTrue($volume->decrement($this->mockTestFile())); |
|
190
|
|
|
$this->assertEquals(0, $volume->load($this->mockTestFile())); |
|
191
|
|
|
$this->assertTrue($volume->remove($this->mockTestFile())); |
|
192
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile())); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|