forked from PHPSocialNetwork/phpfastcache
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomDefaultFileNameFunction.php
54 lines (47 loc) · 1.68 KB
/
CustomDefaultFileNameFunction.php
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
<?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) <[email protected]>
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
*/
use Phpfastcache\CacheManager;
use Phpfastcache\Config\ConfigurationOption;
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
use Phpfastcache\Drivers\Files\Driver;
use Phpfastcache\EventManager;
use Phpfastcache\Tests\Helper\TestHelper;
chdir(__DIR__);
require_once __DIR__ . '/../vendor/autoload.php';
$testHelper = new TestHelper('Custom filename function');
$driverInstance = CacheManager::getInstance(
'Files',
new ConfigurationOption(
[
'defaultFileNameHashFunction' => static function (string $str) {
return hash('sha256', $str);
}
]
)
);
$testPasses = false;
EventManager::getInstance()->onCacheWriteFileOnDisk(
static function (ExtendedCacheItemPoolInterface $itemPool, $file) use ($driverInstance, &$testPasses) {
/** @var $driverInstance Driver */
$testPasses = strlen(basename($file, '.' . $driverInstance->getConfig()->getCacheFileExtension())) === 64;
}
);
$item = $driverInstance->getItem('TestCustomDefaultFileNameFunction');
$item->set(bin2hex(random_bytes(random_int(10, 100))));
$driverInstance->save($item);
if ($testPasses) {
$testHelper->assertPass('Custom filename function returned expected hash length');
} else {
$testHelper->assertFail('Custom filename function did not returned expected hash length');
}
$testHelper->terminateTest();