-
-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathGetAllItems.test.php
More file actions
146 lines (123 loc) · 5.27 KB
/
GetAllItems.test.php
File metadata and controls
146 lines (123 loc) · 5.27 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
<?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\Event\Event;
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException;
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
use Phpfastcache\Exceptions\PhpfastcacheUnsupportedMethodException;
use Phpfastcache\Tests\Helper\TestHelper;
use Phpfastcache\Tests\Config\ConfigFactory;
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
use Phpfastcache\Event\EventReferenceParameter;
use Phpfastcache\EventManager;
chdir(__DIR__);
require_once __DIR__ . '/../../vendor/autoload.php';
$testHelper = new TestHelper('Testing getAllItems() method');
$testHelper->printText('[<blue>EXTENTION:</blue> (redis) <yellow>v' . \phpversion('redis') . '</yellow>]');
/**
* https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV5%CB%96%5D-Fetching-all-keys
*/
EventManager::getInstance()->on([Event::CACHE_GET_ALL_ITEMS], static function(ExtendedCacheItemPoolInterface $driver, EventReferenceParameter $referenceParameter) use ($testHelper, &$eventFlag){
$callback = $referenceParameter->getParameterValue();
$referenceParameter->setParameterValue(function(string $pattern) use ($callback, &$eventFlag, $testHelper) {
$eventFlag = true;
$testHelper->printInfoText('The custom event Event::CACHE_GET_ALL_ITEMS has been called.');
return $callback($pattern);
});
});
$drivers = [
'Memory',
'Predis',
'Redis',
'RedisCluster',
];
$driversConfigs = ConfigFactory::getDefaultConfigs();
foreach ($drivers as $i => $driverName) {
$testHelper->printNoteText(
sprintf(
"<blue>Testing</blue> <red>%s</red> <blue>against getAllItems() method</blue> (<yellow>%d</yellow>/<green>%d</green>)",
strtoupper($driverName),
$i + 1,
count($drivers),
)
);
try {
$poolCache = CacheManager::getInstance($driverName, $driversConfigs[$driverName] ?? null);
} catch (PhpfastcacheDriverConnectException|PhpfastcacheDriverCheckException $e){
$testHelper->assertSkip(
sprintf(
"<blue>Skipping</blue> <red>%s</red> <blue>against getAllItems() method</blue> (Caught <red>%s</red>: <yellow>%s</yellow>)",
strtoupper($driverName),
$e::class,
$e->getMessage(),
)
);
$testHelper->printNewLine();
continue;
}
$eventFlag = false;
$poolCache->clear();
$item1 = $poolCache->getItem('cache-test1');
$item2 = $poolCache->getItem('cache-test2');
$item3 = $poolCache->getItem('cache-test3');
$item1->set('test1')->expiresAfter(3600);
$item2->set('test2')->expiresAfter(3600);
$item3->set('test3')->expiresAfter(3600);
$poolCache->saveMultiple($item1, $item2, $item3);
$poolCache->detachAllItems();
unset($item1, $item2, $item3);
$items = $poolCache->getAllItems();
$itemCount = count($items);
if ($itemCount === 3) {
$testHelper->assertPass('getAllItems() returned 3 cache items as expected.');
} else {
$testHelper->assertFail(sprintf('getAllItems() unexpectedly returned %d cache items.', $itemCount));
}
foreach ($items as $key => $item) {
if ($item->isHit()) {
$testHelper->assertPass(sprintf('Item #%s is hit.', $item->getKey()));
} else {
$testHelper->assertFail(sprintf('Item #%s is not hit.', $item->getKey()));
}
if ($key === $item->getKey()) {
$testHelper->assertPass(sprintf('Cache item #%s object is identified by its cache key.', $item->getKey()));
} else {
$testHelper->assertFail(sprintf('Cache item #%s object is identified by "%s".', $item->getKey(), $key));
}
}
$testHelper->printNoteText("<blue>Testing getAllItems() method</blue> <yellow>(with pattern)</yellow>");
try {
$items = $poolCache->getAllItems('*test1*');
if (count($items) === 1) {
$testHelper->assertPass('Found 1 item using $pattern argument');
} else {
$testHelper->assertFail(sprintf('Found %d items using $pattern argument', count($items)));
}
} catch (PhpfastcacheInvalidArgumentException) {
$testHelper->assertSkip("Pattern argument unsupported by $driverName driver");
}
$testHelper->printNewLine(1);
}
$filesCache = CacheManager::getInstance('Files');
try {
$filesCache->getAllItems();
} catch (PhpfastcacheUnsupportedMethodException) {
$testHelper->assertPass('getAllItems() is not supported by Files driver as expected and thrown a PhpfastcacheUnsupportedMethodException.');
} catch (\Throwable $e) {
$testHelper->assertFail(sprintf('getAllItems() returned a exception "%s" instead of a PhpfastcacheUnsupportedMethodException.', $e::class));
}
if ($eventFlag) {
$testHelper->assertPass('The Event::CACHE_GET_ALL_ITEMS has been triggered allowing the callback to be customized.');
}
$testHelper->terminateTest();