1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoctrineModuleTest\Cache; |
4
|
|
|
|
5
|
|
|
use DoctrineModule\Cache\LaminasStorageCache; |
6
|
|
|
use Doctrine\Common\Cache\Cache; |
7
|
|
|
use Laminas\Cache\Storage\Adapter\Memory; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Tests for the cache bridge |
12
|
|
|
* |
13
|
|
|
* @license MIT |
14
|
|
|
* @link http://www.doctrine-project.org/ |
15
|
|
|
* @author Marco Pivetta <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class LaminasStorageCacheTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @return LaminasStorageCache |
21
|
|
|
*/ |
22
|
|
|
protected function getCacheDriver() |
23
|
|
|
{ |
24
|
|
|
return new LaminasStorageCache(new Memory()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testBasics() |
28
|
|
|
{ |
29
|
|
|
$cache = $this->getCacheDriver(); |
30
|
|
|
|
31
|
|
|
// Test save |
32
|
|
|
$cache->save('test_key', 'testing this out'); |
33
|
|
|
|
34
|
|
|
// Test contains to test that save() worked |
35
|
|
|
$this->assertTrue($cache->contains('test_key')); |
36
|
|
|
|
37
|
|
|
// Test fetch |
38
|
|
|
$this->assertEquals('testing this out', $cache->fetch('test_key')); |
39
|
|
|
|
40
|
|
|
// Test delete |
41
|
|
|
$cache->save('test_key2', 'test2'); |
42
|
|
|
$cache->delete('test_key2'); |
43
|
|
|
$this->assertFalse($cache->contains('test_key2')); |
44
|
|
|
|
45
|
|
|
// Fetch/save test with objects (Is cache driver serializes/unserializes objects correctly ?) |
46
|
|
|
$cache->save('test_object_key', new \ArrayObject()); |
47
|
|
|
$this->assertInstanceOf('ArrayObject', $cache->fetch('test_object_key')); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testDeleteAll() |
51
|
|
|
{ |
52
|
|
|
$cache = $this->getCacheDriver(); |
53
|
|
|
$cache->save('test_key1', '1'); |
54
|
|
|
$cache->save('test_key2', '2'); |
55
|
|
|
$cache->deleteAll(); |
56
|
|
|
|
57
|
|
|
$this->assertFalse($cache->contains('test_key1')); |
58
|
|
|
$this->assertFalse($cache->contains('test_key2')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testFlushAll() |
62
|
|
|
{ |
63
|
|
|
$cache = $this->getCacheDriver(); |
64
|
|
|
$cache->save('test_key1', '1'); |
65
|
|
|
$cache->save('test_key2', '2'); |
66
|
|
|
$cache->flushAll(); |
67
|
|
|
|
68
|
|
|
$this->assertFalse($cache->contains('test_key1')); |
69
|
|
|
$this->assertFalse($cache->contains('test_key2')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testNamespace() |
73
|
|
|
{ |
74
|
|
|
$cache = $this->getCacheDriver(); |
75
|
|
|
$cache->setNamespace('test_'); |
76
|
|
|
$cache->save('key1', 'test'); |
77
|
|
|
|
78
|
|
|
$this->assertTrue($cache->contains('key1')); |
79
|
|
|
|
80
|
|
|
$cache->setNamespace('test2_'); |
81
|
|
|
|
82
|
|
|
$this->assertFalse($cache->contains('key1')); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testGetStats() |
86
|
|
|
{ |
87
|
|
|
$cache = $this->getCacheDriver(); |
88
|
|
|
$stats = $cache->getStats(); |
89
|
|
|
|
90
|
|
|
$this->assertArrayHasKey(Cache::STATS_HITS, $stats); |
|
|
|
|
91
|
|
|
$this->assertArrayHasKey(Cache::STATS_MISSES, $stats); |
|
|
|
|
92
|
|
|
$this->assertArrayHasKey(Cache::STATS_UPTIME, $stats); |
|
|
|
|
93
|
|
|
$this->assertArrayHasKey(Cache::STATS_MEMORY_USAGE, $stats); |
|
|
|
|
94
|
|
|
$this->assertArrayHasKey(Cache::STATS_MEMORY_AVAILIABLE, $stats); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.