1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace RoaveTest\DoctrineSimpleCache; |
5
|
|
|
|
6
|
|
|
use Roave\DoctrineSimpleCache\SimpleCacheAdapter; |
7
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\FullyImplementedCache; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers \Roave\DoctrineSimpleCache\SimpleCacheAdapter |
11
|
|
|
*/ |
12
|
|
|
final class SimpleCacheAdapterTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
public function testGetProxiesToDoctrineFetch() |
15
|
|
|
{ |
16
|
|
|
$key = uniqid('key', true); |
17
|
|
|
$value = uniqid('value', true); |
18
|
|
|
|
19
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
20
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
21
|
|
|
$doctrineCache->expects(self::once())->method('fetch')->with($key)->willReturn($value); |
|
|
|
|
22
|
|
|
|
23
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
24
|
|
|
self::assertSame($value, $psrCache->get($key)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testSetProxiesToDoctrineSave() |
28
|
|
|
{ |
29
|
|
|
$key = uniqid('key', true); |
30
|
|
|
$value = uniqid('value', true); |
31
|
|
|
$ttl = random_int(1000,9999); |
32
|
|
|
|
33
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
34
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
35
|
|
|
$doctrineCache->expects(self::once())->method('save')->with($key, $value, $ttl)->willReturn(true); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
38
|
|
|
self::assertTrue($psrCache->set($key, $value, $ttl)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function testDeleteProxiesToDoctrineDelete() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$key = uniqid('key', true); |
44
|
|
|
|
45
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
46
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
47
|
|
|
$doctrineCache->expects(self::once())->method('delete')->with($key)->willReturn(true); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
50
|
|
|
self::assertTrue($psrCache->delete($key)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testClearProxiesToDeleteAll() |
54
|
|
|
{ |
55
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
56
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
57
|
|
|
$doctrineCache->expects(self::once())->method('deleteAll')->with()->willReturn(true); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
60
|
|
|
self::assertTrue($psrCache->clear()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGetMultipleProxiesToFetchMultiple() |
64
|
|
|
{ |
65
|
|
|
$values = [ |
66
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
67
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
68
|
|
|
]; |
69
|
|
|
$keys = array_keys($values); |
70
|
|
|
|
71
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
72
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
73
|
|
|
$doctrineCache->expects(self::once())->method('fetchMultiple')->with($keys)->willReturn($values); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
76
|
|
|
self::assertSame($values, $psrCache->getMultiple($keys)); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testSetMultipleProxiesToSaveMultiple() |
80
|
|
|
{ |
81
|
|
|
$values = [ |
82
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
83
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
87
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
88
|
|
|
$doctrineCache->expects(self::once())->method('saveMultiple')->with($values)->willReturn(true); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
91
|
|
|
self::assertTrue($psrCache->setMultiple($values)); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
public function testDeleteMultipleReturnsTrueWhenAllDeletesSucceed() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$keys = [ |
97
|
|
|
uniqid('key1', true), |
98
|
|
|
uniqid('key2', true), |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
102
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
103
|
|
|
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(true); |
|
|
|
|
104
|
|
|
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); |
105
|
|
|
|
106
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
107
|
|
|
self::assertTrue($psrCache->deleteMultiple($keys)); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
View Code Duplication |
public function testDeleteMultipleReturnsFalseWhenOneDeleteFails() |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$keys = [ |
113
|
|
|
uniqid('key1', true), |
114
|
|
|
uniqid('key2', true), |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
118
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
119
|
|
|
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(false); |
|
|
|
|
120
|
|
|
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); |
121
|
|
|
|
122
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
123
|
|
|
self::assertFalse($psrCache->deleteMultiple($keys)); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
public function testHasProxiesToDoctrineContains() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$key = uniqid('key', true); |
129
|
|
|
|
130
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
131
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
132
|
|
|
$doctrineCache->expects(self::once())->method('contains')->with($key)->willReturn(true); |
|
|
|
|
133
|
|
|
|
134
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
135
|
|
|
self::assertTrue($psrCache->has($key)); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: