|
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
|
|
|
if (is_file($this->mockTestFile())) { |
|
16
|
|
|
unlink($this->mockTestFile()); |
|
17
|
|
|
} |
|
18
|
|
|
if (is_dir($this->mockTestFile())) { |
|
19
|
|
|
rmdir($this->mockTestFile()); |
|
20
|
|
|
} |
|
21
|
|
|
parent::tearDown(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testDir(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$volume = new Target\Volume(); |
|
27
|
|
|
|
|
28
|
|
|
// test dir |
|
29
|
|
|
$testDir = $this->getTestDir(); |
|
30
|
|
|
$this->assertTrue($volume->isDir($testDir)); |
|
31
|
|
|
$mockPath = substr($testDir, 0, strrpos($testDir, DIRECTORY_SEPARATOR)) . 'dummy'; |
|
32
|
|
|
if (is_dir($mockPath)) { |
|
33
|
|
|
rmdir($mockPath); |
|
34
|
|
|
} |
|
35
|
|
|
file_put_contents($mockPath, 'just leave it there'); |
|
36
|
|
|
$this->assertTrue($volume->check($mockPath . DIRECTORY_SEPARATOR)); |
|
37
|
|
|
rmdir($mockPath); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @throws StorageException |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testExists(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$volume = new Target\Volume(); |
|
46
|
|
|
$this->assertTrue($volume->check($this->getTestDir())); |
|
47
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile())); |
|
48
|
|
|
$this->assertFalse($volume->isDir($this->mockTestFile())); |
|
49
|
|
|
$this->expectException(StorageException::class); |
|
50
|
|
|
$volume->load($this->mockTestFile()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @throws StorageException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function testOperations(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$volume = new Target\Volume(); |
|
59
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile())); |
|
60
|
|
|
$this->assertFalse($volume->isDir($this->mockTestFile())); |
|
61
|
|
|
$this->assertTrue($volume->save($this->mockTestFile(), 'asdfghjklpoiuztrewqyxcvbnm')); |
|
62
|
|
|
$this->assertTrue($volume->exists($this->mockTestFile())); |
|
63
|
|
|
$this->assertFalse($volume->isDir($this->mockTestFile())); |
|
64
|
|
|
$this->assertEquals('asdfghjklpoiuztrewqyxcvbnm', $volume->load($this->mockTestFile())); |
|
65
|
|
|
$this->assertTrue($volume->remove($this->mockTestFile())); |
|
66
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile())); |
|
67
|
|
|
$this->assertFalse($volume->isDir($this->mockTestFile())); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @throws StorageException |
|
72
|
|
|
*/ |
|
73
|
|
|
public function testLookup(): void |
|
74
|
|
|
{ |
|
75
|
|
|
$volume = new Target\Volume(); |
|
76
|
|
|
$this->assertTrue($volume->check($this->getTestDir())); |
|
77
|
|
|
$testFiles = [ |
|
78
|
|
|
'dummyFile.tst' => $this->getTestDir() . 'dummyFile.tst', |
|
79
|
|
|
'dummyFile.0.tst' => $this->getTestDir() . 'dummyFile.0.tst', |
|
80
|
|
|
'dummyFile.1.tst' => $this->getTestDir() . 'dummyFile.1.tst', |
|
81
|
|
|
'dummyFile.2.tst' => $this->getTestDir() . 'dummyFile.2.tst', |
|
82
|
|
|
]; |
|
83
|
|
|
$removal = $volume->removeMulti($testFiles); |
|
84
|
|
|
$this->assertEquals([ |
|
85
|
|
|
'dummyFile.tst' => false, |
|
86
|
|
|
'dummyFile.0.tst' => false, |
|
87
|
|
|
'dummyFile.1.tst' => false, |
|
88
|
|
|
'dummyFile.2.tst' => false, |
|
89
|
|
|
], $removal); |
|
90
|
|
|
|
|
91
|
|
|
iterator_to_array($volume->lookup('this path does not exists')); |
|
92
|
|
|
$this->assertEquals(0, count(array_filter(iterator_to_array($volume->lookup($this->getTestDir())), [$this, 'dotDirs']))); |
|
93
|
|
|
|
|
94
|
|
|
file_put_contents($this->getTestDir() . 'dummyFile.tst', 'asdfghjklqwertzuiopyxcvbnm'); |
|
95
|
|
|
file_put_contents($this->getTestDir() . 'dummyFile.0.tst', 'asdfghjklqwertzuiopyxcvbnm'); |
|
96
|
|
|
file_put_contents($this->getTestDir() . 'dummyFile.1.tst', 'asdfghjklqwertzuiopyxcvbnm'); |
|
97
|
|
|
file_put_contents($this->getTestDir() . 'dummyFile.2.tst', 'asdfghjklqwertzuiopyxcvbnm'); |
|
98
|
|
|
|
|
99
|
|
|
$files = iterator_to_array($volume->lookup($this->getTestDir())); |
|
100
|
|
|
sort($files); |
|
101
|
|
|
$files = array_filter($files, [$this, 'dotDirs']); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertEquals(count($testFiles), count($files)); |
|
104
|
|
|
$this->assertEquals('dummyFile.0.tst', reset($files)); |
|
105
|
|
|
$this->assertEquals('dummyFile.1.tst', next($files)); |
|
106
|
|
|
$this->assertEquals('dummyFile.2.tst', next($files)); |
|
107
|
|
|
$this->assertEquals('dummyFile.tst', next($files)); |
|
108
|
|
|
|
|
109
|
|
|
$removal = $volume->removeMulti($testFiles); |
|
110
|
|
|
$this->assertFalse($volume->exists($this->getTestDir() . 'dummyFile.tst')); |
|
111
|
|
|
$this->assertFalse($volume->exists($this->getTestDir() . 'dummyFile.0.tst')); |
|
112
|
|
|
$this->assertFalse($volume->exists($this->getTestDir() . 'dummyFile.1.tst')); |
|
113
|
|
|
$this->assertFalse($volume->exists($this->getTestDir() . 'dummyFile.2.tst')); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertEquals([ |
|
116
|
|
|
'dummyFile.tst' => true, |
|
117
|
|
|
'dummyFile.0.tst' => true, |
|
118
|
|
|
'dummyFile.1.tst' => true, |
|
119
|
|
|
'dummyFile.2.tst' => true, |
|
120
|
|
|
], $removal); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function dotDirs(string $path): bool |
|
124
|
|
|
{ |
|
125
|
|
|
return !in_array($path, ['.', '..']); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @throws StorageException |
|
130
|
|
|
*/ |
|
131
|
|
|
public function testSimpleCounter(): void |
|
132
|
|
|
{ |
|
133
|
|
|
$volume = new Target\Volume(); |
|
134
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile())); |
|
135
|
|
|
$this->assertTrue($volume->save($this->mockTestFile(), 15)); |
|
136
|
|
|
$this->assertTrue($volume->decrement($this->mockTestFile())); |
|
137
|
|
|
$this->assertTrue($volume->decrement($this->mockTestFile())); |
|
138
|
|
|
$this->assertTrue($volume->increment($this->mockTestFile())); |
|
139
|
|
|
$this->assertEquals(14, $volume->load($this->mockTestFile())); |
|
140
|
|
|
$this->assertTrue($volume->remove($this->mockTestFile())); |
|
141
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile())); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @throws StorageException |
|
146
|
|
|
*/ |
|
147
|
|
|
public function testHarderCounter(): void |
|
148
|
|
|
{ |
|
149
|
|
|
$volume = new Target\Volume(); |
|
150
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile())); |
|
151
|
|
|
$this->assertTrue($volume->decrement($this->mockTestFile())); |
|
152
|
|
|
$this->assertTrue($volume->increment($this->mockTestFile())); |
|
153
|
|
|
$this->assertEquals(1, $volume->load($this->mockTestFile())); |
|
154
|
|
|
$this->assertTrue($volume->remove($this->mockTestFile())); |
|
155
|
|
|
$this->assertTrue($volume->increment($this->mockTestFile())); |
|
156
|
|
|
$this->assertTrue($volume->decrement($this->mockTestFile())); |
|
157
|
|
|
$this->assertEquals(0, $volume->load($this->mockTestFile())); |
|
158
|
|
|
$this->assertTrue($volume->remove($this->mockTestFile())); |
|
159
|
|
|
$this->assertFalse($volume->exists($this->mockTestFile())); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|