|
From: Jason S. <jsw...@ya...> - 2003-10-29 12:55:19
|
Just a check to make sure I am headed in the right direction:
TestDataCacheDaoFile.php:
<?php
/**
* test harness for DataCacheDaoFile class tests
*
* @author Jason E. Sweat
* @since 2003-10-28
* @package DataCache
* @subpackage tests
* @version
* <pre>
* $Log: $
* </pre>
*/
error_reporting(E_ALL);
/**
* relative path to SimpleTest
*/
@define('SIMPLE_TEST', '../simpletest/');
/**
* SimpleTest base class
*/
require_once SIMPLE_TEST.'unit_tester.php';
/**
* SimpleTest reporter class
*/
require_once SIMPLE_TEST.'reporter.php';
/**
* DataCache class to be tested
*/
require_once '../DataCacheDaoFile.php';
/**
* Application Identifier to use for the tests
*/
@define('TEST_APPLICATION', 'SimpleTests');
/**
*
*/
define('TEST_DAOFILE_BLANK', 'empty');
/**
* still not working
* with funky chars ~!@#$%^&*()_+`-={}|\][:"<>?';/.,
*
*/
define('TEST_DAOFILE_STRING', <<<EOS
has both '
and " in it
on multiple lines
EOS
);
/**
*
* @package DataCache
* @subpackage tests
*/
class TestDataCacheDaoFile extends UnitTestCase
{
/**
* @var object $c the data cache object
*/
var $c;
/**
* constructor
*
* @return void
*/
function TestDataCacheDaoFile() {
$this->UnitTestCase();
}
/**
* initialization of test function
*
* @return void
*/
function Setup() {
$this->c =& new DataCacheDaoFile(TEST_APPLICATION);
$id = TEST_DAOFILE_BLANK;
if (file_exists(DATACACHE_PATH.TEST_APPLICATION."/$id.php")) {
unlink(DATACACHE_PATH.TEST_APPLICATION."/$id.php");
}
}
/**
* test php environment is setup correctly
*
* @return void
*/
function TestEnv() {
$this->AssertTrue(defined('DATACACHE_PATH'));
$this->AssertTrue(is_dir(DATACACHE_PATH));
$this->AssertEqual(substr(DATACACHE_PATH,-1,1), '/');
}
/**
* test creation of a directory when instanciated
*
* @return void
*/
function TestDirCreation() {
$id = 'mkdir';
$this->AssertFalse(is_dir(DATACACHE_PATH.TEST_APPLICATION.$id));
$o =& new DataCacheDaoFile(TEST_APPLICATION.$id);
$this->AssertTrue(is_dir(DATACACHE_PATH.TEST_APPLICATION.$id));
$this->AssertTrue(rmdir(DATACACHE_PATH.TEST_APPLICATION.$id));
}
/**
* test when data not already cached
*
* @return void
*/
function TestEmptyCache() {
$id = TEST_DAOFILE_BLANK;
$this->AssertFalse($this->c->IsCached($id));
$r = $this->c->GetCache($id);
$this->AssertError("identifier '$id' does not have a valid cache");
}
/**
* test timestamp
*
* @return void
*/
function TestTimeStamp() {
$id = 'time';
$ts = mktime();
$this->AssertTrue($this->c->SetCache($id,$ts.$ts));
$this->AssertTrue(1 >= abs($this->c->GetCacheTime($id) - $ts));
$this->AssertEqual($this->c->GetCache($id), $ts.$ts);
}
/**
* test caching different types
*
* @return void
*/
function TestCache() {
$data = array(
'str1' => 'a simple string'
,'str2' => "a multi-\nline string"
,'str3' => 'a "double quote" string'
,'str4' => "a 'single quote' string"
,'str5' => TEST_DAOFILE_STRING
,'int' => mktime()
,'array' => array('one', 'two', 'three')
,'object' => new DataCacheDaoFile(TEST_APPLICATION)
);
$this->AssertTrue($this->c->SetCache('data', $data));
$this->AssertIdentical($this->c->GetCache('data'), $data);
$this->AssertTrue($this->c->SetCache('data', $data, false));
$this->AssertIdentical($this->c->GetCache('data'), $data);
}
/**
* test file compression
*
* @return void
*/
function TestZip() {
ob_start();
phpinfo();
$data = ob_get_contents();
ob_end_clean();
$this->AssertTrue($this->c->SetCache('zip', $data));
$this->AssertTrue($this->c->SetCache('nozip', $data, false));
$this->AssertTrue( filesize(DATACACHE_PATH.TEST_APPLICATION.'/nozip.php') >
filesize(DATACACHE_PATH.TEST_APPLICATION.'/zip.php'));
}
}
//run if stand alone
if (!isset($this)) {
$test = &new TestDataCacheDaoFile();
$test->run(new HtmlReporter());
}
#?>
DataCacheDaoFile.php:
<?php
/**
* DataCacheDaoFile class definiton
*
* concrete file based DAO for the DataCache class
*
* A PHP Class to implement server side data caching
* Copyright (C) 2003 Jason E. Sweat
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @author Jason E. Sweat
* @since 2003-10-26
* @package DataCache
* @subpackage DAO
* @version
* <pre>
* $Log: $
* </pre>
*/
/**
* location where cache files are kept
*
* assumed in the rest of the code to be full path with a trailing slash
*/
define('DATACACHE_PATH', '/home/sweatje/phpc/cache/data/');
/**
* template for caching data as readable php code
*/
define('DATACACHE_TEMPLATE', <<<EOS
<?php
\$m_cache_data = unserialize(stripslashes('%s'));
#?>
EOS
);
/**
* template for caching data as zipped data
*/
define('DATACACHE_ZIP_TEMPLATE', <<<EOS
<?php
\$m_cache_data = unserialize(gzuncompress(base64_decode('%s')));
#?>
EOS
);
/**
* concrete file based DAO for the DataCache class
*
* @package DataCache
* @subpackage DAO
*/
class DataCacheDaoFile
{
/**
* @private
* @var string _msAppl the name of the application
*/
var $_msAppl;
/**
* constructor
*
* @param string $psAppl the application identifier
* @return void
*/
function DataCacheDaoFile($psAppl)
{
if (!is_dir(DATACACHE_PATH.$psAppl)) {
umask(0007);
mkdir(DATACACHE_PATH.$psAppl);
}
$this->_msAppl = $psAppl;
}
/**
* return the full path and file name for a cache file
*
* @private
* @param string $psId identifier for the data being cached
* @return string the file path and name
*/
function _GetFileName($psId)
{
return DATACACHE_PATH.$this->_msAppl.'/'.$psId.'.php';
}
/**
* cache a particular piece of data
*
* @param string $psId identifier for the data being cached
* @param mixed $pmValue value to cache
* @param boolean $pbZip optional - compress the data, defaults to true
* @return boolean sucess
*/
function SetCache($psId, $pmValue, $pbZip=true)
{
$s_file = $this->_GetFileName($psId);
$s_template = ($pbZip) ? DATACACHE_ZIP_TEMPLATE : DATACACHE_TEMPLATE;
$s_data = ($pbZip) ? base64_encode(gzcompress(serialize($pmValue)))
: addslashes(serialize($pmValue));
$r_fh = fopen($s_file, 'w');
flock($r_fh, LOCK_EX);
$b_ret = fwrite($r_fh,
sprintf($s_template,
$s_data
)
);
flock($r_fh, LOCK_UN);
fclose($r_fh);
return $b_ret;
}
/**
* check to see if a cached data is valid
*
* @param string $psId identifier for the data being cached
* @return boolean result of check
*/
function IsCached($psId)
{
return file_exists($this->_GetFileName($psId));
}
/**
* retrieve some cached data
*
* @param string $psId identifier for the data being cached
* @return mixed the data cached
*/
function GetCache($psId)
{
if ($this->IsCached($psId)) {
require $this->_GetFileName($psId);
return $m_cache_data;
} else {
trigger_error("identifier '$psId' does not have a valid cache");
}
}
/**
* retrieve time when data was cached
*
* @param string $psId identifier for the data being cached
* @return mixed the data cached
*/
function GetCacheTime($psId)
{
if ($this->IsCached($psId)) {
return filemtime($this->_GetFileName($psId));
} else {
trigger_error("identifier '$psId' does not have a valid cache");
}
}
}
#?>
__________________________________
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/britneyspears/
|