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
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Checks if an item has been deleted |
28
|
|
|
* |
29
|
|
|
* @param string $key |
30
|
|
|
* |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
|
|
public function hasBeenDeleted($key) |
34
|
|
|
{ |
35
|
|
|
return $this->hasBeenDoneByKey($this->calls['delete'], $key); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Checks if an item has been requested with 'get' or 'pull' |
40
|
|
|
* |
41
|
|
|
* @param string $key |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function hasBeenRetrieved($key) |
46
|
|
|
{ |
47
|
|
|
return $this->hasBeenDoneByKey($this->calls['get'], $key); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Checks if an item has been checked with the 'has' method |
52
|
|
|
* |
53
|
|
|
* @param string $key |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function hasBeenChecked($key) |
58
|
|
|
{ |
59
|
|
|
return $this->hasBeenDoneByKey($this->calls['has'], $key); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Checks if an item has been set |
64
|
|
|
* |
65
|
|
|
* @param string $key |
66
|
|
|
* @param mixed $value |
67
|
|
|
* @param int|null $timeToLive |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function hasBeenSet($key, $value = null, $timeToLive = null) |
72
|
|
|
{ |
73
|
|
|
foreach ($this->calls['set'] as $set) { |
74
|
|
|
if ($this->callMatches($set, $key, $value, $timeToLive)) { |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function getCalls() |
86
|
|
|
{ |
87
|
|
|
return $this->calls; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function delete($key) |
94
|
|
|
{ |
95
|
|
|
$this->calls['delete'][] = $key; |
96
|
|
|
|
97
|
|
|
parent::delete($key); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function set($key, $value, $timeToLive = 0) |
104
|
|
|
{ |
105
|
|
|
$this->calls['set'][] = [ |
106
|
|
|
'key' => $key, |
107
|
|
|
'value' => $value, |
108
|
|
|
'timeToLive' => $timeToLive, |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
parent::set($key, $value, $timeToLive); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function has($key) |
118
|
|
|
{ |
119
|
|
|
$this->calls['has'][] = $key; |
120
|
|
|
|
121
|
|
|
return parent::has($key); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public function get($key) |
128
|
|
|
{ |
129
|
|
|
$this->calls['get'][] = $key; |
130
|
|
|
|
131
|
|
|
return parent::get($key); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Tries to match a call by the key |
136
|
|
|
* |
137
|
|
|
* @param array $calls |
138
|
|
|
* @param string $key |
139
|
|
|
* |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
private function hasBeenDoneByKey(array $calls, $key) |
143
|
|
|
{ |
144
|
|
|
foreach ($calls as $keyOnCall) { |
145
|
|
|
if ($keyOnCall == $key) { |
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param array $set |
155
|
|
|
* @param string $key |
156
|
|
|
* @param mixed $value |
157
|
|
|
* @param null|int $timeToLive |
158
|
|
|
* |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
private function callMatches(array $set, $key, $value = null, $timeToLive) |
162
|
|
|
{ |
163
|
|
|
if ( |
164
|
|
|
$set['key'] != $key || |
165
|
|
|
!$this->matches($set['value'], $value) || |
166
|
|
|
!$this->matches($set['timeToLive'] !== $timeToLive) |
167
|
|
|
) { |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param mixed $stored |
176
|
|
|
* @param null $expected |
177
|
|
|
* |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
private function matches($stored, $expected = null) |
181
|
|
|
{ |
182
|
|
|
return $expected === null || $stored == $expected; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|