diff --git a/src/Pluf/Cache/File.php b/src/Pluf/Cache/File.php index d06b0ad..78213e0 100644 --- a/src/Pluf/Cache/File.php +++ b/src/Pluf/Cache/File.php @@ -51,7 +51,8 @@ class Pluf_Cache_File extends Pluf_Cache if ($timeout == null) $timeout = Pluf::f('cache_timeout', 300); if (!file_exists($dir)) mkdir($dir, 0777, true); $expire = time()+$timeout; - file_put_contents($fname, $expire."\n".serialize($value), LOCK_EX); + $success = file_put_contents($fname, $expire."\n".serialize($value), LOCK_EX); + return (false === $success) ? false : true; } /** diff --git a/src/Pluf/Tests/Cache/Apc.php b/src/Pluf/Tests/Cache/Apc.php new file mode 100644 index 0000000..1e5c51c --- /dev/null +++ b/src/Pluf/Tests/Cache/Apc.php @@ -0,0 +1,99 @@ + 'world', + 'foo' => false, + 0 => array('foo', 'bar') + ); + + public function __construct() + { + parent::__construct('Test the APC cache API.'); + } + + public function setUp() + { + $this->_config = $GLOBALS['_PX_config']; // backup + $GLOBALS['_PX_config']['cache_engine'] = 'Pluf_Cache_Apc'; + $GLOBALS['_PX_config']['cache_timeout'] = 5; + $GLOBALS['_PX_config']['cache_apc_keyprefix'] = 'pluf_unittest_'; + } + + public function tearDown() + { + $GLOBALS['_PX_config'] = $this->_config; + } + + public function testBasic() + { + $cache = Pluf_Cache::factory(); + $success = $cache->set('test1', 'foo1'); + $this->assertTrue($success); + $this->assertEqual('foo1', $cache->get('test1')); + } + + public function testGetUnknownKey() + { + $cache = Pluf_Cache::factory(); + $this->assertNull(null, $cache->get('unknown')); + } + + public function testGetDefault() + { + $cache = Pluf_Cache::factory(); + $this->assertEqual('default', $cache->get('unknown', 'default')); + } + + public function testSerialized() + { + $cache = Pluf_Cache::factory(); + $success = $cache->set('array', $this->_arrayData); + $this->assertTrue($success); + $this->assertCopy($this->_arrayData, $cache->get('array')); + + $obj = new stdClass(); + $obj->foo = 'bar'; + $obj->hello = 'world'; + $success = $cache->set('object', $obj); + $this->assertTrue($success); + $this->assertCopy($obj, $cache->get('object')); + + unset($obj); + $this->assertIsA($cache->get('object'), 'stdClass'); + $this->assertEqual('world', $cache->get('object')->hello); + } + + public function testCompress() + { + $GLOBALS['_PX_config']['cache_apc_compress'] = true; + $cache = Pluf_Cache::factory(); + $success = $cache->set('compressed', $this->_arrayData); + $this->assertTrue($success); + $this->assertCopy($this->_arrayData, $cache->get('compressed')); + } +} diff --git a/src/Pluf/Tests/Cache/File.php b/src/Pluf/Tests/Cache/File.php new file mode 100644 index 0000000..85a252b --- /dev/null +++ b/src/Pluf/Tests/Cache/File.php @@ -0,0 +1,102 @@ + 'world', + 'foo' => false, + 0 => array('foo', 'bar') + ); + + public function __construct() + { + parent::__construct('Test the File cache API.'); + } + + public function setUp() + { + $this->_config = $GLOBALS['_PX_config']; // backup + $GLOBALS['_PX_config']['cache_engine'] = 'Pluf_Cache_File'; + $GLOBALS['_PX_config']['cache_timeout'] = 5; + $GLOBALS['_PX_config']['cache_file_folder'] = '/tmp/pluf_unittest_cache'; + } + + public function tearDown() + { + $GLOBALS['_PX_config'] = $this->_config; + } + + public function testConstructor() + { + unset($GLOBALS['_PX_config']['cache_file_folder']); + try { + $cache = Pluf_Cache::factory(); + } catch (Pluf_Exception_SettingError $e) { + $this->pass(); + return; + } + $this->fail('An exception as not been raised, "cache_file_folder" setting not defined.'); + } + + public function testBasic() + { + $cache = Pluf_Cache::factory(); + $success = $cache->set('test1', 'foo1'); + $this->assertTrue($success); + $this->assertEqual('foo1', $cache->get('test1')); + } + + public function testGetUnknownKey() + { + $cache = Pluf_Cache::factory(); + $this->assertNull(null, $cache->get('unknown')); + } + + public function testGetDefault() + { + $cache = Pluf_Cache::factory(); + $this->assertEqual('default', $cache->get('unknown', 'default')); + } + + public function testSerialized() + { + $cache = Pluf_Cache::factory(); + $success = $cache->set('array', $this->_arrayData); + $this->assertTrue($success); + $this->assertCopy($this->_arrayData, $cache->get('array')); + + $obj = new stdClass(); + $obj->foo = 'bar'; + $obj->hello = 'world'; + $success = $cache->set('object', $obj); + $this->assertTrue($success); + $this->assertCopy($obj, $cache->get('object')); + + unset($obj); + $this->assertIsA($cache->get('object'), 'stdClass'); + $this->assertEqual('world', $cache->get('object')->hello); + } +}