forked from TruCopilot/phpfastcache
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCacheContract.test.php
More file actions
142 lines (122 loc) · 4.53 KB
/
CacheContract.test.php
File metadata and controls
142 lines (122 loc) · 4.53 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
<?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\ExtendedCacheItemInterface;
use Phpfastcache\CacheContract;
use Phpfastcache\Tests\Helper\TestHelper;
use Psr\Cache\CacheItemInterface;
chdir(__DIR__);
require_once __DIR__ . '/../vendor/autoload.php';
$testHelper = new TestHelper('Cache Contract');
$defaultDriver = (!empty($argv[ 1 ]) ? ucfirst($argv[ 1 ]) : 'Files');
$cacheInstance = CacheManager::getInstance($defaultDriver);
$cacheKey = 'cacheKey';
$RandomCacheValue = str_shuffle(uniqid('pfc', true));
$cacheInstance->clear();
/**
* Missing cache item test
*/
$cacheValue = (new CacheContract($cacheInstance))->get($cacheKey, static function () use ($cacheKey, $testHelper, $RandomCacheValue) {
if (func_get_arg(0) instanceof ExtendedCacheItemInterface) {
$testHelper->assertPass('The callback has been received the cache item as a parameter (introduced in 8.0.6).');
} else {
$testHelper->assertFail('The callback has not received the cache item as a parameter (introduced in 8.0.6).');
}
/**
* No parameter are passed
* to this closure
*/
$testHelper->printText('Entering in closure as the cache item does not come from the cache backend.');
/**
* Here's your database/webservice/etc stuff
*/
return $RandomCacheValue . '-1337';
});
if ($cacheValue === $RandomCacheValue . '-1337') {
$testHelper->assertPass(sprintf('The cache contract successfully returned expected value "%s".', $cacheValue));
} else {
$testHelper->assertFail(sprintf('The cache contract returned an unexpected value "%s".', $cacheValue));
}
/**
* Existing cache item test
*/
$cacheItem = $cacheInstance->getItem($cacheKey);
$RandomCacheValue = str_shuffle(uniqid('pfc', true));
$cacheItem->set($RandomCacheValue);
$cacheInstance->save($cacheItem);
/**
* Remove objects references
*/
$cacheInstance->detachAllItems();
unset($cacheItem);
$cacheValue = (new CacheContract($cacheInstance))->get($cacheKey, static function () use ($cacheKey, $testHelper, $RandomCacheValue) {
/**
* No parameter are passed
* to this closure
*/
$testHelper->assertFail('Unexpected closure call.');
return $RandomCacheValue . '-1337';
});
if ($cacheValue === $RandomCacheValue) {
$testHelper->assertPass(sprintf('The cache contract successfully returned expected value "%s".', $cacheValue));
} else {
$testHelper->assertFail(sprintf('The cache contract returned an unexpected value "%s".', $cacheValue));
}
$cacheInstance->clear();
/**
* Test TTL
* @since 7.0
*/
$ttl = 5;
$RandomCacheValue = str_shuffle(uniqid('pfc', true));
$cacheInstance->detachAllItems();
unset($cacheItem);
$cacheValue = (new CacheContract($cacheInstance))->get($cacheKey, static fn() => $RandomCacheValue, $ttl);
$testHelper->printText(sprintf('Sleeping for %d seconds...', $ttl));
sleep($ttl + 1);
$cacheInstance->detachAllItems();
$cacheItem = $cacheInstance->getItem($cacheKey);
if (!$cacheItem->isHit()) {
$testHelper->assertPass(sprintf('The cache contract ttl successfully expired the cache after %d seconds', $ttl));
} else {
$testHelper->assertFail(sprintf('The cache contract ttl does not expired the cache after %d seconds', $ttl));
}
/**
* Test closure first argument
* @since 8.0.6
*/
$cacheInstance->clear();
(new CacheContract($cacheInstance))->get($cacheKey, static function () use ($testHelper) {
$args = func_get_args();
if (isset($args[0]) && $args[0] instanceof CacheItemInterface) {
$testHelper->assertPass('The callback has been received the cache item as the first parameter');
} else {
$testHelper->assertFail('The callback did not received the cache item as the first parameter');
}
});
/**
* Test callable cache contract syntax via __invoke()
* @since 8.0.6
*/
try {
$value = (new CacheContract($cacheInstance))($cacheKey, static function () use ($testHelper) {
$testHelper->assertPass('The CacheContract class is callable via __invoke()');
return null;
});
} catch (\Error $e) {
$testHelper->assertFail('The CacheContract class is not callable via __invoke()');
} catch (\Throwable $e) {
$testHelper->assertFail('Got an unknown error: ' . $e->getMessage());
}
$cacheInstance->clear();
$testHelper->terminateTest();