1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmp\Cache\Application; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class TestCacheDecorator |
7
|
|
|
* |
8
|
|
|
* A backend especially designed for testing |
9
|
|
|
* |
10
|
|
|
* @package Cmp\Cache\Infrastureture |
11
|
|
|
*/ |
12
|
|
|
class TestCacheDecorator extends CacheDecorator |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Calls made to the cache |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $calls = [ |
20
|
|
|
'delete' => [], |
21
|
|
|
'set' => [], |
22
|
|
|
'has' => [], |
23
|
|
|
'get' => [], |
24
|
|
|
'demand' => [], |
25
|
|
|
'flush' => false, |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Checks if an item has been set |
30
|
|
|
* |
31
|
|
|
* @param string $key |
32
|
|
|
* @param mixed $value |
33
|
|
|
* @param int|null $timeToLive |
34
|
|
|
* |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
|
public function hasBeenSet($key, $value = null, $timeToLive = null) |
38
|
|
|
{ |
39
|
|
|
foreach ($this->calls['set'] as $set) { |
40
|
|
|
if ($this->callMatches($set, $key, $value, $timeToLive)) { |
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Checks if an item has been checked with the 'has' method |
50
|
|
|
* |
51
|
|
|
* @param string $key |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function hasBeenChecked($key) |
56
|
|
|
{ |
57
|
|
|
return $this->hasBeenDoneByKey($this->calls['has'], $key); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Checks if an item has been requested with 'get' |
62
|
|
|
* |
63
|
|
|
* @param string $key |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function hasBeenGet($key) |
68
|
|
|
{ |
69
|
|
|
return $this->hasBeenDoneByKey($this->calls['get'], $key); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Checks if an item has been requested with 'demand' |
74
|
|
|
* |
75
|
|
|
* @param string $key |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function hasBeenDemanded($key) |
80
|
|
|
{ |
81
|
|
|
return $this->hasBeenDoneByKey($this->calls['demand'], $key); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Checks if an item has been deleted |
86
|
|
|
* |
87
|
|
|
* @param string $key |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function hasBeenDeleted($key) |
92
|
|
|
{ |
93
|
|
|
return $this->hasBeenDoneByKey($this->calls['delete'], $key); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Checks if the cache has been flushed |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function hasBeenFlushed() |
102
|
|
|
{ |
103
|
|
|
return $this->calls['flush']; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function getCalls() |
110
|
|
|
{ |
111
|
|
|
return $this->calls; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function set($key, $value, $timeToLive = 0) |
118
|
|
|
{ |
119
|
|
|
$this->calls['set'][] = [ |
120
|
|
|
'key' => $key, |
121
|
|
|
'value' => $value, |
122
|
|
|
'timeToLive' => $timeToLive, |
123
|
|
|
]; |
124
|
|
|
|
125
|
|
|
parent::set($key, $value, $timeToLive); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function has($key) |
132
|
|
|
{ |
133
|
|
|
$this->calls['has'][] = $key; |
134
|
|
|
|
135
|
|
|
return parent::has($key); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
public function get($key, $default = null) |
142
|
|
|
{ |
143
|
|
|
$this->calls['get'][] = $key; |
144
|
|
|
|
145
|
|
|
return parent::get($key, $default); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
public function demand($key) |
152
|
|
|
{ |
153
|
|
|
$this->calls['demand'][] = $key; |
154
|
|
|
|
155
|
|
|
return parent::demand($key); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function delete($key) |
162
|
|
|
{ |
163
|
|
|
$this->calls['delete'][] = $key; |
164
|
|
|
|
165
|
|
|
parent::delete($key); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
|
|
public function flush() |
172
|
|
|
{ |
173
|
|
|
$this->calls['flush'] = true; |
174
|
|
|
|
175
|
|
|
parent::flush(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Tries to match a call by the key |
180
|
|
|
* |
181
|
|
|
* @param array $calls |
182
|
|
|
* @param string $key |
183
|
|
|
* |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
private function hasBeenDoneByKey(array $calls, $key) |
187
|
|
|
{ |
188
|
|
|
foreach ($calls as $keyOnCall) { |
189
|
|
|
if ($keyOnCall == $key) { |
190
|
|
|
return true; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param array $set |
199
|
|
|
* @param string $key |
200
|
|
|
* @param mixed $value |
201
|
|
|
* @param null|int $timeToLive |
202
|
|
|
* |
203
|
|
|
* @return bool |
204
|
|
|
*/ |
205
|
|
|
private function callMatches(array $set, $key, $value = null, $timeToLive) |
206
|
|
|
{ |
207
|
|
|
if ( |
208
|
|
|
$set['key'] != $key || |
209
|
|
|
!$this->matches($set['value'], $value) || |
210
|
|
|
!$this->matches($set['timeToLive'], $timeToLive) |
|
|
|
|
211
|
|
|
) { |
212
|
|
|
return false; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return true; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param mixed $stored |
220
|
|
|
* @param null $expected |
221
|
|
|
* |
222
|
|
|
* @return bool |
223
|
|
|
*/ |
224
|
|
|
private function matches($stored, $expected = null) |
225
|
|
|
{ |
226
|
|
|
return $expected === null || $stored == $expected; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.