forked from TruCopilot/phpfastcache
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCacheItemDetailedDate.test.php
More file actions
106 lines (95 loc) · 4.4 KB
/
CacheItemDetailedDate.test.php
File metadata and controls
106 lines (95 loc) · 4.4 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
<?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\Config\ConfigurationOption;
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
use Phpfastcache\CacheContract as CacheConditional;
use Phpfastcache\Tests\Helper\TestHelper;
use Psr\Cache\CacheItemPoolInterface;
chdir(__DIR__);
require_once __DIR__ . '/../vendor/autoload.php';
$testHelper = new TestHelper('Cache option: itemDetailedDate');
$defaultDriver = (!empty($argv[ 1 ]) ? ucfirst($argv[ 1 ]) : 'Files');
$cacheInstance = CacheManager::getInstance($defaultDriver, new ConfigurationOption([
'itemDetailedDate' => true,
'path' => __DIR__ . '/../cache/'
]));
$cacheKey = 'cacheKey';
$RandomCacheValue = str_shuffle(uniqid('pfc', true));
$testHelper->printText('Preparing cache test item...');
$realCreationDate = new \DateTime();
$cacheItem = $cacheInstance->getItem($cacheKey);
$cacheItem->set($RandomCacheValue)->expiresAfter(60);
$cacheInstance->save($cacheItem);
$cacheInstance->detachAllItems();
$diffSeconds = 3;
unset($cacheItem);
for ($i = 0; $i < $diffSeconds; $i++) {
$testHelper->printText(sprintf("Sleeping {$diffSeconds} seconds (%ds elapsed)", $i + 1));
sleep(1);
}
$testHelper->printText('Triggering modification date...');
$cacheItem = $cacheInstance->getItem($cacheKey);
$cacheItem->set(str_shuffle($RandomCacheValue));
$realModificationDate = new \DateTime();
$cacheInstance->save($cacheItem);
$cacheInstance->detachAllItems();
unset($cacheItem);
for ($i = 0; $i < $diffSeconds; $i++) {
$testHelper->printText(sprintf("Sleeping {$diffSeconds} additional seconds (%ds elapsed)", $i + 1));
sleep(1);
}
$cacheItem = $cacheInstance->getItem($cacheKey);
try {
$creationDate = $cacheItem->getCreationDate();
if ($creationDate instanceof \DateTimeInterface) {
$testHelper->assertPass('The method getCreationDate() returned a DateTimeInterface object');
if ($creationDate->format(DateTime::W3C) === $realCreationDate->format(DateTime::W3C)) {
$testHelper->assertPass('The item creation date effectively represents the real creation date (obviously).');
} else {
$testHelper->assertFail('The item creation date does not represents the real creation date.');
}
} else {
$testHelper->assertFail('The method getCreationDate() does not returned a DateTimeInterface object, got: ' . var_export($creationDate, true));
}
} catch (PhpfastcacheLogicException $e) {
$testHelper->assertFail('The method getCreationDate() unexpectedly thrown a phpfastcacheLogicException');
}
try {
$modificationDate = $cacheItem->getModificationDate();
if ($modificationDate instanceof \DateTimeInterface) {
$testHelper->assertPass('The method getModificationDate() returned a DateTimeInterface object');
if ($modificationDate->format(DateTime::W3C) === $realModificationDate->format(DateTime::W3C)) {
$testHelper->assertPass('The item modification date effectively represents the real modification date (obviously).');
} else {
$testHelper->assertFail('The item modification date does not represents the real modification date.');
}
/**
* Using >= operator instead of === due to a possible micro time
* offset that can often results to a value of 6 seconds (rounded)
*/
if ($modificationDate->getTimestamp() - $cacheItem->getCreationDate()->getTimestamp() >= $diffSeconds) {
$testHelper->assertPass("The item modification date is effectively {$diffSeconds} seconds greater than the creation date.");
} else {
$testHelper->assertFail('The item modification date effectively is not greater than the creation date.');
}
} else {
$testHelper->assertFail('The method getModificationDate() does not returned a DateTimeInterface object, got: ' . var_export($modificationDate, true));
}
} catch (PhpfastcacheLogicException $e) {
$testHelper->assertFail('The method getModificationDate() unexpectedly thrown a phpfastcacheLogicException');
}
$cacheInstance->clear();
unset($cacheInstance);
CacheManager::clearInstances();
$testHelper->terminateTest();