1
+ <?php
2
+
3
+ final class BaseCachesTest extends TestCase
4
+ {
5
+ public static function cacheProvider ()
6
+ {
7
+ return array (
8
+ array (Memcached::create ()),
9
+ array (SharedMemory::create ()),
10
+ array (PeclMemcached::create ()),
11
+ array (RuntimeMemory::create ()),
12
+ array (RubberFileSystem::create ())
13
+ );
14
+ }
15
+
16
+ /**
17
+ * @dataProvider cacheProvider
18
+ **/
19
+ public function testClean (CachePeer $ cache )
20
+ {
21
+ $ this ->assertInstanceOf ('CachePeer ' , $ cache ->clean ());
22
+ }
23
+
24
+ /**
25
+ * @dataProvider cacheProvider
26
+ **/
27
+ public function testClients (CachePeer $ cache )
28
+ {
29
+ $ this ->clientTest ($ cache );
30
+ $ this ->clientTest ($ cache ->enableCompression ());
31
+ }
32
+
33
+ /**
34
+ * @dataProvider cacheProvider
35
+ * @depends testClients
36
+ **/
37
+ public function testWatermarked (CachePeer $ cache )
38
+ {
39
+ $ cache = WatermarkedPeer::create ($ cache );
40
+ $ this ->clientTest ($ cache );
41
+ $ this ->clientTest ($ cache ->enableCompression ());
42
+ $ this ->doTestWrongKeys ($ cache );
43
+ }
44
+
45
+ /**
46
+ * @dataProvider cacheProvider
47
+ * @depends testWatermarked
48
+ **/
49
+ public function testWrongKeys (CachePeer $ cache )
50
+ {
51
+ if (!$ cache ->isAlive ()) {
52
+ return $ this ->markTestSkipped ('cache not available ' );
53
+ }
54
+
55
+ $ this ->doTestWrongKeys ($ cache );
56
+ }
57
+
58
+ /**
59
+ * @depends testWrongKeys
60
+ **/
61
+ public function testWithTimeout ()
62
+ {
63
+ $ cache =
64
+ Memcached::create ('localhost ' )->
65
+ setTimeout (200 );
66
+
67
+ $ cache ->add ('a ' , 'b ' );
68
+
69
+ $ this ->assertEquals ($ cache ->get ('a ' ), 'b ' );
70
+
71
+ $ cache ->clean ();
72
+ }
73
+
74
+ protected function clientTest (CachePeer $ cache )
75
+ {
76
+ if (!$ cache ->isAlive ()) {
77
+ return $ this ->markTestSkipped ('cache not available ' );
78
+ }
79
+
80
+ $ this ->clientTestSingleGet ($ cache );
81
+ $ this ->clientTestMultiGet ($ cache );
82
+ $ this ->doExpires ($ cache );
83
+ }
84
+
85
+ protected function clientTestSingleGet (CachePeer $ cache )
86
+ {
87
+ $ cache ->clean ();
88
+
89
+ $ value = 'a ' ;
90
+
91
+ $ this ->assertTrue ($ cache ->set ('a ' , $ value , Cache::EXPIRES_MEDIUM ));
92
+ $ this ->assertEquals ($ cache ->get ('a ' ), $ value );
93
+
94
+ $ this ->assertTrue ($ cache ->append ('a ' , $ value ));
95
+ $ this ->assertEquals ($ cache ->get ('a ' ), $ value .$ value );
96
+
97
+ $ this ->assertTrue ($ cache ->replace ('a ' , $ value ));
98
+ $ this ->assertEquals ($ cache ->get ('a ' ), $ value );
99
+
100
+ $ value = array (1 ,'s ' , 1234.18 , array (1 ,'w ' ));
101
+ $ this ->assertSame ($ value , $ value );
102
+ $ cache ->set ('a ' , $ value , Cache::EXPIRES_MEDIUM );
103
+ $ this ->assertSame ($ cache ->get ('a ' ), $ value );
104
+
105
+ $ value = 1 ;
106
+ $ cache ->set ('a ' , $ value , Cache::EXPIRES_MEDIUM );
107
+ $ this ->assertEquals ($ cache ->increment ('a ' , 1 ), $ value +1 );
108
+ $ this ->assertEquals ($ cache ->get ('a ' ), $ value +1 );
109
+ $ this ->assertEquals ($ cache ->increment ('a ' , 2 ), $ value +3 );
110
+ $ this ->assertEquals ($ cache ->get ('a ' ), $ value +3 );
111
+ $ this ->assertEquals ($ cache ->decrement ('a ' , 2 ), $ value +1 );
112
+ $ this ->assertEquals ($ cache ->get ('a ' ), $ value +1 );
113
+
114
+ $ value = '25 ' ;
115
+ $ cache ->set ('a ' , $ value , Cache::EXPIRES_MEDIUM );
116
+ $ cache ->append ('a ' , $ value );
117
+ $ this ->assertEquals ($ cache ->get ('a ' ), $ value .$ value );
118
+
119
+ $ this ->assertTrue ($ cache ->delete ('a ' ));
120
+
121
+ $ cache ->clean ();
122
+ }
123
+
124
+ protected function clientTestMultiGet (CachePeer $ cache )
125
+ {
126
+ $ cache ->clean ();
127
+
128
+ $ cache ->set ('a ' , 'a ' , Cache::EXPIRES_MEDIUM );
129
+ $ cache ->set ('b ' , 2 , Cache::EXPIRES_MEDIUM );
130
+ $ cache ->set ('c ' , 42.28 , Cache::EXPIRES_MEDIUM );
131
+
132
+ $ this ->assertEquals ($ cache ->get ('a ' ), 'a ' );
133
+ $ this ->assertEquals ($ cache ->get ('b ' ), 2 );
134
+ $ this ->assertEquals ($ cache ->get ('c ' ), 42.28 );
135
+
136
+ $ list = $ cache ->getList (array ('a ' , 'b ' , 'c ' ));
137
+
138
+ $ this ->assertEquals (count ($ list ), 3 );
139
+
140
+ $ this ->assertEquals ($ list ['a ' ], 'a ' );
141
+ $ this ->assertEquals ($ list ['b ' ], 2 );
142
+ $ this ->assertEquals ($ list ['c ' ], 42.28 );
143
+
144
+ $ list = $ cache ->getList (array ('a ' ));
145
+
146
+ $ this ->assertEquals (count ($ list ), 1 );
147
+
148
+ $ this ->assertEquals ($ list ['a ' ], 'a ' );
149
+
150
+ $ list = $ cache ->getList (array ('a ' , 'b ' , 'c ' , 'd ' ));
151
+
152
+ $ this ->assertEquals (count ($ list ), 3 );
153
+
154
+ $ this ->assertEquals ($ list ['a ' ], 'a ' );
155
+ $ this ->assertEquals ($ list ['b ' ], 2 );
156
+ $ this ->assertEquals ($ list ['c ' ], 42.28 );
157
+
158
+ $ list = $ cache ->getList (array ('d ' ));
159
+
160
+ $ this ->assertEquals (count ($ list ), 0 );
161
+
162
+ $ cache ->clean ();
163
+ }
164
+
165
+ private function doExpires (CachePeer $ cache )
166
+ {
167
+ $ cache ->clean ();
168
+
169
+ $ value = 'a ' ;
170
+
171
+ // do not set if exist and not expired (RubberFileSystem logic)
172
+ $ cache ->set ('a ' , $ value , Cache::EXPIRES_MAXIMUM );
173
+ $ this ->assertTrue ($ cache ->set ('a ' , '!!! ' , 1 ));
174
+ $ this ->assertEquals ($ cache ->get ('a ' ), $ value );
175
+ $ this ->assertTrue ($ cache ->replace ('a ' , '!!! ' , Cache::EXPIRES_MINIMUM ));
176
+ $ this ->assertEquals ($ cache ->get ('a ' ), '!!! ' );
177
+
178
+ $ cache ->replace ('a ' , $ value , 1 );
179
+ sleep (2 );
180
+ $ this ->assertFalse ($ cache ->get ('a ' ));
181
+
182
+ $ cache ->clean ();
183
+ }
184
+
185
+ private function doTestWrongKeys (CachePeer $ cache )
186
+ {
187
+ $ cache ->clean ();
188
+
189
+ $ value = 'a ' ;
190
+ // unexist key
191
+ $ this ->assertNull ($ cache ->get ('b ' ));
192
+ $ this ->assertTrue ($ cache ->isAlive ());
193
+ $ this ->assertFalse ($ cache ->replace ('b ' , $ value ));
194
+ $ this ->assertTrue ($ cache ->isAlive ());
195
+ $ this ->assertFalse ($ cache ->append ('b ' , $ value ));
196
+ $ this ->assertTrue ($ cache ->isAlive ());
197
+ $ this ->assertNull ($ cache ->increment ('b ' , $ value ));
198
+ $ this ->assertTrue ($ cache ->isAlive ());
199
+ $ this ->assertNull ($ cache ->decrement ('b ' , $ value ));
200
+ $ this ->assertTrue ($ cache ->isAlive ());
201
+ $ this ->assertFalse ($ cache ->delete ('b ' ));
202
+ $ this ->assertTrue ($ cache ->isAlive ());
203
+
204
+ // wrong key
205
+ $ this ->assertNull ($ cache ->get (null ));
206
+ $ this ->assertTrue ($ cache ->isAlive ());
207
+ $ this ->assertFalse ($ cache ->replace (null , $ value ));
208
+ $ this ->assertTrue ($ cache ->isAlive ());
209
+ $ this ->assertFalse ($ cache ->append (null , $ value ));
210
+ $ this ->assertTrue ($ cache ->isAlive ());
211
+ $ this ->assertNull ($ cache ->increment (null , $ value ));
212
+ $ this ->assertTrue ($ cache ->isAlive ());
213
+ $ this ->assertNull ($ cache ->decrement (null , $ value ));
214
+ $ this ->assertTrue ($ cache ->isAlive ());
215
+ $ this ->assertFalse ($ cache ->delete (null ));
216
+
217
+ $ this ->assertTrue ($ cache ->isAlive ());
218
+ $ cache ->clean ();
219
+ }
220
+ }
221
+ ?>
0 commit comments