forked from TruCopilot/phpfastcache
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathItemTags.test.php
More file actions
400 lines (340 loc) · 15.8 KB
/
ItemTags.test.php
File metadata and controls
400 lines (340 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
/**
*
* This file is part of Phpfastcache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
*
* @author Georges.L (Geolim4) <contact@geolim4.com>
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
*/
use Phpfastcache\CacheManager;
use Phpfastcache\Core\Item\TaggableCacheItemInterface;
use Phpfastcache\Core\Pool\TaggableCacheItemPoolInterface;
use Phpfastcache\Tests\Helper\TestHelper;
chdir(__DIR__);
require_once __DIR__ . '/../vendor/autoload.php';
$testHelper = new TestHelper('Items tags features');
$defaultDriver = (!empty($argv[ 1 ]) ? ucfirst($argv[ 1 ]) : 'Files');
$driverInstance = CacheManager::getInstance($defaultDriver);
/**
* Item tag test // Init tags/items
*/
$createItemsCallback = static function () use ($driverInstance) {
$item1 = $driverInstance->getItem('tag-test1');
$item2 = $driverInstance->getItem('tag-test2');
$item3 = $driverInstance->getItem('tag-test3');
$item1->set('item-test_1')
->expiresAfter(600)
->addTag('tag-test_1')
->addTag('tag-test_all')
->addTag('tag-test_all2')
->addTag('tag-test_all3');
$item2->set('item-test_2')
->expiresAfter(600)
->addTag('tag-test_1')
->addTag('tag-test_2')
->addTag('tag-test_all')
->addTag('tag-test_all2')
->addTag('tag-test_all3');
$item3->set('item-test_3')
->expiresAfter(600)
->addTag('tag-test_1')
->addTag('tag-test_2')
->addTag('tag-test_3')
->addTag('tag-test_all')
->addTag('tag-test_all2')
->addTag('tag-test_all3')
->addTag('tag-test_all4');
$driverInstance->saveMultiple($item1, $item2, $item3);
return [
'item1' => $item1,
'item2' => $item2,
'item3' => $item3
];
};
$testHelper->printNoteText('##### TESTING POOL TAGS GETTERS');
/**
* Item tag test // Step 1
*/
$testHelper->printNewLine()->printText('#1 Testing getter: getItemsByTag() with strategy TAG_STRATEGY_ONE // Expecting 3 results');
$createItemsCallback();
$tagsItems = $driverInstance->getItemsByTag('tag-test_all', TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE);
if (is_array($tagsItems)) {
if (count($tagsItems) === 3) {
foreach ($tagsItems as $tagsItem) {
if (!in_array($tagsItem->getKey(), ['tag-test1', 'tag-test2', 'tag-test3'])) {
$testHelper->assertFail('STEP#1 // Got unexpected tagged item key:' . $tagsItem->getKey());
goto itemTagTest2;
}
}
$testHelper->assertPass('STEP#1 // Successfully retrieved 3 tagged item keys');
} else {
$testHelper->assertFail('STEP#1 //Got wrong count of item:' . count($tagsItems));
goto itemTagTest2;
}
} else {
$testHelper->assertFail('STEP#1 // Expected $tagsItems to be an array, got: ' . gettype($tagsItems));
goto itemTagTest2;
}
/**
* Item tag test // Step 2
*/
itemTagTest2:
$testHelper->printNewLine()->printText('#2 Testing getter: getItemsByTags() with strategy TAG_STRATEGY_ALL // Expecting 3 results');
$driverInstance->deleteItems(['item-test_1', 'item-test_2', 'item-test_3']);
$createItemsCallback();
$tagsItems = $driverInstance->getItemsByTags(['tag-test_all', 'tag-test_all2', 'tag-test_all3'], TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL);
if (is_array($tagsItems)) {
if (count($tagsItems) === 3) {
foreach ($tagsItems as $tagsItem) {
if (!in_array($tagsItem->getKey(), ['tag-test1', 'tag-test2', 'tag-test3'])) {
$testHelper->assertFail('STEP#2 // Got unexpected tagged item key:' . $tagsItem->getKey());
goto itemTagTest3;
}
}
$testHelper->assertPass('STEP#2 // Successfully retrieved 3 tagged item key');
} else {
$testHelper->assertFail('STEP#2 // Got wrong count of item:' . count($tagsItems));
goto itemTagTest3;
}
} else {
$testHelper->assertFail('STEP#2 // Expected $tagsItems to be an array, got: ' . gettype($tagsItems));
goto itemTagTest3;
}
/**
* Item tag test // Step 3
*/
itemTagTest3:
$testHelper->printNewLine()->printText('#3 Testing getter: getItemsByTags() with strategy TAG_STRATEGY_ALL // Expecting 1 result');
$driverInstance->deleteItems(['item-test_1', 'item-test_2', 'item-test_3']);
$createItemsCallback();
$tagsItems = $driverInstance->getItemsByTags(['tag-test_all', 'tag-test_all2', 'tag-test_all3', 'tag-test_all4'], TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL);
if (is_array($tagsItems)) {
if (count($tagsItems) === 1) {
if (isset($tagsItems['tag-test3'])) {
if ($tagsItems['tag-test3']->getKey() !== 'tag-test3') {
$testHelper->assertFail('STEP#3 // Got unexpected tagged item key:' . $tagsItems['tag-test3']->getKey());
goto itemTagTest4;
}
$testHelper->assertPass('STEP#3 // Successfully retrieved 1 tagged item keys');
} else {
$testHelper->assertFail('STEP#3 // Got wrong array key, expected "tag-test3", got "' . key($tagsItems) . '"');
goto itemTagTest4;
}
} else {
$testHelper->assertFail('STEP#3 // Got wrong count of item:' . count($tagsItems));
goto itemTagTest4;
}
} else {
$testHelper->assertFail('STEP#3 // Expected $tagsItems to be an array, got: ' . gettype($tagsItems));
goto itemTagTest4;
}
/**
* Item tag test // Step 4
*/
itemTagTest4:
$testHelper->printNewLine()->printText('#4 Testing deleter: deleteItemsByTag() // Expecting no item left');
$driverInstance->deleteItems(['item-test_1', 'item-test_2', 'item-test_3']);
$createItemsCallback();
$driverInstance->deleteItemsByTag('tag-test_all');
if (count($driverInstance->getItemsByTag('tag-test_all')) > 0) {
$testHelper->assertFail('[FAIL] STEP#4 // Getter getItemsByTag() found item(s), possible memory leak');
} else {
$testHelper->assertPass('STEP#4 // Getter getItemsByTag() found no item');
}
$i = 0;
while (++$i <= 3) {
if ($driverInstance->getItem("tag-test{$i}")->isHit()) {
$testHelper->assertFail("STEP#4 // Item 'tag-test{$i}' should've been deleted and is still in cache storage");
} else {
$testHelper->assertPass("STEP#4 // Item 'tag-test{$i}' have been deleted and is no longer in cache storage");
}
}
/**
* Item tag test // Step 5
*/
itemTagTest5:
$testHelper->printNewLine()->printText('#5 Testing deleter: deleteItemsByTags() with strategy TAG_STRATEGY_ALL // Expecting no item left');
$driverInstance->deleteItems(['item-test_1', 'item-test_2', 'item-test_3']);
$createItemsCallback();
$driverInstance->deleteItemsByTags(['tag-test_all', 'tag-test_all2', 'tag-test_all3'], TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL);
if (count($driverInstance->getItemsByTag('tag-test_all')) > 0) {
$testHelper->assertFail('STEP#5 // Getter getItemsByTag() found item(s), possible memory leak');
} else {
$testHelper->assertPass('STEP#5 // Getter getItemsByTag() found no item');
}
$i = 0;
while (++$i <= 3) {
if ($driverInstance->getItem("tag-test{$i}")->isHit()) {
$testHelper->assertFail("STEP#5 // Item 'tag-test{$i}' should've been deleted and is still in cache storage");
} else {
$testHelper->assertPass("STEP#5 // Item 'tag-test{$i}' have been deleted and is no longer in cache storage");
}
}
/**
* Item tag test // Step 6
*/
itemTagTest6:
$testHelper->printNewLine()->printText('#6 Testing deleter: deleteItemsByTags() with strategy TAG_STRATEGY_ALL // Expecting a specific item to be deleted');
$driverInstance->deleteItems(['item-test_1', 'item-test_2', 'item-test_3']);
$createItemsCallback();
/**
* Only item 'item-test_3' got all those tags
*/
$driverInstance->deleteItemsByTags(['tag-test_all', 'tag-test_all2', 'tag-test_all3', 'tag-test_all4'], TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL);
if ($driverInstance->getItem('item-test_3')->isHit()) {
$testHelper->assertFail('STEP#6 // Getter getItem() found item \'item-test_3\', possible memory leak');
} else {
$testHelper->assertPass('STEP#6 // Getter getItem() did not found item \'item-test_3\'');
}
/**
* Item tag test // Step 7
*/
itemTagTest7:
$testHelper->printNewLine()->printText('#7 Testing appender: appendItemsByTags() with strategy TAG_STRATEGY_ALL // Expecting items value to get an appended part of string');
$driverInstance->deleteItems(['item-test_1', 'item-test_2', 'item-test_3']);
$createItemsCallback();
$appendStr = '$*#*$';
$driverInstance->appendItemsByTags(['tag-test_all', 'tag-test_all2', 'tag-test_all3'], $appendStr, TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL);
foreach ($driverInstance->getItems(['tag-test1', 'tag-test2', 'tag-test3']) as $item) {
if (strpos($item->get(), $appendStr) === false) {
$testHelper->assertFail("STEP#7 // Item '{$item->getKey()}' does not have the string part '{$appendStr}' in it's value");
} else {
$testHelper->assertPass("STEP#7 // Item 'tag-test{$item->getKey()}' does have the string part '{$appendStr}' in it's value");
}
}
/**
* Item tag test // Step 7
*/
itemTagTest8:
$testHelper->printNewLine()->printText('#8 Testing prepender: prependItemsByTags() with strategy TAG_STRATEGY_ALL // Expecting items value to get a prepended part of string');
$driverInstance->deleteItems(['item-test_1', 'item-test_2', 'item-test_3']);
$createItemsCallback();
$prependStr = '&+_+&';
$driverInstance->prependItemsByTags(['tag-test_all', 'tag-test_all2', 'tag-test_all3'], $prependStr, TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL);
foreach ($driverInstance->getItems(['tag-test1', 'tag-test2', 'tag-test3']) as $item) {
if (strpos($item->get(), $prependStr) === false) {
$testHelper->assertFail("STEP#8 // Item '{$item->getKey()}' does not have the string part '{$prependStr}' in it's value");
} else {
$testHelper->assertPass("STEP#8 // Item 'tag-test{$item->getKey()}' does have the string part '{$prependStr}' in it's value");
}
}
/**
* Item tag test // Step 9
*/
itemTagTest9:
$testHelper->printNewLine()->printText('#9 Testing getter: getItemsByTags() with strategy TAG_STRATEGY_ONLY // Expecting 1 result');
$driverInstance->clear();
$createItemsCallback();
$tagsItems = $driverInstance->getItemsByTags(['tag-test_1', 'tag-test_all', 'tag-test_all2', 'tag-test_all3'], TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY);
if (is_array($tagsItems)) {
if (count($tagsItems) === 1) {
if (isset($tagsItems['tag-test1'])) {
if ($tagsItems['tag-test1']->getKey() !== 'tag-test1') {
$testHelper->assertFail('STEP#9 // Got unexpected tagged item key:' . $tagsItems['tag-test1']->getKey());
goto itemTagTest10;
}
$testHelper->assertPass('STEP#9 // Successfully retrieved 1 tagged item keys');
} else {
$testHelper->assertFail('STEP#9 // Got wrong array key, expected "tag-test3", got "' . key($tagsItems) . '"');
goto itemTagTest10;
}
} else {
$testHelper->assertFail('STEP#9 // Got wrong count of item:' . count($tagsItems));
goto itemTagTest10;
}
} else {
$testHelper->assertFail('STEP#9 // Expected $tagsItems to be an array, got: ' . gettype($tagsItems));
goto itemTagTest10;
}
/**
* Item tag test // Step 10
*/
itemTagTest10:
$testHelper->printNewLine()->printText('#10 Testing getter: getItemsByTags() with strategy TAG_STRATEGY_ONLY // Expecting 0 result');
$driverInstance->clear();
$createItemsCallback();
$tagsItems = $driverInstance->getItemsByTags(['tag-test_1', 'tag-test_all', 'tag-test_all2', /*'tag-test_all3'*/], TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY);
if (is_array($tagsItems)) {
if (count($tagsItems) === 0) {
$testHelper->assertPass('STEP#10 // Successfully retrieved 0 tagged item keys');
} else {
$testHelper->assertFail('STEP#10 // Got wrong count of item:' . count($tagsItems));
goto itemTagTest11;
}
} else {
$testHelper->assertFail('STEP#10 // Expected $tagsItems to be an array, got: ' . gettype($tagsItems));
goto itemTagTest11;
}
itemTagTest11:
$testHelper->printNewLine()->printNoteText('##### TESTING ITEM TAGS HASSERS');
$testHelper->printNewLine()->printText('#1 Testing ExtendedCacheItemInterface::hasTag()');
$driverInstance->clear();
$createItemsCallback();
$cacheItem = $driverInstance->getItem('tag-test1');
if ($cacheItem->hasTag('tag-test_1')) {
$testHelper->assertPass('STEP#1 // Successfully found the expected tag');
} else {
$testHelper->assertFail('STEP#1 // Failed finding the expected tag');
}
if (!$cacheItem->hasTag('non_existing_tag')) {
$testHelper->assertPass('STEP#2 // Successfully not found an unknown tag');
} else {
$testHelper->assertFail('STEP#2 // Failed not finding an unknown tag');
}
$testHelper->printNewLine()->printText('#2 Testing ExtendedCacheItemInterface::hasTags() with strategy "TAG_STRATEGY_ONE"');
$driverInstance->clear();
$createItemsCallback();
$cacheItem = $driverInstance->getItem('tag-test2');
if ($cacheItem->hasTags(['tag-test_1', 'tag-test_2'], TaggableCacheItemInterface::TAG_STRATEGY_ONE)) {
$testHelper->assertPass('STEP#1 // Successfully finding both the known tags');
} else {
$testHelper->assertFail('STEP#1 // Failed finding both the known tags');
}
if ($cacheItem->hasTags(['tag-test_1', 'non_existing_tag'], TaggableCacheItemInterface::TAG_STRATEGY_ONE)) {
$testHelper->assertPass('STEP#2 // Successfully finding one of the known tags');
} else {
$testHelper->assertFail('STEP#2 // Failed finding one of the known tags');
}
if (!$cacheItem->hasTags(['non_existing_tag', 'non_existing_tag2'], TaggableCacheItemInterface::TAG_STRATEGY_ONE)) {
$testHelper->assertPass('STEP#3 // Successfully not finding one of the unknown tags');
} else {
$testHelper->assertFail('STEP#3 // Failed not finding one of the unknown tags');
}
$testHelper->printNewLine()->printText('#3 Testing ExtendedCacheItemInterface::hasTags() with strategy "TAG_STRATEGY_ALL"');
$driverInstance->clear();
$createItemsCallback();
$cacheItem = $driverInstance->getItem('tag-test2');
if ($cacheItem->hasTags(['tag-test_1', 'tag-test_2'], TaggableCacheItemInterface::TAG_STRATEGY_ALL)) {
$testHelper->assertPass('STEP#1 // Successfully found both the known tags');
} else {
$testHelper->assertFail('STEP#1 // Failed finding both the known tags');
}
if ($cacheItem->hasTags(['tag-test_1', 'non_existing_tag'], TaggableCacheItemInterface::TAG_STRATEGY_ALL)) {
$testHelper->assertPass('STEP#2 // Successfully not finding both of the known and unknown tags');
} else {
$testHelper->assertPass('STEP#2 // Failed not finding both of the known and unknown tags');
}
$testHelper->printNewLine()->printText('#4 Testing ExtendedCacheItemInterface::hasTags() with strategy "TAG_STRATEGY_ONLY"');
$driverInstance->clear();
$createItemsCallback();
$cacheItem = $driverInstance->getItem('tag-test3');
if ($cacheItem->hasTags($cacheItem->getTags(), TaggableCacheItemInterface::TAG_STRATEGY_ONLY)) {
$testHelper->assertPass('STEP#1 // Successfully matching only and exclusively the known tags');
} else {
$testHelper->assertFail('STEP#1 // Failed matching only and exclusively the known tags');
}
if (!$cacheItem->hasTags(['tag-test_1', 'tag-test_2', /*'tag-test_3',*/ 'tag-test_all', 'tag-test_all2', 'tag-test_all3', 'tag-test_all4'], TaggableCacheItemInterface::TAG_STRATEGY_ONLY)) {
$testHelper->assertPass('STEP#1 // Successfully not matching only the known tags with some of them omitted');
} else {
$testHelper->assertPass('STEP#1 // Failed not matching only the known tags with some of them omitted');
}
if (!$cacheItem->hasTags(array_merge($cacheItem->getTags(), ['non_existing_tag']), TaggableCacheItemInterface::TAG_STRATEGY_ONLY)) {
$testHelper->assertPass('STEP#1 // Successfully matching only the known tags plus an unknown tag');
} else {
$testHelper->assertFail('STEP#1 // Failed matching only the known tags plus an unknown tag');
}
$testHelper->terminateTest();