1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DoctrineModuleTest\Cache; |
6
|
|
|
|
7
|
|
|
use ArrayObject; |
8
|
|
|
use Doctrine\Common\Cache\Cache; |
9
|
|
|
use DoctrineModule\Cache\LaminasStorageCache; |
10
|
|
|
use Laminas\Cache\Storage\Adapter\Memory; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Tests for the cache bridge |
15
|
|
|
* |
16
|
|
|
* @link http://www.doctrine-project.org/ |
17
|
|
|
*/ |
18
|
|
|
class LaminasStorageCacheTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
protected function getCacheDriver() : LaminasStorageCache |
21
|
|
|
{ |
22
|
|
|
return new LaminasStorageCache(new Memory()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testBasics() : void |
26
|
|
|
{ |
27
|
|
|
$cache = $this->getCacheDriver(); |
28
|
|
|
|
29
|
|
|
// Test save |
30
|
|
|
$cache->save('test_key', 'testing this out'); |
31
|
|
|
|
32
|
|
|
// Test contains to test that save() worked |
33
|
|
|
$this->assertTrue($cache->contains('test_key')); |
34
|
|
|
|
35
|
|
|
// Test fetch |
36
|
|
|
$this->assertEquals('testing this out', $cache->fetch('test_key')); |
37
|
|
|
|
38
|
|
|
// Test delete |
39
|
|
|
$cache->save('test_key2', 'test2'); |
40
|
|
|
$cache->delete('test_key2'); |
41
|
|
|
$this->assertFalse($cache->contains('test_key2')); |
42
|
|
|
|
43
|
|
|
// Fetch/save test with objects (Is cache driver serializes/unserializes objects correctly ?) |
44
|
|
|
$cache->save('test_object_key', new ArrayObject()); |
45
|
|
|
$this->assertInstanceOf('ArrayObject', $cache->fetch('test_object_key')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testDeleteAll() : void |
49
|
|
|
{ |
50
|
|
|
$cache = $this->getCacheDriver(); |
51
|
|
|
$cache->save('test_key1', '1'); |
52
|
|
|
$cache->save('test_key2', '2'); |
53
|
|
|
$cache->deleteAll(); |
54
|
|
|
|
55
|
|
|
$this->assertFalse($cache->contains('test_key1')); |
56
|
|
|
$this->assertFalse($cache->contains('test_key2')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testFlushAll() : void |
60
|
|
|
{ |
61
|
|
|
$cache = $this->getCacheDriver(); |
62
|
|
|
$cache->save('test_key1', '1'); |
63
|
|
|
$cache->save('test_key2', '2'); |
64
|
|
|
$cache->flushAll(); |
65
|
|
|
|
66
|
|
|
$this->assertFalse($cache->contains('test_key1')); |
67
|
|
|
$this->assertFalse($cache->contains('test_key2')); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testNamespace() : void |
71
|
|
|
{ |
72
|
|
|
$cache = $this->getCacheDriver(); |
73
|
|
|
$cache->setNamespace('test_'); |
74
|
|
|
$cache->save('key1', 'test'); |
75
|
|
|
|
76
|
|
|
$this->assertTrue($cache->contains('key1')); |
77
|
|
|
|
78
|
|
|
$cache->setNamespace('test2_'); |
79
|
|
|
|
80
|
|
|
$this->assertFalse($cache->contains('key1')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testGetStats() : void |
84
|
|
|
{ |
85
|
|
|
$cache = $this->getCacheDriver(); |
86
|
|
|
$stats = $cache->getStats(); |
87
|
|
|
|
88
|
|
|
$this->assertArrayHasKey(Cache::STATS_HITS, $stats); |
|
|
|
|
89
|
|
|
$this->assertArrayHasKey(Cache::STATS_MISSES, $stats); |
|
|
|
|
90
|
|
|
$this->assertArrayHasKey(Cache::STATS_UPTIME, $stats); |
|
|
|
|
91
|
|
|
$this->assertArrayHasKey(Cache::STATS_MEMORY_USAGE, $stats); |
|
|
|
|
92
|
|
|
$this->assertArrayHasKey(Cache::STATS_MEMORY_AVAILIABLE, $stats); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.