1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BasicTests; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use CommonTestClass; |
7
|
|
|
use kalanis\kw_storage\Helper; |
8
|
|
|
use kalanis\kw_storage\Storage; |
9
|
|
|
use kalanis\kw_storage\StorageException; |
10
|
|
|
use kalanis\kw_storage\Translations; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class StorageTest extends CommonTestClass |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @throws StorageException |
17
|
|
|
*/ |
18
|
|
|
public function testStorageUninitialized(): void |
19
|
|
|
{ |
20
|
|
|
$storage = new Storage($this->getStorageFactory()); |
21
|
|
|
$this->expectException(StorageException::class); |
22
|
|
|
$storage->increment('abc'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @throws StorageException |
27
|
|
|
*/ |
28
|
|
|
public function testStorageInitialized1(): void |
29
|
|
|
{ |
30
|
|
|
Helper::initStorage(); |
31
|
|
|
$volume = $this->getStorageVolume1(); |
32
|
|
|
$this->assertTrue($volume->isConnected()); |
33
|
|
|
$this->assertFalse($volume->exists('utz')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @throws StorageException |
38
|
|
|
*/ |
39
|
|
|
public function testStorageInitialized2(): void |
40
|
|
|
{ |
41
|
|
|
$volume = $this->getStorageVolume2(); |
42
|
|
|
$this->assertTrue($volume->isConnected()); |
43
|
|
|
$this->assertFalse($volume->exists('utz')); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @throws StorageException |
48
|
|
|
*/ |
49
|
|
|
public function testOperations(): void |
50
|
|
|
{ |
51
|
|
|
$volume = $this->getStorageVolume1(); |
52
|
|
|
$this->assertTrue($volume->set('abv', 'sdfhgdfh', null)); // must be empty timeout - enables in result - hack |
53
|
|
|
$this->assertTrue($volume->add('abv', 'dummy mock', null)); // must be same as in mock and have no timer - hack |
54
|
|
|
$this->assertEquals('dummy mock', $volume->get('abv')); |
55
|
|
|
$this->assertFalse($volume->add('abv', 'unknown', 354)); // must be different from mock |
56
|
|
|
$this->assertFalse($volume->delete('abv')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @throws StorageException |
61
|
|
|
*/ |
62
|
|
|
public function testLookup(): void |
63
|
|
|
{ |
64
|
|
|
$volume = $this->getStorageVolume1(); |
65
|
|
|
$this->assertEmpty(iterator_to_array($volume->getAllKeys())); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @throws StorageException |
70
|
|
|
*/ |
71
|
|
|
public function testVolumeFileCounter(): void |
72
|
|
|
{ |
73
|
|
|
$volume = $this->getStorageVolume1(); |
74
|
|
|
$this->assertTrue($volume->increment($this->mockTestFile())); |
75
|
|
|
$this->assertFalse($volume->decrement($this->mockTestFile())); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @throws StorageException |
80
|
|
|
*/ |
81
|
|
|
public function testVolumeFileHarderCounter(): void |
82
|
|
|
{ |
83
|
|
|
$volume = $this->getStorageVolume1(); |
84
|
|
|
$this->assertEmpty($volume->deleteMulti(['dummyFile.tst'])); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @throws StorageException |
89
|
|
|
* @return Storage |
90
|
|
|
* For testing purposes that factory returns only one possible class |
91
|
|
|
*/ |
92
|
|
|
protected function getStorageVolume1(): Storage |
93
|
|
|
{ |
94
|
|
|
$storage = new Storage($this->getStorageFactory()); |
95
|
|
|
$storage->init(null); |
96
|
|
|
return $storage; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @throws StorageException |
101
|
|
|
* @return Storage |
102
|
|
|
* For testing purposes that factory returns only one possible class |
103
|
|
|
*/ |
104
|
|
|
protected function getStorageVolume2(): Storage |
105
|
|
|
{ |
106
|
|
|
$storage = new Storage($this->getStorageFactory(), new Translations()); |
107
|
|
|
$storage->init(['something' => 'somewhere']); |
108
|
|
|
return $storage; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function getStorageFactory(): Storage\Factory |
112
|
|
|
{ |
113
|
|
|
return new Storage\Factory(new \MockKeyFactory(), new \MockTargetFactory()); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|