Skip to content

Commit d2d485e

Browse files
authored
Merge pull request #537 from Geolim4/final
Added test for cache option "itemDetailedDate" + Removed VersionEye badge 😭
2 parents 26b072d + 1ab9043 commit d2d485e

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![Total Downloads](http://img.shields.io/packagist/dt/phpfastcache/phpfastcache.svg)](https://packagist.org/packages/phpfastcache/phpfastcache) [![Dependency Status](https://www.versioneye.com/php/phpfastcache:phpfastcache/badge.svg)](https://www.versioneye.com/php/phpfastcache:phpfastcache) [![Latest Stable Version](http://img.shields.io/packagist/v/phpfastcache/phpfastcache.svg)](https://packagist.org/packages/phpfastcache/phpfastcache) [![License](https://img.shields.io/packagist/l/phpfastcache/phpfastcache.svg)](https://packagist.org/packages/phpfastcache/phpfastcache) [![Coding Standards](https://img.shields.io/badge/CI-PSR6-orange.svg)](https://github.com/php-fig/cache) [![Coding Standards](https://img.shields.io/badge/CS-PSR16-orange.svg)](https://github.com/php-fig/simple-cache)
1+
[![Total Downloads](http://img.shields.io/packagist/dt/phpfastcache/phpfastcache.svg)](https://packagist.org/packages/phpfastcache/phpfastcache) [![Latest Stable Version](http://img.shields.io/packagist/v/phpfastcache/phpfastcache.svg)](https://packagist.org/packages/phpfastcache/phpfastcache) [![License](https://img.shields.io/packagist/l/phpfastcache/phpfastcache.svg)](https://packagist.org/packages/phpfastcache/phpfastcache) [![Coding Standards](https://img.shields.io/badge/CI-PSR6-orange.svg)](https://github.com/php-fig/cache) [![Coding Standards](https://img.shields.io/badge/CS-PSR16-orange.svg)](https://github.com/php-fig/simple-cache)
22
[![Code Climate](https://codeclimate.com/github/PHPSocialNetwork/phpfastcache/badges/gpa.svg)](https://codeclimate.com/github/PHPSocialNetwork/phpfastcache) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/PHPSocialNetwork/phpfastcache/badges/quality-score.png?b=final)](https://scrutinizer-ci.com/g/PHPSocialNetwork/phpfastcache/?branch=final) [![Build Status](https://travis-ci.org/PHPSocialNetwork/phpfastcache.svg?branch=final)](https://travis-ci.org/PHPSocialNetwork/phpfastcache) [![Semver compliant](https://img.shields.io/badge/Semver-2.0.0-yellow.svg)](http://semver.org/spec/v2.0.0.html)
33

44
:exclamation: V6 USERS, PLEASE NOTE THAT THE V6 REQUIRES PHP 5.6 AT LEAST :exclamation:
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)