|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Cache\Storage; |
|
4
|
|
|
|
|
5
|
|
|
use Ds\Cache\CacheException; |
|
6
|
|
|
use Ds\Cache\InvalidArgumentException; |
|
7
|
|
|
use Ds\Cache\Storage\FileStorage; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class FileStorageTest |
|
11
|
|
|
* |
|
12
|
|
|
* @package Tests\Cache\Storage |
|
13
|
|
|
*/ |
|
14
|
|
|
class FileStorageTest extends \PHPUnit\Framework\TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var FileStorage |
|
19
|
|
|
*/ |
|
20
|
|
|
public $cacheStorage; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var |
|
24
|
|
|
*/ |
|
25
|
|
|
public $testDir; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
|
public function setUp() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->testDir = __DIR__ . '/cacheTest'; |
|
33
|
|
|
$this->cacheStorage = new FileStorage( $this->testDir, new \DateInterval('P1M')); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Test that InvalidArgumentException is thrown when not using a valid string |
|
38
|
|
|
*/ |
|
39
|
|
|
public function testNonStringDirectory() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
42
|
|
|
$this->cacheStorage = new FileStorage(false, new \DateInterval('P1M')); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Test that CacheException is thrown if directory is not valid/writable. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testUnwritableDirectory(){ |
|
49
|
|
|
|
|
50
|
|
|
$this->expectException(CacheException::class); |
|
51
|
|
|
$this->cacheStorage = new FileStorage('php://memory', new \DateInterval('P1M')); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Test that set is called. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function testSet(){ |
|
58
|
|
|
$actual = $this->cacheStorage->set('key','value',60*60); |
|
|
|
|
|
|
59
|
|
|
$this->assertEquals(true, $actual); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Test has when not value is found. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function testHasNoValue(){ |
|
66
|
|
|
$this->assertEquals($this->cacheStorage->has('someRandomKey'), false); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Test has when value is found. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testHas(){ |
|
73
|
|
|
$this->cacheStorage->set('foo','bar'); |
|
|
|
|
|
|
74
|
|
|
$this->assertEquals($this->cacheStorage->has('foo'), true); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Test has when value is found but has expired. |
|
79
|
|
|
*/ |
|
80
|
|
|
public function testHasExpired(){ |
|
81
|
|
|
$this->cacheStorage->set('expired','bar', -1200); |
|
|
|
|
|
|
82
|
|
|
$this->assertEquals($this->cacheStorage->has('expired'), false); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Test that clear is called. |
|
87
|
|
|
*/ |
|
88
|
|
|
public function testClear(){ |
|
89
|
|
|
$this->assertEquals($this->cacheStorage->clear(), true); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Test that get returns a found key. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function testGet(){ |
|
96
|
|
|
$expected = 'bat'; |
|
97
|
|
|
$this->cacheStorage->set('baz',$expected); |
|
|
|
|
|
|
98
|
|
|
$actual = $this->cacheStorage->get('baz'); |
|
99
|
|
|
$this->assertEquals($expected, $actual); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testDelete(){ |
|
103
|
|
|
$this->cacheStorage->delete('baz'); |
|
|
|
|
|
|
104
|
|
|
$this->assertEquals($this->cacheStorage->has('baz'), false); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testCreateCacheItem(){ |
|
108
|
|
|
|
|
109
|
|
|
$reflector = new \ReflectionClass(FileStorage::class); |
|
110
|
|
|
$method = $reflector->getMethod('_createCacheItem'); |
|
111
|
|
|
$method->setAccessible(true); |
|
112
|
|
|
|
|
113
|
|
|
$key= 'my-key_+'; |
|
114
|
|
|
$value = 'my-value'; |
|
115
|
|
|
$ttl = 60 * 60 * 7; |
|
116
|
|
|
|
|
117
|
|
|
$expected = [ |
|
118
|
|
|
'file' => $this->testDir . DIRECTORY_SEPARATOR . implode('.',[$key, FileStorage::EXTENSION]), |
|
119
|
|
|
'data' => json_encode([$ttl, $value]) |
|
120
|
|
|
]; |
|
121
|
|
|
|
|
122
|
|
|
$actual = $method->invokeArgs($this->cacheStorage, [$key, $value, $ttl]); |
|
123
|
|
|
$this->assertEquals($expected, $actual); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function testCreateCacheItemInvalidKey(){ |
|
127
|
|
|
|
|
128
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
129
|
|
|
$reflector = new \ReflectionClass(FileStorage::class); |
|
130
|
|
|
$method = $reflector->getMethod('_createCacheItem'); |
|
131
|
|
|
$method->setAccessible(true); |
|
132
|
|
|
$key = '\jlasd$dll.'; |
|
133
|
|
|
$value = 'my-value'; |
|
134
|
|
|
$ttl = 60 * 60 * 7; |
|
135
|
|
|
$method->invokeArgs($this->cacheStorage, [$key, $value, $ttl]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function testFetchCacheNoItem(){ |
|
139
|
|
|
$reflector = new \ReflectionClass(FileStorage::class); |
|
140
|
|
|
$method = $reflector->getMethod('_fetchCacheFile'); |
|
141
|
|
|
$method->setAccessible(true); |
|
142
|
|
|
$key= 'unknown'; |
|
143
|
|
|
$actual = $method->invokeArgs($this->cacheStorage, [$key]); |
|
144
|
|
|
$this->assertEquals(false, $actual); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function testFetchCacheValid(){ |
|
148
|
|
|
|
|
149
|
|
|
$key= 'my-item'; |
|
150
|
|
|
$ttl = 60 * 60; |
|
151
|
|
|
$value = 'some-value'; |
|
152
|
|
|
|
|
153
|
|
|
$this->cacheStorage->set($key, $value, $ttl); |
|
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
$expected = [ |
|
156
|
|
|
0 => time() + $ttl, |
|
157
|
|
|
1 => $value |
|
158
|
|
|
]; |
|
159
|
|
|
|
|
160
|
|
|
$reflector = new \ReflectionClass(FileStorage::class); |
|
161
|
|
|
$method = $reflector->getMethod('_fetchCacheFile'); |
|
162
|
|
|
$method->setAccessible(true); |
|
163
|
|
|
$actual = $method->invokeArgs($this->cacheStorage, [$key]); |
|
164
|
|
|
$this->assertEquals($expected, $actual); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function testFetchCacheExpired(){ |
|
168
|
|
|
$key= 'my-item'; |
|
169
|
|
|
$ttl = -1500; |
|
170
|
|
|
$value = 'some-value'; |
|
171
|
|
|
$this->cacheStorage->set($key, $value, $ttl); |
|
|
|
|
|
|
172
|
|
|
$reflector = new \ReflectionClass(FileStorage::class); |
|
173
|
|
|
$method = $reflector->getMethod('_fetchCacheFile'); |
|
174
|
|
|
$method->setAccessible(true); |
|
175
|
|
|
$actual = $method->invokeArgs($this->cacheStorage, [$key]); |
|
176
|
|
|
$this->assertEquals(false, $actual); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Clean up any cache files left on system. |
|
181
|
|
|
*/ |
|
182
|
|
|
public function tearDown() |
|
183
|
|
|
{ |
|
184
|
|
|
$dir = $this->testDir; |
|
185
|
|
|
|
|
186
|
|
|
if (is_dir( $dir)){ |
|
187
|
|
|
array_map('unlink', glob("$dir/*.*")); |
|
188
|
|
|
} |
|
189
|
|
|
\rmdir($dir); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: