Location: PHPKode > scripts > PHP DataCache > php-datacachea1/tests/TestDataCacheDaoFile.php
<?php
/**
 * test harness for DataCacheDaoFile class tests
 *
 * Modification History: <pre>
 *	$Log: TestDataCacheDaoFile.php,v $
 *	Revision 1.1.1.1  2003/12/11 05:22:11  jsweat
 *	Initial check-in of PHP DataCache library.
 *	
 * </pre>
 * @author		Jason E. Sweat
 * @since		2003-10-26
 * @package		DataCache
 * @subpackage	tests
 * @version		$Id: TestDataCacheDaoFile.php,v 1.1.1.1 2003/12/11 05:22:11 jsweat Exp $
 */
error_reporting(E_ALL);

/**
 *	relative path to SimpleTest
 *	@ignore
 */
@define('SIMPLE_TEST', '../.lib/simpletest/');
/**
 *	SimpleTest base class
 */
require_once SIMPLE_TEST.'unit_tester.php';
/**
 *	SimpleTest reporter class
 */
require_once SIMPLE_TEST.'reporter.php';
/**
 * modify the php include path to include parent directory
 *
 * This is done becuase the tests are run from the tests sub-directory
 * and the application is run (and coded for) from the parent directory
 * @ignore
 */
if (!defined('TEST_PATH_MODIFIED')) {
	ini_set('include_path', '..:'.ini_get('include_path'));
	define('TEST_PATH_MODIFIED', true);
}
/**
 *	DataCache class to be tested
 */
require_once 'DataCacheDaoFile.php';
/**
 *	Application Identifier to use for the tests
 *	@ignore
 */
@define('TEST_APPLICATION', 'SimpleTests');
/**
 *	identifier for the missing cache id
 */
define('TEST_DAOFILE_BLANK', 'empty');
/**
 * big long multi line string with both quotes in it 
 * @todo 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	$_moCache	the data cache object
	 */
	var $_moCache;

	/**
	 * constructor
	 *
	 * @return	void
	 */
	function TestDataCacheDaoFile($psName='') {
		$this->UnitTestCase($psName);
	}
	
	/**
	 * initialization of test function
	 *
	 * @return	void
	 */
	function Setup() {
		$this->_moCache =& new DataCacheDaoFile(TEST_APPLICATION);
		$blank_file = DATACACHE_PATH.TEST_APPLICATION.'/'.TEST_DAOFILE_BLANK.'.php';
		if (file_exists($blank_file)) {
			unlink($blank_file);
		}
	}
	
	/**
	 * 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->_moCache->IsCached($id));
		$r = $this->_moCache->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->_moCache->SetCache($id,$ts.$ts));
		$this->AssertTrue(1 >= abs($this->_moCache->GetCacheTime($id) - $ts));
		$this->AssertEqual($this->_moCache->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->_moCache->SetCache('data', $data));
		$this->AssertIdentical($this->_moCache->GetCache('data'), $data);
		$this->AssertTrue($this->_moCache->SetCache('data', $data, false));
		$this->AssertIdentical($this->_moCache->GetCache('data'), $data);
	}


	/**
	 * test file compression
	 *
	 * @return	void
	 */
	function TestZip() {
		ob_start();
		phpinfo();
		$data = ob_get_contents();
		ob_end_clean();
		$this->AssertTrue($this->_moCache->SetCache('zip', $data));
		$this->AssertTrue($this->_moCache->SetCache('nozip', $data, false));
		$this->AssertTrue(	filesize(DATACACHE_PATH.TEST_APPLICATION.'/nozip.php') >
							filesize(DATACACHE_PATH.TEST_APPLICATION.'/zip.php'));
	}

	/**
	 * test removing cached data works
	 *
	 * @return	void
	 */
	function TestRemove()
	{
		$data = TEST_APPLICATION.mktime();
		$id = 'to_be_removed';
		$this->AssertTrue($this->_moCache->SetCache($id, $data));
		$this->AssertIdentical($data, $this->_moCache->GetCache($id));
		$this->AssertTrue($this->_moCache->RemoveCache($id));
		$r = $this->_moCache->GetCache($id);
		$this->AssertError("identifier '$id' does not have a valid cache");
	}

	/**
	 * test removing cached data works
	 *
	 * @return	void
	 */
	function TestRemovePattern()
	{
		$data = TEST_APPLICATION.mktime();
		$id1 = 'ato_be_removed';
		$id2 = 'bto_be_removed';
		$id3 = 'foobar';
		$this->AssertTrue($this->_moCache->SetCache($id1.'1', $data.'1'));
		$this->AssertTrue($this->_moCache->SetCache($id1.'2', $data.'2'));
		$this->AssertTrue($this->_moCache->SetCache($id2.'1', $data.'1'));
		$this->AssertTrue($this->_moCache->SetCache($id2.'2', $data.'2'));
		$this->AssertIdentical($data.'1', $this->_moCache->GetCache($id1.'1'));
		$this->AssertIdentical($data.'2', $this->_moCache->GetCache($id1.'2'));
		$this->AssertIdentical($data.'1', $this->_moCache->GetCache($id2.'1'));
		$this->AssertIdentical($data.'2', $this->_moCache->GetCache($id2.'2'));
		$this->AssertTrue($this->_moCache->RemovePatternCache(substr($id1,0,3)));
		$r = $this->_moCache->GetCache($id1.'1');
		$this->AssertError("identifier '{$id1}1' does not have a valid cache");
		$r = $this->_moCache->GetCache($id1.'2');
		$this->AssertError("identifier '{$id1}2' does not have a valid cache");
		$this->AssertIdentical($data.'1', $this->_moCache->GetCache($id2.'1'));
		$this->AssertIdentical($data.'2', $this->_moCache->GetCache($id2.'2'));
		$this->AssertTrue($this->_moCache->RemovePatternCache(substr($id2,0,5)));
		$r = $this->_moCache->GetCache($id2.'1');
		$this->AssertError("identifier '{$id2}1' does not have a valid cache");
		$r = $this->_moCache->GetCache($id2.'2');
		$this->AssertError("identifier '{$id2}2' does not have a valid cache");
		$this->AssertFalse($this->_moCache->RemovePatternCache($id3));
		$this->AssertError("identifier pattern '$id3' does not have any valid cached data");
	}

	
}

//run if stand alone
if (!isset($this)) {
	$test = &new TestDataCacheDaoFile('DataCache File DAO Test');
	$test->run(new HtmlReporter());
}

/* vim: set tabstop=4: */
#?>
Return current item: PHP DataCache