1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DoctrineModuleTest\Cache; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
8
|
|
|
use DoctrineModule\Cache\DoctrineCacheStorage; |
9
|
|
|
use Laminas\Cache\Storage\Adapter\AdapterOptions; |
10
|
|
|
use Laminas\Cache\Storage\StorageInterface; |
11
|
|
|
use Laminas\Stdlib\ErrorHandler; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use stdClass; |
14
|
|
|
use function array_keys; |
15
|
|
|
use function count; |
16
|
|
|
use function fopen; |
17
|
|
|
use function is_string; |
18
|
|
|
use function ksort; |
19
|
|
|
use function method_exists; |
20
|
|
|
use function rand; |
21
|
|
|
use function settype; |
22
|
|
|
use function sort; |
23
|
|
|
use function sprintf; |
24
|
|
|
use function str_replace; |
25
|
|
|
use function ucwords; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Tests for the cache bridge |
29
|
|
|
* |
30
|
|
|
* @link http://www.doctrine-project.org/ |
31
|
|
|
* |
32
|
|
|
* @todo extend \ZendTest\Cache\Storage\CommonAdapterTest instead |
33
|
|
|
* @covers \DoctrineModule\Cache\DoctrineCacheStorage |
34
|
|
|
*/ |
35
|
|
|
class DoctrineCacheStorageTest extends TestCase |
36
|
|
|
{ |
37
|
|
|
/** @var AdapterOptions */ |
38
|
|
|
protected $options; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The storage adapter |
42
|
|
|
* |
43
|
|
|
* @var StorageInterface |
44
|
|
|
*/ |
45
|
|
|
protected $storage; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* All datatypes of PHP |
49
|
|
|
* |
50
|
|
|
* @var string[] |
51
|
|
|
*/ |
52
|
|
|
protected $phpDatatypes = ['NULL', 'boolean', 'integer', 'double', 'string', 'array', 'object', 'resource']; |
53
|
|
|
|
54
|
|
|
protected function setUp() : void |
55
|
|
|
{ |
56
|
|
|
$this->options = new AdapterOptions(); |
57
|
|
|
// @todo fix constructor as it is messy |
58
|
|
|
$this->storage = new DoctrineCacheStorage($this->options, new ArrayCache()); |
59
|
|
|
|
60
|
|
|
$this->assertInstanceOf( |
|
|
|
|
61
|
|
|
'Laminas\Cache\Storage\StorageInterface', |
62
|
|
|
$this->storage, |
63
|
|
|
'Storage adapter instance is needed for tests' |
64
|
|
|
); |
65
|
|
|
$this->assertInstanceOf( |
|
|
|
|
66
|
|
|
'Laminas\Cache\Storage\Adapter\AdapterOptions', |
67
|
|
|
$this->options, |
68
|
|
|
'Options instance is needed for tests' |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function tearDown() : void |
73
|
|
|
{ |
74
|
|
|
// be sure the error handler has been stopped |
75
|
|
|
if (! ErrorHandler::started()) { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
ErrorHandler::stop(); |
80
|
|
|
$this->fail('ErrorHandler not stopped'); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testOptionNamesValid() : void |
84
|
|
|
{ |
85
|
|
|
$options = $this->storage->getOptions()->toArray(); |
86
|
|
|
foreach ($options as $name => $value) { |
87
|
|
|
$this->assertRegExp( |
|
|
|
|
88
|
|
|
'/^[a-z]+[a-z0-9_]*[a-z0-9]+$/', |
89
|
|
|
$name, |
90
|
|
|
sprintf( |
91
|
|
|
"Invalid option name '%s'", |
92
|
|
|
$name |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testGettersAndSettersOfOptionsExists() : void |
99
|
|
|
{ |
100
|
|
|
$options = $this->storage->getOptions(); |
101
|
|
|
foreach ($options->toArray() as $option => $value) { |
102
|
|
|
if ($option === 'adapter') { |
103
|
|
|
// Skip this, as it's a "special" value |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$method = ucwords(str_replace('_', ' ', $option)); |
108
|
|
|
$method = str_replace(' ', '', $method); |
109
|
|
|
|
110
|
|
|
$this->assertTrue( |
|
|
|
|
111
|
|
|
method_exists($options, 'set' . $method), |
112
|
|
|
sprintf( |
113
|
|
|
"Missing method 'set'%s", |
114
|
|
|
$method |
115
|
|
|
) |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$this->assertTrue( |
|
|
|
|
119
|
|
|
method_exists($options, 'get' . $method), |
120
|
|
|
sprintf( |
121
|
|
|
"Missing method 'get'%s", |
122
|
|
|
$method |
123
|
|
|
) |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testOptionsGetAndSetDefault() : void |
129
|
|
|
{ |
130
|
|
|
$options = $this->storage->getOptions(); |
131
|
|
|
$this->storage->setOptions($options); |
132
|
|
|
$this->assertSame($options, $this->storage->getOptions()); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testOptionsFluentInterface() : void |
136
|
|
|
{ |
137
|
|
|
$options = $this->storage->getOptions(); |
138
|
|
|
foreach ($options->toArray() as $option => $value) { |
139
|
|
|
$method = ucwords(str_replace('_', ' ', $option)); |
140
|
|
|
$method = 'set' . str_replace(' ', '', $method); |
141
|
|
|
$this->assertSame( |
|
|
|
|
142
|
|
|
$options, |
143
|
|
|
$options->{$method}($value), |
144
|
|
|
sprintf( |
145
|
|
|
"Method '%s' doesn't implement the fluent interface", |
146
|
|
|
$method |
147
|
|
|
) |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$this->assertSame( |
|
|
|
|
152
|
|
|
$this->storage, |
153
|
|
|
$this->storage->setOptions($options), |
154
|
|
|
"Method 'setOptions' doesn't implement the fluent interface" |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testGetCapabilities() : void |
159
|
|
|
{ |
160
|
|
|
$capabilities = $this->storage->getCapabilities(); |
161
|
|
|
$this->assertInstanceOf('Laminas\Cache\Storage\Capabilities', $capabilities); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testDatatypesCapability() : void |
165
|
|
|
{ |
166
|
|
|
$capabilities = $this->storage->getCapabilities(); |
167
|
|
|
$datatypes = $capabilities->getSupportedDatatypes(); |
168
|
|
|
$this->assertIsArray($datatypes); |
|
|
|
|
169
|
|
|
|
170
|
|
|
foreach ($datatypes as $sourceType => $targetType) { |
171
|
|
|
$this->assertContains($sourceType, $this->phpDatatypes, 'Unknown source type "' . $sourceType . '"'); |
|
|
|
|
172
|
|
|
|
173
|
|
|
if (is_string($targetType)) { |
174
|
|
|
$this->assertContains($targetType, $this->phpDatatypes, 'Unknown source type "' . $sourceType . '"'); |
|
|
|
|
175
|
|
|
} else { |
176
|
|
|
$this->assertIsBool($targetType, 'Target type must be a string or boolean'); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testSupportedMetadataCapability() : void |
182
|
|
|
{ |
183
|
|
|
$capabilities = $this->storage->getCapabilities(); |
184
|
|
|
$metadata = $capabilities->getSupportedMetadata(); |
185
|
|
|
$this->assertIsArray($metadata); |
|
|
|
|
186
|
|
|
|
187
|
|
|
foreach ($metadata as $property) { |
188
|
|
|
$this->assertIsString($property); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function testTtlCapabilities() : void |
193
|
|
|
{ |
194
|
|
|
$capabilities = $this->storage->getCapabilities(); |
195
|
|
|
|
196
|
|
|
$this->assertIsInt($capabilities->getMaxTtl()); |
|
|
|
|
197
|
|
|
$this->assertGreaterThanOrEqual(0, $capabilities->getMaxTtl()); |
|
|
|
|
198
|
|
|
|
199
|
|
|
$this->assertIsBool($capabilities->getStaticTtl()); |
|
|
|
|
200
|
|
|
|
201
|
|
|
$this->assertIsNumeric($capabilities->getTtlPrecision()); |
|
|
|
|
202
|
|
|
$this->assertGreaterThan(0, $capabilities->getTtlPrecision()); |
|
|
|
|
203
|
|
|
|
204
|
|
|
$this->assertIsBool($capabilities->getStaticTtl()); |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testKeyCapabilities() : void |
208
|
|
|
{ |
209
|
|
|
$capabilities = $this->storage->getCapabilities(); |
210
|
|
|
|
211
|
|
|
$this->assertIsInt($capabilities->getMaxKeyLength()); |
|
|
|
|
212
|
|
|
$this->assertGreaterThanOrEqual(-1, $capabilities->getMaxKeyLength()); |
|
|
|
|
213
|
|
|
|
214
|
|
|
$this->assertIsBool($capabilities->getNamespaceIsPrefix()); |
|
|
|
|
215
|
|
|
|
216
|
|
|
$this->assertIsString($capabilities->getNamespaceSeparator()); |
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function testHasItemReturnsTrueOnValidItem() : void |
220
|
|
|
{ |
221
|
|
|
$this->assertTrue($this->storage->setItem('key', 'value')); |
|
|
|
|
222
|
|
|
$this->assertTrue($this->storage->hasItem('key')); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testHasItemReturnsFalseOnMissingItem() : void |
226
|
|
|
{ |
227
|
|
|
$this->assertFalse($this->storage->hasItem('key')); |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function testHasItemNonReadable() : void |
231
|
|
|
{ |
232
|
|
|
$this->assertTrue($this->storage->setItem('key', 'value')); |
|
|
|
|
233
|
|
|
|
234
|
|
|
$this->options->setReadable(false); |
235
|
|
|
$this->assertFalse($this->storage->hasItem('key')); |
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function testHasItemsReturnsKeysOfFoundItems() : void |
239
|
|
|
{ |
240
|
|
|
$this->assertTrue($this->storage->setItem('key1', 'value1')); |
|
|
|
|
241
|
|
|
$this->assertTrue($this->storage->setItem('key2', 'value2')); |
|
|
|
|
242
|
|
|
|
243
|
|
|
$result = $this->storage->hasItems(['missing', 'key1', 'key2']); |
244
|
|
|
sort($result); |
245
|
|
|
|
246
|
|
|
$expectedResult = ['key1', 'key2']; |
247
|
|
|
$this->assertEquals($expectedResult, $result); |
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function testHasItemsReturnsEmptyArrayIfNonReadable() : void |
251
|
|
|
{ |
252
|
|
|
$this->assertTrue($this->storage->setItem('key', 'value')); |
|
|
|
|
253
|
|
|
|
254
|
|
|
$this->options->setReadable(false); |
255
|
|
|
$this->assertEquals([], $this->storage->hasItems(['key'])); |
|
|
|
|
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testGetItemReturnsNullOnMissingItem() : void |
259
|
|
|
{ |
260
|
|
|
$this->assertNull($this->storage->getItem('unknown')); |
|
|
|
|
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function testGetItemSetsSuccessFlag() : void |
264
|
|
|
{ |
265
|
|
|
$success = null; |
266
|
|
|
|
267
|
|
|
// $success = false on get missing item |
268
|
|
|
$this->storage->getItem('unknown', $success); |
269
|
|
|
$this->assertFalse($success); |
|
|
|
|
270
|
|
|
|
271
|
|
|
// $success = true on get valid item |
272
|
|
|
$this->storage->setItem('test', 'test'); |
273
|
|
|
$this->storage->getItem('test', $success); |
274
|
|
|
$this->assertTrue($success); |
|
|
|
|
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function testGetItemReturnsNullIfNonReadable() : void |
278
|
|
|
{ |
279
|
|
|
$this->options->setReadable(false); |
280
|
|
|
|
281
|
|
|
$this->assertTrue($this->storage->setItem('key', 'value')); |
|
|
|
|
282
|
|
|
$this->assertNull($this->storage->getItem('key')); |
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function testGetItemsReturnsKeyValuePairsOfFoundItems() : void |
286
|
|
|
{ |
287
|
|
|
$this->assertTrue($this->storage->setItem('key1', 'value1')); |
|
|
|
|
288
|
|
|
$this->assertTrue($this->storage->setItem('key2', 'value2')); |
|
|
|
|
289
|
|
|
|
290
|
|
|
$result = $this->storage->getItems(['missing', 'key1', 'key2']); |
291
|
|
|
ksort($result); |
292
|
|
|
|
293
|
|
|
$expectedResult = [ |
294
|
|
|
'key1' => 'value1', |
295
|
|
|
'key2' => 'value2', |
296
|
|
|
]; |
297
|
|
|
$this->assertEquals($expectedResult, $result); |
|
|
|
|
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
public function testGetItemsReturnsEmptyArrayIfNonReadable() : void |
301
|
|
|
{ |
302
|
|
|
$this->options->setReadable(false); |
303
|
|
|
|
304
|
|
|
$this->assertTrue($this->storage->setItem('key', 'value')); |
|
|
|
|
305
|
|
|
$this->assertEquals([], $this->storage->getItems(['key'])); |
|
|
|
|
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function testGetMetadata() : void |
309
|
|
|
{ |
310
|
|
|
$capabilities = $this->storage->getCapabilities(); |
311
|
|
|
$supportedMetadatas = $capabilities->getSupportedMetadata(); |
312
|
|
|
|
313
|
|
|
$this->assertTrue($this->storage->setItem('key', 'value')); |
|
|
|
|
314
|
|
|
$metadata = $this->storage->getMetadata('key'); |
315
|
|
|
|
316
|
|
|
$this->assertIsArray($metadata); |
|
|
|
|
317
|
|
|
foreach ($supportedMetadatas as $supportedMetadata) { |
318
|
|
|
$this->assertArrayHasKey($supportedMetadata, $metadata); |
|
|
|
|
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function testGetMetadataReturnsFalseOnMissingItem() : void |
323
|
|
|
{ |
324
|
|
|
$this->assertFalse($this->storage->getMetadata('unknown')); |
|
|
|
|
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
public function testGetMetadataReturnsFalseIfNonReadable() : void |
328
|
|
|
{ |
329
|
|
|
$this->options->setReadable(false); |
330
|
|
|
|
331
|
|
|
$this->assertTrue($this->storage->setItem('key', 'value')); |
|
|
|
|
332
|
|
|
$this->assertFalse($this->storage->getMetadata('key')); |
|
|
|
|
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function testGetMetadatas() : void |
336
|
|
|
{ |
337
|
|
|
$capabilities = $this->storage->getCapabilities(); |
338
|
|
|
$supportedMetadatas = $capabilities->getSupportedMetadata(); |
339
|
|
|
|
340
|
|
|
$items = [ |
341
|
|
|
'key1' => 'value1', |
342
|
|
|
'key2' => 'value2', |
343
|
|
|
]; |
344
|
|
|
$this->assertSame([], $this->storage->setItems($items)); |
|
|
|
|
345
|
|
|
|
346
|
|
|
$metadatas = $this->storage->getMetadatas(array_keys($items)); |
347
|
|
|
$this->assertIsArray($metadatas); |
|
|
|
|
348
|
|
|
$this->assertSame(count($items), count($metadatas)); |
|
|
|
|
349
|
|
|
foreach ($metadatas as $metadata) { |
350
|
|
|
$this->assertIsArray($metadata); |
|
|
|
|
351
|
|
|
foreach ($supportedMetadatas as $supportedMetadata) { |
352
|
|
|
$this->assertArrayHasKey($supportedMetadata, $metadata); |
|
|
|
|
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
public function testGetMetadatasReturnsEmptyArrayIfNonReadable() : void |
358
|
|
|
{ |
359
|
|
|
$this->options->setReadable(false); |
360
|
|
|
|
361
|
|
|
$this->assertTrue($this->storage->setItem('key', 'value')); |
|
|
|
|
362
|
|
|
$this->assertEquals([], $this->storage->getMetadatas(['key'])); |
|
|
|
|
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
public function testSetGetHasAndRemoveItem() : void |
366
|
|
|
{ |
367
|
|
|
$this->assertTrue($this->storage->setItem('key', 'value')); |
|
|
|
|
368
|
|
|
$this->assertEquals('value', $this->storage->getItem('key')); |
|
|
|
|
369
|
|
|
$this->assertTrue($this->storage->hasItem('key')); |
|
|
|
|
370
|
|
|
|
371
|
|
|
$this->assertTrue($this->storage->removeItem('key')); |
|
|
|
|
372
|
|
|
$this->assertFalse($this->storage->hasItem('key')); |
|
|
|
|
373
|
|
|
$this->assertNull($this->storage->getItem('key')); |
|
|
|
|
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
public function testSetGetHasAndRemoveItems() : void |
377
|
|
|
{ |
378
|
|
|
$items = [ |
379
|
|
|
'key1' => 'value1', |
380
|
|
|
'key2' => 'value2', |
381
|
|
|
'key3' => 'value3', |
382
|
|
|
]; |
383
|
|
|
|
384
|
|
|
$this->assertSame([], $this->storage->setItems($items)); |
|
|
|
|
385
|
|
|
|
386
|
|
|
$rs = $this->storage->getItems(array_keys($items)); |
387
|
|
|
$this->assertIsArray($rs); |
|
|
|
|
388
|
|
|
foreach ($items as $key => $value) { |
389
|
|
|
$this->assertArrayHasKey($key, $rs); |
|
|
|
|
390
|
|
|
$this->assertEquals($value, $rs[$key]); |
|
|
|
|
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
$rs = $this->storage->hasItems(array_keys($items)); |
394
|
|
|
$this->assertIsArray($rs); |
|
|
|
|
395
|
|
|
$this->assertEquals(count($items), count($rs)); |
|
|
|
|
396
|
|
|
foreach ($items as $key => $value) { |
397
|
|
|
$this->assertContains($key, $rs); |
|
|
|
|
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
$this->assertSame(['missing'], $this->storage->removeItems(['missing', 'key1', 'key3'])); |
|
|
|
|
401
|
|
|
unset($items['key1'], $items['key3']); |
402
|
|
|
|
403
|
|
|
$rs = $this->storage->getItems(array_keys($items)); |
404
|
|
|
$this->assertIsArray($rs); |
|
|
|
|
405
|
|
|
foreach ($items as $key => $value) { |
406
|
|
|
$this->assertArrayHasKey($key, $rs); |
|
|
|
|
407
|
|
|
$this->assertEquals($value, $rs[$key]); |
|
|
|
|
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
$rs = $this->storage->hasItems(array_keys($items)); |
411
|
|
|
$this->assertIsArray($rs); |
|
|
|
|
412
|
|
|
$this->assertEquals(count($items), count($rs)); |
|
|
|
|
413
|
|
|
foreach ($items as $key => $value) { |
414
|
|
|
$this->assertContains($key, $rs); |
|
|
|
|
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
public function testSetGetHasAndRemoveItemWithNamespace() : void |
419
|
|
|
{ |
420
|
|
|
// write "key" to default namespace |
421
|
|
|
$this->options->setNamespace('defaultns1'); |
422
|
|
|
$this->assertTrue($this->storage->setItem('key', 'defaultns1')); |
|
|
|
|
423
|
|
|
|
424
|
|
|
// write "key" to an other default namespace |
425
|
|
|
$this->options->setNamespace('defaultns2'); |
426
|
|
|
$this->assertTrue($this->storage->setItem('key', 'defaultns2')); |
|
|
|
|
427
|
|
|
|
428
|
|
|
// test value of defaultns2 |
429
|
|
|
$this->assertTrue($this->storage->hasItem('key')); |
|
|
|
|
430
|
|
|
$this->assertEquals('defaultns2', $this->storage->getItem('key')); |
|
|
|
|
431
|
|
|
|
432
|
|
|
// test value of defaultns1 |
433
|
|
|
$this->options->setNamespace('defaultns1'); |
434
|
|
|
$this->assertTrue($this->storage->hasItem('key')); |
|
|
|
|
435
|
|
|
$this->assertEquals('defaultns1', $this->storage->getItem('key')); |
|
|
|
|
436
|
|
|
|
437
|
|
|
// remove item of defaultns1 |
438
|
|
|
$this->options->setNamespace('defaultns1'); |
439
|
|
|
$this->assertTrue($this->storage->removeItem('key')); |
|
|
|
|
440
|
|
|
$this->assertFalse($this->storage->hasItem('key')); |
|
|
|
|
441
|
|
|
|
442
|
|
|
// remove item of defaultns2 |
443
|
|
|
$this->options->setNamespace('defaultns2'); |
444
|
|
|
$this->assertTrue($this->storage->removeItem('key')); |
|
|
|
|
445
|
|
|
$this->assertFalse($this->storage->hasItem('key')); |
|
|
|
|
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
public function testSetGetHasAndRemoveItemsWithNamespace() : void |
449
|
|
|
{ |
450
|
|
|
$items = [ |
451
|
|
|
'key1' => 'value1', |
452
|
|
|
'key2' => 'value2', |
453
|
|
|
'key3' => 'value3', |
454
|
|
|
]; |
455
|
|
|
|
456
|
|
|
$this->options->setNamespace('defaultns1'); |
457
|
|
|
$this->assertSame([], $this->storage->setItems($items)); |
|
|
|
|
458
|
|
|
|
459
|
|
|
$this->options->setNamespace('defaultns2'); |
460
|
|
|
$this->assertSame([], $this->storage->hasItems(array_keys($items))); |
|
|
|
|
461
|
|
|
|
462
|
|
|
$this->options->setNamespace('defaultns1'); |
463
|
|
|
$rs = $this->storage->getItems(array_keys($items)); |
464
|
|
|
$this->assertIsArray($rs); |
|
|
|
|
465
|
|
|
foreach ($items as $key => $value) { |
466
|
|
|
$this->assertArrayHasKey($key, $rs); |
|
|
|
|
467
|
|
|
$this->assertEquals($value, $rs[$key]); |
|
|
|
|
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
$rs = $this->storage->hasItems(array_keys($items)); |
471
|
|
|
$this->assertIsArray($rs); |
|
|
|
|
472
|
|
|
$this->assertEquals(count($items), count($rs)); |
|
|
|
|
473
|
|
|
foreach ($items as $key => $value) { |
474
|
|
|
$this->assertContains($key, $rs); |
|
|
|
|
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
// remove the first and the last item |
478
|
|
|
$this->assertSame(['missing'], $this->storage->removeItems(['missing', 'key1', 'key3'])); |
|
|
|
|
479
|
|
|
unset($items['key1'], $items['key3']); |
480
|
|
|
|
481
|
|
|
$rs = $this->storage->getItems(array_keys($items)); |
482
|
|
|
$this->assertIsArray($rs); |
|
|
|
|
483
|
|
|
foreach ($items as $key => $value) { |
484
|
|
|
$this->assertArrayHasKey($key, $rs); |
|
|
|
|
485
|
|
|
$this->assertEquals($value, $rs[$key]); |
|
|
|
|
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
$rs = $this->storage->hasItems(array_keys($items)); |
489
|
|
|
$this->assertIsArray($rs); |
|
|
|
|
490
|
|
|
$this->assertEquals(count($items), count($rs)); |
|
|
|
|
491
|
|
|
foreach ($items as $key => $value) { |
492
|
|
|
$this->assertContains($key, $rs); |
|
|
|
|
493
|
|
|
} |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
public function testSetAndGetItemOfDifferentTypes() : void |
497
|
|
|
{ |
498
|
|
|
$capabilities = $this->storage->getCapabilities(); |
499
|
|
|
|
500
|
|
|
$types = [ |
501
|
|
|
'NULL' => null, |
502
|
|
|
'boolean' => true, |
503
|
|
|
'integer' => 12345, |
504
|
|
|
'double' => 123.45, |
505
|
|
|
'string' => 'string', // already tested |
506
|
|
|
'array' => ['one', 'tow' => 'two', 'three' => ['four' => 'four']], |
507
|
|
|
'object' => new stdClass(), |
508
|
|
|
'resource' => fopen(__FILE__, 'r'), |
509
|
|
|
]; |
510
|
|
|
|
511
|
|
|
$types['object']->one = 'one'; |
512
|
|
|
$types['object']->two = new stdClass(); |
513
|
|
|
$types['object']->two->three = 'three'; |
514
|
|
|
|
515
|
|
|
foreach ($capabilities->getSupportedDatatypes() as $sourceType => $targetType) { |
516
|
|
|
if ($targetType === false) { |
517
|
|
|
continue; |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
$value = $types[$sourceType]; |
521
|
|
|
$this->assertTrue( |
|
|
|
|
522
|
|
|
$this->storage->setItem('key', $value), |
523
|
|
|
sprintf( |
524
|
|
|
"Failed to set type '%s'", |
525
|
|
|
$sourceType |
526
|
|
|
) |
527
|
|
|
); |
528
|
|
|
|
529
|
|
|
if ($targetType === true) { |
530
|
|
|
$this->assertSame($value, $this->storage->getItem('key')); |
|
|
|
|
531
|
|
|
} elseif (is_string($targetType)) { |
532
|
|
|
settype($value, $targetType); |
533
|
|
|
$this->assertEquals($value, $this->storage->getItem('key')); |
|
|
|
|
534
|
|
|
} |
535
|
|
|
} |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
public function testSetItemReturnsFalseIfNonWritable() : void |
539
|
|
|
{ |
540
|
|
|
$this->options->setWritable(false); |
541
|
|
|
|
542
|
|
|
$this->assertFalse($this->storage->setItem('key', 'value')); |
|
|
|
|
543
|
|
|
$this->assertFalse($this->storage->hasItem('key')); |
|
|
|
|
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
public function testAddNewItem() : void |
547
|
|
|
{ |
548
|
|
|
$this->assertTrue($this->storage->addItem('key', 'value')); |
|
|
|
|
549
|
|
|
$this->assertTrue($this->storage->hasItem('key')); |
|
|
|
|
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
public function testAddItemReturnsFalseIfItemAlreadyExists() : void |
553
|
|
|
{ |
554
|
|
|
$this->assertTrue($this->storage->setItem('key', 'value')); |
|
|
|
|
555
|
|
|
$this->assertFalse($this->storage->addItem('key', 'newValue')); |
|
|
|
|
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
public function testAddItemReturnsFalseIfNonWritable() : void |
559
|
|
|
{ |
560
|
|
|
$this->options->setWritable(false); |
561
|
|
|
|
562
|
|
|
$this->assertFalse($this->storage->addItem('key', 'value')); |
|
|
|
|
563
|
|
|
$this->assertFalse($this->storage->hasItem('key')); |
|
|
|
|
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
public function testAddItemsReturnsFailedKeys() : void |
567
|
|
|
{ |
568
|
|
|
$this->assertTrue($this->storage->setItem('key1', 'value1')); |
|
|
|
|
569
|
|
|
|
570
|
|
|
$failedKeys = $this->storage->addItems([ |
571
|
|
|
'key1' => 'XYZ', |
572
|
|
|
'key2' => 'value2', |
573
|
|
|
]); |
574
|
|
|
|
575
|
|
|
$this->assertSame(['key1'], $failedKeys); |
|
|
|
|
576
|
|
|
$this->assertSame('value1', $this->storage->getItem('key1')); |
|
|
|
|
577
|
|
|
$this->assertTrue($this->storage->hasItem('key2')); |
|
|
|
|
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
public function testReplaceExistingItem() : void |
581
|
|
|
{ |
582
|
|
|
$this->assertTrue($this->storage->setItem('key', 'value')); |
|
|
|
|
583
|
|
|
$this->assertTrue($this->storage->replaceItem('key', 'anOtherValue')); |
|
|
|
|
584
|
|
|
$this->assertEquals('anOtherValue', $this->storage->getItem('key')); |
|
|
|
|
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
public function testReplaceItemReturnsFalseOnMissingItem() : void |
588
|
|
|
{ |
589
|
|
|
$this->assertFalse($this->storage->replaceItem('missingKey', 'value')); |
|
|
|
|
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
public function testReplaceItemReturnsFalseIfNonWritable() : void |
593
|
|
|
{ |
594
|
|
|
$this->storage->setItem('key', 'value'); |
595
|
|
|
$this->options->setWritable(false); |
596
|
|
|
|
597
|
|
|
$this->assertFalse($this->storage->replaceItem('key', 'newvalue')); |
|
|
|
|
598
|
|
|
$this->assertEquals('value', $this->storage->getItem('key')); |
|
|
|
|
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
public function testReplaceItemsReturnsFailedKeys() : void |
602
|
|
|
{ |
603
|
|
|
$this->assertTrue($this->storage->setItem('key1', 'value1')); |
|
|
|
|
604
|
|
|
|
605
|
|
|
$failedKeys = $this->storage->replaceItems([ |
606
|
|
|
'key1' => 'XYZ', |
607
|
|
|
'key2' => 'value2', |
608
|
|
|
]); |
609
|
|
|
|
610
|
|
|
$this->assertSame(['key2'], $failedKeys); |
|
|
|
|
611
|
|
|
$this->assertSame('XYZ', $this->storage->getItem('key1')); |
|
|
|
|
612
|
|
|
$this->assertFalse($this->storage->hasItem('key2')); |
|
|
|
|
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
public function testRemoveItemReturnsFalseOnMissingItem() : void |
616
|
|
|
{ |
617
|
|
|
$this->assertFalse($this->storage->removeItem('missing')); |
|
|
|
|
618
|
|
|
} |
619
|
|
|
|
620
|
|
|
public function testRemoveItemsReturnsMissingKeys() : void |
621
|
|
|
{ |
622
|
|
|
$this->storage->setItem('key', 'value'); |
623
|
|
|
$this->assertSame(['missing'], $this->storage->removeItems(['key', 'missing'])); |
|
|
|
|
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
public function testCheckAndSetItem() : void |
627
|
|
|
{ |
628
|
|
|
$this->assertTrue($this->storage->setItem('key', 'value')); |
|
|
|
|
629
|
|
|
|
630
|
|
|
$success = null; |
631
|
|
|
$casToken = null; |
632
|
|
|
$this->assertEquals('value', $this->storage->getItem('key', $success, $casToken)); |
|
|
|
|
633
|
|
|
$this->assertNotNull($casToken); |
|
|
|
|
634
|
|
|
|
635
|
|
|
$this->assertTrue($this->storage->checkAndSetItem($casToken, 'key', 'newValue')); |
|
|
|
|
636
|
|
|
$this->assertFalse($this->storage->checkAndSetItem($casToken, 'key', 'failedValue')); |
|
|
|
|
637
|
|
|
$this->assertEquals('newValue', $this->storage->getItem('key')); |
|
|
|
|
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
public function testIncrementItem() : void |
641
|
|
|
{ |
642
|
|
|
$this->assertTrue($this->storage->setItem('counter', 10)); |
|
|
|
|
643
|
|
|
$this->assertEquals(15, $this->storage->incrementItem('counter', 5)); |
|
|
|
|
644
|
|
|
$this->assertEquals(15, $this->storage->getItem('counter')); |
|
|
|
|
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
public function testIncrementItemInitialValue() : void |
648
|
|
|
{ |
649
|
|
|
$this->assertEquals(5, $this->storage->incrementItem('counter', 5)); |
|
|
|
|
650
|
|
|
$this->assertEquals(5, $this->storage->getItem('counter')); |
|
|
|
|
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
public function testIncrementItemReturnsFalseIfNonWritable() : void |
654
|
|
|
{ |
655
|
|
|
$this->storage->setItem('key', 10); |
656
|
|
|
$this->options->setWritable(false); |
657
|
|
|
|
658
|
|
|
$this->assertFalse($this->storage->incrementItem('key', 5)); |
|
|
|
|
659
|
|
|
$this->assertEquals(10, $this->storage->getItem('key')); |
|
|
|
|
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
public function testIncrementItemsReturnsKeyValuePairsOfWrittenItems() : void |
663
|
|
|
{ |
664
|
|
|
$this->assertTrue($this->storage->setItem('key1', 10)); |
|
|
|
|
665
|
|
|
|
666
|
|
|
$result = $this->storage->incrementItems(['key1' => 10, 'key2' => 10]); |
667
|
|
|
|
668
|
|
|
ksort($result); |
669
|
|
|
|
670
|
|
|
$this->assertSame(['key1' => 20, 'key2' => 10], $result); |
|
|
|
|
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
public function testIncrementItemsReturnsEmptyArrayIfNonWritable() : void |
674
|
|
|
{ |
675
|
|
|
$this->storage->setItem('key', 10); |
676
|
|
|
$this->options->setWritable(false); |
677
|
|
|
|
678
|
|
|
$this->assertSame([], $this->storage->incrementItems(['key' => 5])); |
|
|
|
|
679
|
|
|
$this->assertEquals(10, $this->storage->getItem('key')); |
|
|
|
|
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
public function testDecrementItem() : void |
683
|
|
|
{ |
684
|
|
|
$this->assertTrue($this->storage->setItem('counter', 30)); |
|
|
|
|
685
|
|
|
$this->assertEquals(25, $this->storage->decrementItem('counter', 5)); |
|
|
|
|
686
|
|
|
$this->assertEquals(25, $this->storage->getItem('counter')); |
|
|
|
|
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
public function testDecrementItemInitialValue() : void |
690
|
|
|
{ |
691
|
|
|
$this->assertEquals(-5, $this->storage->decrementItem('counter', 5)); |
|
|
|
|
692
|
|
|
$this->assertEquals(-5, $this->storage->getItem('counter')); |
|
|
|
|
693
|
|
|
} |
694
|
|
|
|
695
|
|
|
public function testDecrementItemReturnsFalseIfNonWritable() : void |
696
|
|
|
{ |
697
|
|
|
$this->storage->setItem('key', 10); |
698
|
|
|
$this->options->setWritable(false); |
699
|
|
|
|
700
|
|
|
$this->assertFalse($this->storage->decrementItem('key', 5)); |
|
|
|
|
701
|
|
|
$this->assertEquals(10, $this->storage->getItem('key')); |
|
|
|
|
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
public function testDecrementItemsReturnsEmptyArrayIfNonWritable() : void |
705
|
|
|
{ |
706
|
|
|
$this->storage->setItem('key', 10); |
707
|
|
|
$this->options->setWritable(false); |
708
|
|
|
|
709
|
|
|
$this->assertSame([], $this->storage->decrementItems(['key' => 5])); |
|
|
|
|
710
|
|
|
$this->assertEquals(10, $this->storage->getItem('key')); |
|
|
|
|
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
public function testTouchItemReturnsFalseOnMissingItem() : void |
714
|
|
|
{ |
715
|
|
|
$this->assertFalse($this->storage->touchItem('missing')); |
|
|
|
|
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
public function testTouchItemReturnsFalseIfNonWritable() : void |
719
|
|
|
{ |
720
|
|
|
$this->options->setWritable(false); |
721
|
|
|
|
722
|
|
|
$this->assertFalse($this->storage->touchItem('key')); |
|
|
|
|
723
|
|
|
} |
724
|
|
|
|
725
|
|
|
public function testTouchItemsReturnsGivenKeysIfNonWritable() : void |
726
|
|
|
{ |
727
|
|
|
$this->options->setWritable(false); |
728
|
|
|
$this->assertSame(['key'], $this->storage->touchItems(['key'])); |
|
|
|
|
729
|
|
|
} |
730
|
|
|
|
731
|
|
|
public function testSetItemAndSetItemsCallSaveWithTtl() : void |
732
|
|
|
{ |
733
|
|
|
$ttl = rand(); |
734
|
|
|
|
735
|
|
|
$provider = $this->createMock('Doctrine\Common\Cache\ArrayCache'); |
|
|
|
|
736
|
|
|
$provider->expects($this->exactly(4)) |
|
|
|
|
737
|
|
|
->method('save') |
738
|
|
|
->with($this->anything(), $this->anything(), $ttl); |
|
|
|
|
739
|
|
|
|
740
|
|
|
$this->storage = new DoctrineCacheStorage($this->options, $provider); |
|
|
|
|
741
|
|
|
|
742
|
|
|
$this->options->setTtl($ttl); |
743
|
|
|
$this->storage->setItem('key', 'value'); |
744
|
|
|
|
745
|
|
|
$items = [ |
746
|
|
|
'key1' => 'value1', |
747
|
|
|
'key2' => 'value2', |
748
|
|
|
'key3' => 'value3', |
749
|
|
|
]; |
750
|
|
|
$this->storage->setItems($items); |
751
|
|
|
} |
752
|
|
|
} |
753
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.