1+ <?php
2+
3+ /**
4+ * @author Khoa Bui (khoaofgod) <[email protected] > http://www.phpfastcache.com 5+ * @author Georges.L (Geolim4) <[email protected] > 6+ */
7+
8+ use phpFastCache \CacheManager ;
9+ use phpFastCache \Exceptions \phpFastCacheLogicException ;
10+ use phpFastCache \Helper \TestHelper ;
11+
12+ chdir (__DIR__ );
13+ require_once __DIR__ . '/../src/autoload.php ' ;
14+ $ testHelper = new TestHelper ('Cache option: itemDetailedDate ' );
15+ $ defaultDriver = (!empty ($ argv [ 1 ]) ? ucfirst ($ argv [ 1 ]) : 'Files ' );
16+ $ cacheInstance = CacheManager::getInstance ($ defaultDriver , ['itemDetailedDate ' => true , 'path ' => __DIR__ . '/../cache/ ' ]);
17+ $ cacheKey = 'cacheKey ' ;
18+ $ RandomCacheValue = str_shuffle (uniqid ('pfc ' , true ));
19+
20+ $ testHelper ->printText ('Preparing cache test item... ' );
21+ $ realCreationDate = new \DateTime ();
22+ $ cacheItem = $ cacheInstance ->getItem ($ cacheKey );
23+ $ cacheItem ->set ($ RandomCacheValue )->expiresAfter (60 );
24+ $ cacheInstance ->save ($ cacheItem );
25+ $ cacheInstance ->detachAllItems ();
26+ $ diffSeconds = 3 ;
27+
28+ unset($ cacheItem );
29+ for ($ i = 0 ; $ i < $ diffSeconds ; $ i ++){
30+ $ testHelper ->printText (sprintf ("Sleeping {$ diffSeconds } seconds (%ds elapsed) " , $ i + 1 ));
31+ sleep (1 );
32+ }
33+ $ testHelper ->printText ('Triggering modification date... ' );
34+
35+ $ cacheItem = $ cacheInstance ->getItem ($ cacheKey );
36+ $ cacheItem ->set (str_shuffle ($ RandomCacheValue ));
37+ $ realModificationDate = new \DateTime ();
38+ $ cacheInstance ->save ($ cacheItem );
39+ $ cacheInstance ->detachAllItems ();
40+ unset($ cacheItem );
41+
42+ for ($ i = 0 ; $ i < $ diffSeconds ; $ i ++){
43+ $ testHelper ->printText (sprintf ("Sleeping {$ diffSeconds } additional seconds (%ds elapsed) " , $ i + 1 ));
44+ sleep (1 );
45+ }
46+ $ cacheItem = $ cacheInstance ->getItem ($ cacheKey );
47+
48+ try {
49+ $ creationDate = $ cacheItem ->getCreationDate ();
50+ if ($ creationDate instanceof \DateTimeInterface){
51+ $ testHelper ->printPassText ('The method getCreationDate() returned a DateTimeInterface object ' );
52+ if ($ creationDate ->format (DateTime::W3C ) === $ realCreationDate ->format (DateTime::W3C )){
53+ $ testHelper ->printPassText ('The item creation date effectively represents the real creation date (obviously). ' );
54+ }else {
55+ $ testHelper ->printFailText ('The item creation date does not represents the real creation date. ' );
56+ }
57+ }else {
58+ $ testHelper ->printFailText ('The method getCreationDate() does not returned a DateTimeInterface object, got: ' . var_export ($ creationDate , true ));
59+ }
60+ }catch (phpFastCacheLogicException $ e ){
61+ $ testHelper ->printFailText ('The method getCreationDate() unexpectedly thrown a phpFastCacheLogicException ' );
62+ }
63+
64+ try {
65+ $ modificationDate = $ cacheItem ->getModificationDate ();
66+ if ($ modificationDate instanceof \DateTimeInterface){
67+ $ testHelper ->printPassText ('The method getModificationDate() returned a DateTimeInterface object ' );
68+ if ($ modificationDate ->format (DateTime::W3C ) === $ realModificationDate ->format (DateTime::W3C )){
69+ $ testHelper ->printPassText ('The item modification date effectively represents the real modification date (obviously). ' );
70+ }else {
71+ $ testHelper ->printFailText ('The item modification date does not represents the real modification date. ' );
72+ }
73+ /**
74+ * Using >= operator instead of === due to a possible micro time
75+ * offset that can often results to a value of 6 seconds (rounded)
76+ */
77+ if ($ modificationDate ->getTimestamp () - $ cacheItem ->getCreationDate ()->getTimestamp () >= $ diffSeconds ){
78+ $ testHelper ->printPassText ("The item modification date is effectively {$ diffSeconds } seconds greater than the creation date. " );
79+ }else {
80+ $ testHelper ->printFailText ('The item modification date effectively is not greater than the creation date. ' );
81+ }
82+ }else {
83+ $ testHelper ->printFailText ('The method getModificationDate() does not returned a DateTimeInterface object, got: ' . var_export ($ modificationDate , true ));
84+ }
85+ }catch (phpFastCacheLogicException $ e ){
86+ $ testHelper ->printFailText ('The method getModificationDate() unexpectedly thrown a phpFastCacheLogicException ' );
87+ }
88+
89+ $ cacheInstance ->clear ();
90+ unset($ cacheInstance );
91+ CacheManager::clearInstances ();
92+ $ cacheInstance = CacheManager::getInstance ($ defaultDriver , ['itemDetailedDate ' => false ]);
93+
94+ $ testHelper ->terminateTest ();
0 commit comments