1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace RoaveTest\DoctrineSimpleCache; |
5
|
|
|
|
6
|
|
|
use Roave\DoctrineSimpleCache\CacheException; |
7
|
|
|
use Roave\DoctrineSimpleCache\SimpleCacheAdapter; |
8
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\FullyImplementedCache; |
9
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\NotClearableCache; |
10
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\NotMultiGettableCache; |
11
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\NotMultiPuttableCache; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @covers \Roave\DoctrineSimpleCache\SimpleCacheAdapter |
15
|
|
|
*/ |
16
|
|
|
final class SimpleCacheAdapterTest extends \PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
public function testConstructorThrowsExceptionWhenNotMultiPuttableCacheIsUsed() |
19
|
|
|
{ |
20
|
|
|
/** @var NotMultiPuttableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
21
|
|
|
$doctrineCache = $this->createMock(NotMultiPuttableCache::class); |
22
|
|
|
|
23
|
|
|
$this->expectException(CacheException::class); |
24
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testConstructorThrowsExceptionWhenNotClearableCacheIsUsed() |
28
|
|
|
{ |
29
|
|
|
/** @var NotClearableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
30
|
|
|
$doctrineCache = $this->createMock(NotClearableCache::class); |
31
|
|
|
|
32
|
|
|
$this->expectException(CacheException::class); |
33
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testConstructorThrowsExceptionWhenNotMultiGettableCacheIsUsed() |
37
|
|
|
{ |
38
|
|
|
/** @var NotMultiGettableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
39
|
|
|
$doctrineCache = $this->createMock(NotMultiGettableCache::class); |
40
|
|
|
|
41
|
|
|
$this->expectException(CacheException::class); |
42
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testGetProxiesToDoctrineFetch() |
46
|
|
|
{ |
47
|
|
|
$key = uniqid('key', true); |
48
|
|
|
$value = uniqid('value', true); |
49
|
|
|
|
50
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
51
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
52
|
|
|
$doctrineCache->expects(self::once())->method('fetch')->with($key)->willReturn($value); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
55
|
|
|
self::assertSame($value, $psrCache->get($key)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testSetProxiesToDoctrineSave() |
59
|
|
|
{ |
60
|
|
|
$key = uniqid('key', true); |
61
|
|
|
$value = uniqid('value', true); |
62
|
|
|
$ttl = random_int(1000,9999); |
63
|
|
|
|
64
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
65
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
66
|
|
|
$doctrineCache->expects(self::once())->method('save')->with($key, $value, $ttl)->willReturn(true); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
69
|
|
|
self::assertTrue($psrCache->set($key, $value, $ttl)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public function testDeleteProxiesToDoctrineDelete() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$key = uniqid('key', true); |
75
|
|
|
|
76
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
77
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
78
|
|
|
$doctrineCache->expects(self::once())->method('delete')->with($key)->willReturn(true); |
|
|
|
|
79
|
|
|
|
80
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
81
|
|
|
self::assertTrue($psrCache->delete($key)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testClearProxiesToDeleteAll() |
85
|
|
|
{ |
86
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
87
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
88
|
|
|
$doctrineCache->expects(self::once())->method('deleteAll')->with()->willReturn(true); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
91
|
|
|
self::assertTrue($psrCache->clear()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testGetMultipleProxiesToFetchMultiple() |
95
|
|
|
{ |
96
|
|
|
$values = [ |
97
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
98
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
99
|
|
|
]; |
100
|
|
|
$keys = array_keys($values); |
101
|
|
|
|
102
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
103
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
104
|
|
|
$doctrineCache->expects(self::once())->method('fetchMultiple')->with($keys)->willReturn($values); |
|
|
|
|
105
|
|
|
|
106
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
107
|
|
|
self::assertSame($values, $psrCache->getMultiple($keys)); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testSetMultipleProxiesToSaveMultiple() |
111
|
|
|
{ |
112
|
|
|
$values = [ |
113
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
114
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
118
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
119
|
|
|
$doctrineCache->expects(self::once())->method('saveMultiple')->with($values)->willReturn(true); |
|
|
|
|
120
|
|
|
|
121
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
122
|
|
|
self::assertTrue($psrCache->setMultiple($values)); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
View Code Duplication |
public function testDeleteMultipleReturnsTrueWhenAllDeletesSucceed() |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$keys = [ |
128
|
|
|
uniqid('key1', true), |
129
|
|
|
uniqid('key2', true), |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
133
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
134
|
|
|
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(true); |
|
|
|
|
135
|
|
|
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); |
136
|
|
|
|
137
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
138
|
|
|
self::assertTrue($psrCache->deleteMultiple($keys)); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
View Code Duplication |
public function testDeleteMultipleReturnsFalseWhenOneDeleteFails() |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$keys = [ |
144
|
|
|
uniqid('key1', true), |
145
|
|
|
uniqid('key2', true), |
146
|
|
|
]; |
147
|
|
|
|
148
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
149
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
150
|
|
|
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(false); |
|
|
|
|
151
|
|
|
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); |
152
|
|
|
|
153
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
154
|
|
|
self::assertFalse($psrCache->deleteMultiple($keys)); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
View Code Duplication |
public function testHasProxiesToDoctrineContains() |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
$key = uniqid('key', true); |
160
|
|
|
|
161
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
162
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
163
|
|
|
$doctrineCache->expects(self::once())->method('contains')->with($key)->willReturn(true); |
|
|
|
|
164
|
|
|
|
165
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
166
|
|
|
self::assertTrue($psrCache->has($key)); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
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.