Initial commit

This commit is contained in:
mareksebera 2014-09-10 20:20:58 +02:00
commit 3b93da31de
1004 changed files with 265840 additions and 0 deletions

View file

@ -0,0 +1,522 @@
<?php
/**
* CacheTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Cache
* @since CakePHP(tm) v 1.2.0.5432
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Cache', 'Cache');
/**
* CacheTest class
*
* @package Cake.Test.Case.Cache
*/
class CacheTest extends CakeTestCase {
protected $_count = 0;
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->_cacheDisable = Configure::read('Cache.disable');
Configure::write('Cache.disable', false);
$this->_defaultCacheConfig = Cache::config('default');
Cache::config('default', array('engine' => 'File', 'path' => TMP . 'tests'));
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
Cache::drop('latest');
Cache::drop('page');
Cache::drop('archive');
Configure::write('Cache.disable', $this->_cacheDisable);
Cache::config('default', $this->_defaultCacheConfig['settings']);
}
/**
* testConfig method
*
* @return void
*/
public function testConfig() {
$settings = array('engine' => 'File', 'path' => TMP . 'tests', 'prefix' => 'cake_test_');
$results = Cache::config('new', $settings);
$this->assertEquals(Cache::config('new'), $results);
$this->assertTrue(isset($results['engine']));
$this->assertTrue(isset($results['settings']));
}
/**
* testConfigInvalidEngine method
*
* @expectedException CacheException
* @return void
*/
public function testConfigInvalidEngine() {
$settings = array('engine' => 'Imaginary');
Cache::config('imaginary', $settings);
}
/**
* Check that no fatal errors are issued doing normal things when Cache.disable is true.
*
* @return void
*/
public function testNonFatalErrorsWithCachedisable() {
Configure::write('Cache.disable', true);
Cache::config('test', array('engine' => 'File', 'path' => TMP, 'prefix' => 'error_test_'));
Cache::write('no_save', 'Noooo!', 'test');
Cache::read('no_save', 'test');
Cache::delete('no_save', 'test');
Cache::set('duration', '+10 minutes');
Configure::write('Cache.disable', false);
}
/**
* test configuring CacheEngines in App/libs
*
* @return void
*/
public function testConfigWithLibAndPluginEngines() {
App::build(array(
'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
), App::RESET);
CakePlugin::load('TestPlugin');
$settings = array('engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_');
$result = Cache::config('libEngine', $settings);
$this->assertEquals(Cache::config('libEngine'), $result);
$settings = array('engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_');
$result = Cache::config('pluginLibEngine', $settings);
$this->assertEquals(Cache::config('pluginLibEngine'), $result);
Cache::drop('libEngine');
Cache::drop('pluginLibEngine');
App::build();
CakePlugin::unload();
}
/**
* testInvalidConfig method
*
* Test that the cache class doesn't cause fatal errors with a partial path
*
* @expectedException PHPUnit_Framework_Error_Warning
* @return void
*/
public function testInvalidConfig() {
// In debug mode it would auto create the folder.
$debug = Configure::read('debug');
Configure::write('debug', 0);
Cache::config('invalid', array(
'engine' => 'File',
'duration' => '+1 year',
'prefix' => 'testing_invalid_',
'path' => 'data/',
'serialize' => true,
'random' => 'wii'
));
Cache::read('Test', 'invalid');
Configure::write('debug', $debug);
}
/**
* Test reading from a config that is undefined.
*
* @return void
*/
public function testReadNonExistingConfig() {
$this->assertFalse(Cache::read('key', 'totally fake'));
$this->assertFalse(Cache::write('key', 'value', 'totally fake'));
$this->assertFalse(Cache::increment('key', 1, 'totally fake'));
$this->assertFalse(Cache::decrement('key', 1, 'totally fake'));
}
/**
* test that trying to configure classes that don't extend CacheEngine fail.
*
* @expectedException CacheException
* @return void
*/
public function testAttemptingToConfigureANonCacheEngineClass() {
$this->getMock('StdClass', array(), array(), 'RubbishEngine');
Cache::config('Garbage', array(
'engine' => 'Rubbish'
));
}
/**
* testConfigChange method
*
* @return void
*/
public function testConfigChange() {
$_cacheConfigSessions = Cache::config('sessions');
$_cacheConfigTests = Cache::config('tests');
$result = Cache::config('sessions', array('engine' => 'File', 'path' => TMP . 'sessions'));
$this->assertEquals(Cache::settings('sessions'), $result['settings']);
$result = Cache::config('tests', array('engine' => 'File', 'path' => TMP . 'tests'));
$this->assertEquals(Cache::settings('tests'), $result['settings']);
Cache::config('sessions', $_cacheConfigSessions['settings']);
Cache::config('tests', $_cacheConfigTests['settings']);
}
/**
* test that calling config() sets the 'default' configuration up.
*
* @return void
*/
public function testConfigSettingDefaultConfigKey() {
Cache::config('test_name', array('engine' => 'File', 'prefix' => 'test_name_'));
Cache::write('value_one', 'I am cached', 'test_name');
$result = Cache::read('value_one', 'test_name');
$this->assertEquals('I am cached', $result);
$result = Cache::read('value_one');
$this->assertEquals(null, $result);
Cache::write('value_one', 'I am in default config!');
$result = Cache::read('value_one');
$this->assertEquals('I am in default config!', $result);
$result = Cache::read('value_one', 'test_name');
$this->assertEquals('I am cached', $result);
Cache::delete('value_one', 'test_name');
Cache::delete('value_one', 'default');
}
/**
* testWritingWithConfig method
*
* @return void
*/
public function testWritingWithConfig() {
$_cacheConfigSessions = Cache::config('sessions');
Cache::write('test_something', 'this is the test data', 'tests');
$expected = array(
'path' => TMP . 'sessions' . DS,
'prefix' => 'cake_',
'lock' => true,
'serialize' => true,
'duration' => 3600,
'probability' => 100,
'engine' => 'File',
'isWindows' => DIRECTORY_SEPARATOR === '\\',
'mask' => 0664,
'groups' => array()
);
$this->assertEquals($expected, Cache::settings('sessions'));
Cache::config('sessions', $_cacheConfigSessions['settings']);
}
/**
* testGroupConfigs method
*
* @return void
*/
public function testGroupConfigs() {
Cache::config('latest', array(
'duration' => 300,
'engine' => 'File',
'groups' => array(
'posts', 'comments',
),
));
$expected = array(
'posts' => array('latest'),
'comments' => array('latest'),
);
$result = Cache::groupConfigs();
$this->assertEquals($expected, $result);
$result = Cache::groupConfigs('posts');
$this->assertEquals(array('posts' => array('latest')), $result);
Cache::config('page', array(
'duration' => 86400,
'engine' => 'File',
'groups' => array(
'posts', 'archive'
),
));
$result = Cache::groupConfigs();
$expected = array(
'posts' => array('latest', 'page'),
'comments' => array('latest'),
'archive' => array('page'),
);
$this->assertEquals($expected, $result);
$result = Cache::groupConfigs('archive');
$this->assertEquals(array('archive' => array('page')), $result);
Cache::config('archive', array(
'duration' => 86400 * 30,
'engine' => 'File',
'groups' => array(
'posts', 'archive', 'comments',
),
));
$result = Cache::groupConfigs('archive');
$this->assertEquals(array('archive' => array('archive', 'page')), $result);
}
/**
* testGroupConfigsThrowsException method
*
* @expectedException CacheException
* @return void
*/
public function testGroupConfigsThrowsException() {
Cache::groupConfigs('bogus');
}
/**
* test that configured returns an array of the currently configured cache
* settings
*
* @return void
*/
public function testConfigured() {
$result = Cache::configured();
$this->assertTrue(in_array('_cake_core_', $result));
$this->assertTrue(in_array('default', $result));
}
/**
* testInitSettings method
*
* @return void
*/
public function testInitSettings() {
$initial = Cache::settings();
$override = array('engine' => 'File', 'path' => TMP . 'tests');
Cache::config('for_test', $override);
$settings = Cache::settings();
$expecting = $override + $initial;
$this->assertEquals($settings, $expecting);
}
/**
* test that drop removes cache configs, and that further attempts to use that config
* do not work.
*
* @return void
*/
public function testDrop() {
App::build(array(
'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
), App::RESET);
$result = Cache::drop('some_config_that_does_not_exist');
$this->assertFalse($result);
$_testsConfig = Cache::config('tests');
$result = Cache::drop('tests');
$this->assertTrue($result);
Cache::config('unconfigTest', array(
'engine' => 'TestAppCache'
));
$this->assertTrue(Cache::isInitialized('unconfigTest'));
$this->assertTrue(Cache::drop('unconfigTest'));
$this->assertFalse(Cache::isInitialized('TestAppCache'));
Cache::config('tests', $_testsConfig);
App::build();
}
/**
* testWriteEmptyValues method
*
* @return void
*/
public function testWriteEmptyValues() {
Cache::write('App.falseTest', false);
$this->assertFalse(Cache::read('App.falseTest'));
Cache::write('App.trueTest', true);
$this->assertTrue(Cache::read('App.trueTest'));
Cache::write('App.nullTest', null);
$this->assertNull(Cache::read('App.nullTest'));
Cache::write('App.zeroTest', 0);
$this->assertSame(Cache::read('App.zeroTest'), 0);
Cache::write('App.zeroTest2', '0');
$this->assertSame(Cache::read('App.zeroTest2'), '0');
}
/**
* Test that failed writes cause errors to be triggered.
*
* @return void
*/
public function testWriteTriggerError() {
App::build(array(
'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
), App::RESET);
Cache::config('test_trigger', array('engine' => 'TestAppCache', 'prefix' => ''));
try {
Cache::write('fail', 'value', 'test_trigger');
$this->fail('No exception thrown');
} catch (PHPUnit_Framework_Error $e) {
$this->assertTrue(true);
}
Cache::drop('test_trigger');
App::build();
}
/**
* testCacheDisable method
*
* Check that the "Cache.disable" configuration and a change to it
* (even after a cache config has been setup) is taken into account.
*
* @return void
*/
public function testCacheDisable() {
Configure::write('Cache.disable', false);
Cache::config('test_cache_disable_1', array('engine' => 'File', 'path' => TMP . 'tests'));
$this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
$this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
Configure::write('Cache.disable', true);
$this->assertFalse(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
$this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
Configure::write('Cache.disable', false);
$this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
$this->assertSame(Cache::read('key_3', 'test_cache_disable_1'), 'hello');
Configure::write('Cache.disable', true);
Cache::config('test_cache_disable_2', array('engine' => 'File', 'path' => TMP . 'tests'));
$this->assertFalse(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
$this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
Configure::write('Cache.disable', false);
$this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
$this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
Configure::write('Cache.disable', true);
$this->assertFalse(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
$this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
}
/**
* testSet method
*
* @return void
*/
public function testSet() {
$_cacheSet = Cache::set();
Cache::set(array('duration' => '+1 year'));
$data = Cache::read('test_cache');
$this->assertFalse($data);
$data = 'this is just a simple test of the cache system';
$write = Cache::write('test_cache', $data);
$this->assertTrue($write);
Cache::set(array('duration' => '+1 year'));
$data = Cache::read('test_cache');
$this->assertEquals('this is just a simple test of the cache system', $data);
Cache::delete('test_cache');
Cache::settings();
Cache::set($_cacheSet);
}
/**
* test set() parameter handling for user cache configs.
*
* @return void
*/
public function testSetOnAlternateConfigs() {
Cache::config('file_config', array('engine' => 'File', 'prefix' => 'test_file_'));
Cache::set(array('duration' => '+1 year'), 'file_config');
$settings = Cache::settings('file_config');
$this->assertEquals('test_file_', $settings['prefix']);
$this->assertEquals(strtotime('+1 year') - time(), $settings['duration']);
}
/**
* test remember method.
*
* @return void
*/
public function testRemember() {
$expected = 'This is some data 0';
$result = Cache::remember('test_key', array($this, 'cacher'), 'default');
$this->assertEquals($expected, $result);
$this->_count = 1;
$result = Cache::remember('test_key', array($this, 'cacher'), 'default');
$this->assertEquals($expected, $result);
}
/**
* Method for testing Cache::remember()
*
* @return string
*/
public function cacher() {
return 'This is some data ' . $this->_count;
}
}

View file

@ -0,0 +1,276 @@
<?php
/**
* ApcEngineTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Cache.Engine
* @since CakePHP(tm) v 1.2.0.5434
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Cache', 'Cache');
/**
* ApcEngineTest class
*
* @package Cake.Test.Case.Cache.Engine
*/
class ApcEngineTest extends CakeTestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->skipIf(!function_exists('apc_store'), 'Apc is not installed or configured properly.');
if (php_sapi_name() === 'cli') {
$this->skipIf(!ini_get('apc.enable_cli'), 'APC is not enabled for the CLI.');
}
$this->_cacheDisable = Configure::read('Cache.disable');
Configure::write('Cache.disable', false);
Cache::config('apc', array('engine' => 'Apc', 'prefix' => 'cake_'));
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
Configure::write('Cache.disable', $this->_cacheDisable);
Cache::drop('apc');
Cache::drop('apc_groups');
Cache::config('default');
}
/**
* testReadAndWriteCache method
*
* @return void
*/
public function testReadAndWriteCache() {
Cache::set(array('duration' => 1), 'apc');
$result = Cache::read('test', 'apc');
$expecting = '';
$this->assertEquals($expecting, $result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('test', $data, 'apc');
$this->assertTrue($result);
$result = Cache::read('test', 'apc');
$expecting = $data;
$this->assertEquals($expecting, $result);
Cache::delete('test', 'apc');
}
/**
* Writing cache entries with duration = 0 (forever) should work.
*
* @return void
*/
public function testReadWriteDurationZero() {
Cache::config('apc', array('engine' => 'Apc', 'duration' => 0, 'prefix' => 'cake_'));
Cache::write('zero', 'Should save', 'apc');
sleep(1);
$result = Cache::read('zero', 'apc');
$this->assertEquals('Should save', $result);
}
/**
* testExpiry method
*
* @return void
*/
public function testExpiry() {
Cache::set(array('duration' => 1), 'apc');
$result = Cache::read('test', 'apc');
$this->assertFalse($result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 'apc');
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test', 'apc');
$this->assertFalse($result);
Cache::set(array('duration' => 1), 'apc');
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 'apc');
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test', 'apc');
$this->assertFalse($result);
sleep(2);
$result = Cache::read('other_test', 'apc');
$this->assertFalse($result);
}
/**
* testDeleteCache method
*
* @return void
*/
public function testDeleteCache() {
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('delete_test', $data, 'apc');
$this->assertTrue($result);
$result = Cache::delete('delete_test', 'apc');
$this->assertTrue($result);
}
/**
* testDecrement method
*
* @return void
*/
public function testDecrement() {
$this->skipIf(!function_exists('apc_dec'), 'No apc_dec() function, cannot test decrement().');
$result = Cache::write('test_decrement', 5, 'apc');
$this->assertTrue($result);
$result = Cache::decrement('test_decrement', 1, 'apc');
$this->assertEquals(4, $result);
$result = Cache::read('test_decrement', 'apc');
$this->assertEquals(4, $result);
$result = Cache::decrement('test_decrement', 2, 'apc');
$this->assertEquals(2, $result);
$result = Cache::read('test_decrement', 'apc');
$this->assertEquals(2, $result);
}
/**
* testIncrement method
*
* @return void
*/
public function testIncrement() {
$this->skipIf(!function_exists('apc_inc'), 'No apc_inc() function, cannot test increment().');
$result = Cache::write('test_increment', 5, 'apc');
$this->assertTrue($result);
$result = Cache::increment('test_increment', 1, 'apc');
$this->assertEquals(6, $result);
$result = Cache::read('test_increment', 'apc');
$this->assertEquals(6, $result);
$result = Cache::increment('test_increment', 2, 'apc');
$this->assertEquals(8, $result);
$result = Cache::read('test_increment', 'apc');
$this->assertEquals(8, $result);
}
/**
* test the clearing of cache keys
*
* @return void
*/
public function testClear() {
apc_store('not_cake', 'survive');
Cache::write('some_value', 'value', 'apc');
$result = Cache::clear(false, 'apc');
$this->assertTrue($result);
$this->assertFalse(Cache::read('some_value', 'apc'));
$this->assertEquals('survive', apc_fetch('not_cake'));
apc_delete('not_cake');
}
/**
* Tests that configuring groups for stored keys return the correct values when read/written
* Shows that altering the group value is equivalent to deleting all keys under the same
* group
*
* @return void
*/
public function testGroupsReadWrite() {
Cache::config('apc_groups', array(
'engine' => 'Apc',
'duration' => 0,
'groups' => array('group_a', 'group_b'),
'prefix' => 'test_'
));
$this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'apc_groups'));
apc_inc('test_group_a');
$this->assertFalse(Cache::read('test_groups', 'apc_groups'));
$this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups'));
$this->assertEquals('value2', Cache::read('test_groups', 'apc_groups'));
apc_inc('test_group_b');
$this->assertFalse(Cache::read('test_groups', 'apc_groups'));
$this->assertTrue(Cache::write('test_groups', 'value3', 'apc_groups'));
$this->assertEquals('value3', Cache::read('test_groups', 'apc_groups'));
}
/**
* Tests that deleteing from a groups-enabled config is possible
*
* @return void
*/
public function testGroupDelete() {
Cache::config('apc_groups', array(
'engine' => 'Apc',
'duration' => 0,
'groups' => array('group_a', 'group_b'),
'prefix' => 'test_'
));
$this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'apc_groups'));
$this->assertTrue(Cache::delete('test_groups', 'apc_groups'));
$this->assertFalse(Cache::read('test_groups', 'apc_groups'));
}
/**
* Test clearing a cache group
*
* @return void
*/
public function testGroupClear() {
Cache::config('apc_groups', array(
'engine' => 'Apc',
'duration' => 0,
'groups' => array('group_a', 'group_b'),
'prefix' => 'test_'
));
$this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
$this->assertTrue(Cache::clearGroup('group_a', 'apc_groups'));
$this->assertFalse(Cache::read('test_groups', 'apc_groups'));
$this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups'));
$this->assertTrue(Cache::clearGroup('group_b', 'apc_groups'));
$this->assertFalse(Cache::read('test_groups', 'apc_groups'));
}
}

View file

@ -0,0 +1,553 @@
<?php
/**
* FileEngineTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Cache.Engine
* @since CakePHP(tm) v 1.2.0.5434
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Cache', 'Cache');
/**
* FileEngineTest class
*
* @package Cake.Test.Case.Cache.Engine
*/
class FileEngineTest extends CakeTestCase {
/**
* config property
*
* @var array
*/
public $config = array();
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
Configure::write('Cache.disable', false);
Cache::config('file_test', array('engine' => 'File', 'path' => CACHE));
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
// Cache::clear(false, 'file_test');
Cache::drop('file_test');
Cache::drop('file_groups');
Cache::drop('file_groups2');
Cache::drop('file_groups3');
}
/**
* testCacheDirChange method
*
* @return void
*/
public function testCacheDirChange() {
$result = Cache::config('sessions', array('engine' => 'File', 'path' => TMP . 'sessions'));
$this->assertEquals(Cache::settings('sessions'), $result['settings']);
$result = Cache::config('sessions', array('engine' => 'File', 'path' => TMP . 'tests'));
$this->assertEquals(Cache::settings('sessions'), $result['settings']);
$this->assertNotEquals(Cache::settings('default'), $result['settings']);
}
/**
* testReadAndWriteCache method
*
* @return void
*/
public function testReadAndWriteCache() {
Cache::config('default');
$result = Cache::write(null, 'here', 'file_test');
$this->assertFalse($result);
Cache::set(array('duration' => 1), 'file_test');
$result = Cache::read('test', 'file_test');
$expecting = '';
$this->assertEquals($expecting, $result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('test', $data, 'file_test');
$this->assertTrue(file_exists(CACHE . 'cake_test'));
$result = Cache::read('test', 'file_test');
$expecting = $data;
$this->assertEquals($expecting, $result);
Cache::delete('test', 'file_test');
}
/**
* Test read/write on the same cache key. Ensures file handles are re-wound.
*
* @return void
*/
public function testConsecutiveReadWrite() {
Cache::write('rw', 'first write', 'file_test');
$result = Cache::read('rw', 'file_test');
Cache::write('rw', 'second write', 'file_test');
$resultB = Cache::read('rw', 'file_test');
Cache::delete('rw', 'file_test');
$this->assertEquals('first write', $result);
$this->assertEquals('second write', $resultB);
}
/**
* testExpiry method
*
* @return void
*/
public function testExpiry() {
Cache::set(array('duration' => 1), 'file_test');
$result = Cache::read('test', 'file_test');
$this->assertFalse($result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 'file_test');
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test', 'file_test');
$this->assertFalse($result);
Cache::set(array('duration' => "+1 second"), 'file_test');
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 'file_test');
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test', 'file_test');
$this->assertFalse($result);
}
/**
* testDeleteCache method
*
* @return void
*/
public function testDeleteCache() {
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('delete_test', $data, 'file_test');
$this->assertTrue($result);
$result = Cache::delete('delete_test', 'file_test');
$this->assertTrue($result);
$this->assertFalse(file_exists(TMP . 'tests' . DS . 'delete_test'));
$result = Cache::delete('delete_test', 'file_test');
$this->assertFalse($result);
}
/**
* testSerialize method
*
* @return void
*/
public function testSerialize() {
Cache::config('file_test', array('engine' => 'File', 'serialize' => true));
$data = 'this is a test of the emergency broadcasting system';
$write = Cache::write('serialize_test', $data, 'file_test');
$this->assertTrue($write);
Cache::config('file_test', array('serialize' => false));
$read = Cache::read('serialize_test', 'file_test');
$newread = Cache::read('serialize_test', 'file_test');
Cache::delete('serialize_test', 'file_test');
$this->assertSame($read, serialize($data));
$this->assertSame(unserialize($newread), $data);
}
/**
* testClear method
*
* @return void
*/
public function testClear() {
Cache::config('file_test', array('engine' => 'File', 'duration' => 1));
$data = 'this is a test of the emergency broadcasting system';
Cache::write('serialize_test1', $data, 'file_test');
Cache::write('serialize_test2', $data, 'file_test');
Cache::write('serialize_test3', $data, 'file_test');
$this->assertTrue(file_exists(CACHE . 'cake_serialize_test1'));
$this->assertTrue(file_exists(CACHE . 'cake_serialize_test2'));
$this->assertTrue(file_exists(CACHE . 'cake_serialize_test3'));
sleep(2);
$result = Cache::clear(true, 'file_test');
$this->assertTrue($result);
$this->assertFalse(file_exists(CACHE . 'cake_serialize_test1'));
$this->assertFalse(file_exists(CACHE . 'cake_serialize_test2'));
$this->assertFalse(file_exists(CACHE . 'cake_serialize_test3'));
$data = 'this is a test of the emergency broadcasting system';
Cache::write('serialize_test1', $data, 'file_test');
Cache::write('serialize_test2', $data, 'file_test');
Cache::write('serialize_test3', $data, 'file_test');
$this->assertTrue(file_exists(CACHE . 'cake_serialize_test1'));
$this->assertTrue(file_exists(CACHE . 'cake_serialize_test2'));
$this->assertTrue(file_exists(CACHE . 'cake_serialize_test3'));
$result = Cache::clear(false, 'file_test');
$this->assertTrue($result);
$this->assertFalse(file_exists(CACHE . 'cake_serialize_test1'));
$this->assertFalse(file_exists(CACHE . 'cake_serialize_test2'));
$this->assertFalse(file_exists(CACHE . 'cake_serialize_test3'));
}
/**
* test that clear() doesn't wipe files not in the current engine's prefix.
*
* @return void
*/
public function testClearWithPrefixes() {
$FileOne = new FileEngine();
$FileOne->init(array(
'prefix' => 'prefix_one_',
'duration' => DAY
));
$FileTwo = new FileEngine();
$FileTwo->init(array(
'prefix' => 'prefix_two_',
'duration' => DAY
));
$dataOne = $dataTwo = $expected = 'content to cache';
$FileOne->write('prefix_one_key_one', $dataOne, DAY);
$FileTwo->write('prefix_two_key_two', $dataTwo, DAY);
$this->assertEquals($expected, $FileOne->read('prefix_one_key_one'));
$this->assertEquals($expected, $FileTwo->read('prefix_two_key_two'));
$FileOne->clear(false);
$this->assertEquals($expected, $FileTwo->read('prefix_two_key_two'), 'secondary config was cleared by accident.');
$FileTwo->clear(false);
}
/**
* Test that clear() also removes files with group tags.
*
* @return void
*/
public function testClearWithGroups() {
$engine = new FileEngine();
$engine->init(array(
'prefix' => 'cake_test_',
'duration' => DAY,
'groups' => array('short', 'round')
));
$key = 'cake_test_test_key';
$engine->write($key, 'it works', DAY);
$engine->clear(false);
$this->assertFalse($engine->read($key), 'Key should have been removed');
}
/**
* Test that clear() also removes files with group tags.
*
* @return void
*/
public function testClearWithNoKeys() {
$engine = new FileEngine();
$engine->init(array(
'prefix' => 'cake_test_',
'duration' => DAY,
'groups' => array('one', 'two')
));
$key = 'cake_test_test_key';
$engine->clear(false);
$this->assertFalse($engine->read($key), 'No errors should be found');
}
/**
* testKeyPath method
*
* @return void
*/
public function testKeyPath() {
$result = Cache::write('views.countries.something', 'here', 'file_test');
$this->assertTrue($result);
$this->assertTrue(file_exists(CACHE . 'cake_views_countries_something'));
$result = Cache::read('views.countries.something', 'file_test');
$this->assertEquals('here', $result);
$result = Cache::clear(false, 'file_test');
$this->assertTrue($result);
$result = Cache::write('domain.test.com:8080', 'here', 'file_test');
$this->assertTrue($result);
$this->assertTrue(file_exists(CACHE . 'cake_domain_test_com_8080'));
$result = Cache::write('command>dir|more', 'here', 'file_test');
$this->assertTrue($result);
$this->assertTrue(file_exists(CACHE . 'cake_command_dir_more'));
}
/**
* testRemoveWindowsSlashesFromCache method
*
* @return void
*/
public function testRemoveWindowsSlashesFromCache() {
Cache::config('windows_test', array('engine' => 'File', 'isWindows' => true, 'prefix' => null, 'path' => TMP));
$expected = array(
'C:\dev\prj2\sites\cake\libs' => array(
0 => 'C:\dev\prj2\sites\cake\libs', 1 => 'C:\dev\prj2\sites\cake\libs\view',
2 => 'C:\dev\prj2\sites\cake\libs\view\scaffolds', 3 => 'C:\dev\prj2\sites\cake\libs\view\pages',
4 => 'C:\dev\prj2\sites\cake\libs\view\layouts', 5 => 'C:\dev\prj2\sites\cake\libs\view\layouts\xml',
6 => 'C:\dev\prj2\sites\cake\libs\view\layouts\rss', 7 => 'C:\dev\prj2\sites\cake\libs\view\layouts\js',
8 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email', 9 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\text',
10 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\html', 11 => 'C:\dev\prj2\sites\cake\libs\view\helpers',
12 => 'C:\dev\prj2\sites\cake\libs\view\errors', 13 => 'C:\dev\prj2\sites\cake\libs\view\elements',
14 => 'C:\dev\prj2\sites\cake\libs\view\elements\email', 15 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\text',
16 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\html', 17 => 'C:\dev\prj2\sites\cake\libs\model',
18 => 'C:\dev\prj2\sites\cake\libs\model\datasources', 19 => 'C:\dev\prj2\sites\cake\libs\model\datasources\dbo',
20 => 'C:\dev\prj2\sites\cake\libs\model\behaviors', 21 => 'C:\dev\prj2\sites\cake\libs\controller',
22 => 'C:\dev\prj2\sites\cake\libs\controller\components', 23 => 'C:\dev\prj2\sites\cake\libs\cache'),
'C:\dev\prj2\sites\main_site\vendors' => array(
0 => 'C:\dev\prj2\sites\main_site\vendors', 1 => 'C:\dev\prj2\sites\main_site\vendors\shells',
2 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates', 3 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates\cdc_project',
4 => 'C:\dev\prj2\sites\main_site\vendors\shells\tasks', 5 => 'C:\dev\prj2\sites\main_site\vendors\js',
6 => 'C:\dev\prj2\sites\main_site\vendors\css'),
'C:\dev\prj2\sites\vendors' => array(
0 => 'C:\dev\prj2\sites\vendors', 1 => 'C:\dev\prj2\sites\vendors\simpletest',
2 => 'C:\dev\prj2\sites\vendors\simpletest\test', 3 => 'C:\dev\prj2\sites\vendors\simpletest\test\support',
4 => 'C:\dev\prj2\sites\vendors\simpletest\test\support\collector', 5 => 'C:\dev\prj2\sites\vendors\simpletest\extensions',
6 => 'C:\dev\prj2\sites\vendors\simpletest\extensions\testdox', 7 => 'C:\dev\prj2\sites\vendors\simpletest\docs',
8 => 'C:\dev\prj2\sites\vendors\simpletest\docs\fr', 9 => 'C:\dev\prj2\sites\vendors\simpletest\docs\en'),
'C:\dev\prj2\sites\main_site\views\helpers' => array(
0 => 'C:\dev\prj2\sites\main_site\views\helpers')
);
Cache::write('test_dir_map', $expected, 'windows_test');
$data = Cache::read('test_dir_map', 'windows_test');
Cache::delete('test_dir_map', 'windows_test');
$this->assertEquals($expected, $data);
Cache::drop('windows_test');
}
/**
* testWriteQuotedString method
*
* @return void
*/
public function testWriteQuotedString() {
Cache::config('file_test', array('engine' => 'File', 'path' => TMP . 'tests'));
Cache::write('App.doubleQuoteTest', '"this is a quoted string"', 'file_test');
$this->assertSame(Cache::read('App.doubleQuoteTest', 'file_test'), '"this is a quoted string"');
Cache::write('App.singleQuoteTest', "'this is a quoted string'", 'file_test');
$this->assertSame(Cache::read('App.singleQuoteTest', 'file_test'), "'this is a quoted string'");
Cache::config('file_test', array('isWindows' => true, 'path' => TMP . 'tests'));
$this->assertSame(Cache::read('App.doubleQuoteTest', 'file_test'), '"this is a quoted string"');
Cache::write('App.singleQuoteTest', "'this is a quoted string'", 'file_test');
$this->assertSame(Cache::read('App.singleQuoteTest', 'file_test'), "'this is a quoted string'");
Cache::delete('App.singleQuoteTest', 'file_test');
Cache::delete('App.doubleQuoteTest', 'file_test');
}
/**
* check that FileEngine does not generate an error when a configured Path does not exist in debug mode.
*
* @return void
*/
public function testPathDoesNotExist() {
$this->skipIf(is_dir(TMP . 'tests' . DS . 'autocreate'), 'Cannot run if test directory exists.');
Cache::config('autocreate', array(
'engine' => 'File',
'path' => TMP . 'tests' . DS . 'autocreate'
));
Cache::drop('autocreate');
}
/**
* Testing the mask setting in FileEngine
*
* @return void
*/
public function testMaskSetting() {
if (DS === '\\') {
$this->markTestSkipped('File permission testing does not work on Windows.');
}
Cache::config('mask_test', array('engine' => 'File', 'path' => TMP . 'tests'));
$data = 'This is some test content';
$write = Cache::write('masking_test', $data, 'mask_test');
$result = substr(sprintf('%o', fileperms(TMP . 'tests' . DS . 'cake_masking_test')), -4);
$expected = '0664';
$this->assertEquals($expected, $result);
Cache::delete('masking_test', 'mask_test');
Cache::drop('mask_test');
Cache::config('mask_test', array('engine' => 'File', 'mask' => 0666, 'path' => TMP . 'tests'));
Cache::write('masking_test', $data, 'mask_test');
$result = substr(sprintf('%o', fileperms(TMP . 'tests' . DS . 'cake_masking_test')), -4);
$expected = '0666';
$this->assertEquals($expected, $result);
Cache::delete('masking_test', 'mask_test');
Cache::drop('mask_test');
Cache::config('mask_test', array('engine' => 'File', 'mask' => 0644, 'path' => TMP . 'tests'));
Cache::write('masking_test', $data, 'mask_test');
$result = substr(sprintf('%o', fileperms(TMP . 'tests' . DS . 'cake_masking_test')), -4);
$expected = '0644';
$this->assertEquals($expected, $result);
Cache::delete('masking_test', 'mask_test');
Cache::drop('mask_test');
Cache::config('mask_test', array('engine' => 'File', 'mask' => 0640, 'path' => TMP . 'tests'));
Cache::write('masking_test', $data, 'mask_test');
$result = substr(sprintf('%o', fileperms(TMP . 'tests' . DS . 'cake_masking_test')), -4);
$expected = '0640';
$this->assertEquals($expected, $result);
Cache::delete('masking_test', 'mask_test');
Cache::drop('mask_test');
}
/**
* Tests that configuring groups for stored keys return the correct values when read/written
*
* @return void
*/
public function testGroupsReadWrite() {
Cache::config('file_groups', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_a', 'group_b')));
$this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'file_groups'));
$this->assertTrue(Cache::write('test_groups2', 'value2', 'file_groups'));
$this->assertTrue(Cache::write('test_groups3', 'value3', 'file_groups'));
}
/**
* Test that clearing with repeat writes works properly
*
* @return void
*/
public function testClearingWithRepeatWrites() {
Cache::config('repeat', array(
'engine' => 'File', 'groups' => array('users')
));
$this->assertTrue(Cache::write('user', 'rchavik', 'repeat'));
$this->assertEquals('rchavik', Cache::read('user', 'repeat'));
Cache::delete('user', 'repeat');
$this->assertEquals(false, Cache::read('user', 'repeat'));
$this->assertTrue(Cache::write('user', 'ADmad', 'repeat'));
$this->assertEquals('ADmad', Cache::read('user', 'repeat'));
Cache::clearGroup('users', 'repeat');
$this->assertEquals(false, Cache::read('user', 'repeat'));
$this->assertTrue(Cache::write('user', 'markstory', 'repeat'));
$this->assertEquals('markstory', Cache::read('user', 'repeat'));
Cache::drop('repeat');
}
/**
* Tests that deleting from a groups-enabled config is possible
*
* @return void
*/
public function testGroupDelete() {
Cache::config('file_groups', array(
'engine' => 'File',
'duration' => 3600,
'groups' => array('group_a', 'group_b')
));
$this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'file_groups'));
$this->assertTrue(Cache::delete('test_groups', 'file_groups'));
$this->assertFalse(Cache::read('test_groups', 'file_groups'));
}
/**
* Test clearing a cache group
*
* @return void
*/
public function testGroupClear() {
Cache::config('file_groups', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_a', 'group_b')));
Cache::config('file_groups2', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_b')));
Cache::config('file_groups3', array(
'engine' => 'File',
'duration' => 3600,
'groups' => array('group_b'),
'prefix' => 'leading_',
));
$this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
$this->assertTrue(Cache::write('test_groups2', 'value 2', 'file_groups2'));
$this->assertTrue(Cache::write('test_groups3', 'value 3', 'file_groups3'));
$this->assertTrue(Cache::clearGroup('group_b', 'file_groups'));
$this->assertFalse(Cache::read('test_groups', 'file_groups'));
$this->assertFalse(Cache::read('test_groups2', 'file_groups2'));
$this->assertEquals('value 3', Cache::read('test_groups3', 'file_groups3'));
$this->assertTrue(Cache::write('test_groups4', 'value', 'file_groups'));
$this->assertTrue(Cache::write('test_groups5', 'value 2', 'file_groups2'));
$this->assertTrue(Cache::write('test_groups6', 'value 3', 'file_groups3'));
$this->assertTrue(Cache::clearGroup('group_b', 'file_groups'));
$this->assertFalse(Cache::read('test_groups4', 'file_groups'));
$this->assertFalse(Cache::read('test_groups5', 'file_groups2'));
$this->assertEquals('value 3', Cache::read('test_groups6', 'file_groups3'));
}
/**
* Test that clearGroup works with no prefix.
*
* @return void
*/
public function testGroupClearNoPrefix() {
Cache::config('file_groups', array(
'engine' => 'File',
'duration' => 3600,
'prefix' => '',
'groups' => array('group_a', 'group_b')
));
Cache::write('key_1', 'value', 'file_groups');
Cache::write('key_2', 'value', 'file_groups');
Cache::clearGroup('group_a', 'file_groups');
$this->assertFalse(Cache::read('key_1', 'file_groups'), 'Did not delete');
$this->assertFalse(Cache::read('key_2', 'file_groups'), 'Did not delete');
}
}

View file

@ -0,0 +1,482 @@
<?php
/**
* MemcacheEngineTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Cache.Engine
* @since CakePHP(tm) v 1.2.0.5434
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Cache', 'Cache');
App::uses('MemcacheEngine', 'Cache/Engine');
/**
* Class TestMemcacheEngine
*
* @package Cake.Test.Case.Cache.Engine
*/
class TestMemcacheEngine extends MemcacheEngine {
/**
* public accessor to _parseServerString
*
* @param string $server
* @return array
*/
public function parseServerString($server) {
return $this->_parseServerString($server);
}
public function setMemcache($memcache) {
$this->_Memcache = $memcache;
}
}
/**
* MemcacheEngineTest class
*
* @package Cake.Test.Case.Cache.Engine
*/
class MemcacheEngineTest extends CakeTestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->skipIf(!class_exists('Memcache'), 'Memcache is not installed or configured properly.');
$this->_cacheDisable = Configure::read('Cache.disable');
Configure::write('Cache.disable', false);
Cache::config('memcache', array(
'engine' => 'Memcache',
'prefix' => 'cake_',
'duration' => 3600
));
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
Configure::write('Cache.disable', $this->_cacheDisable);
Cache::drop('memcache');
Cache::drop('memcache_groups');
Cache::drop('memcache_helper');
Cache::config('default');
}
/**
* testSettings method
*
* @return void
*/
public function testSettings() {
$settings = Cache::settings('memcache');
unset($settings['serialize'], $settings['path']);
$expecting = array(
'prefix' => 'cake_',
'duration' => 3600,
'probability' => 100,
'servers' => array('127.0.0.1'),
'persistent' => true,
'compress' => false,
'engine' => 'Memcache',
'groups' => array()
);
$this->assertEquals($expecting, $settings);
}
/**
* testSettings method
*
* @return void
*/
public function testMultipleServers() {
$servers = array('127.0.0.1:11211', '127.0.0.1:11222');
$available = true;
$Memcache = new Memcache();
foreach ($servers as $server) {
list($host, $port) = explode(':', $server);
//@codingStandardsIgnoreStart
if (!@$Memcache->connect($host, $port)) {
$available = false;
}
//@codingStandardsIgnoreEnd
}
$this->skipIf(!$available, 'Need memcache servers at ' . implode(', ', $servers) . ' to run this test.');
$Memcache = new MemcacheEngine();
$Memcache->init(array('engine' => 'Memcache', 'servers' => $servers));
$settings = $Memcache->settings();
$this->assertEquals($settings['servers'], $servers);
Cache::drop('dual_server');
}
/**
* testConnect method
*
* @return void
*/
public function testConnect() {
$Memcache = new MemcacheEngine();
$Memcache->init(Cache::settings('memcache'));
$result = $Memcache->connect('127.0.0.1');
$this->assertTrue($result);
}
/**
* test connecting to an ipv6 server.
*
* @return void
*/
public function testConnectIpv6() {
$Memcache = new MemcacheEngine();
$result = $Memcache->init(array(
'prefix' => 'cake_',
'duration' => 200,
'engine' => 'Memcache',
'servers' => array(
'[::1]:11211'
)
));
$this->assertTrue($result);
}
/**
* test non latin domains.
*
* @return void
*/
public function testParseServerStringNonLatin() {
$Memcache = new TestMemcacheEngine();
$result = $Memcache->parseServerString('schülervz.net:13211');
$this->assertEquals(array('schülervz.net', '13211'), $result);
$result = $Memcache->parseServerString('sülül:1111');
$this->assertEquals(array('sülül', '1111'), $result);
}
/**
* test unix sockets.
*
* @return void
*/
public function testParseServerStringUnix() {
$Memcache = new TestMemcacheEngine();
$result = $Memcache->parseServerString('unix:///path/to/memcached.sock');
$this->assertEquals(array('unix:///path/to/memcached.sock', 0), $result);
}
/**
* testReadAndWriteCache method
*
* @return void
*/
public function testReadAndWriteCache() {
Cache::set(array('duration' => 1), null, 'memcache');
$result = Cache::read('test', 'memcache');
$expecting = '';
$this->assertEquals($expecting, $result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('test', $data, 'memcache');
$this->assertTrue($result);
$result = Cache::read('test', 'memcache');
$expecting = $data;
$this->assertEquals($expecting, $result);
Cache::delete('test', 'memcache');
}
/**
* testExpiry method
*
* @return void
*/
public function testExpiry() {
Cache::set(array('duration' => 1), 'memcache');
$result = Cache::read('test', 'memcache');
$this->assertFalse($result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 'memcache');
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test', 'memcache');
$this->assertFalse($result);
Cache::set(array('duration' => "+1 second"), 'memcache');
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 'memcache');
$this->assertTrue($result);
sleep(3);
$result = Cache::read('other_test', 'memcache');
$this->assertFalse($result);
Cache::config('memcache', array('duration' => '+1 second'));
$result = Cache::read('other_test', 'memcache');
$this->assertFalse($result);
Cache::config('memcache', array('duration' => '+29 days'));
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('long_expiry_test', $data, 'memcache');
$this->assertTrue($result);
sleep(2);
$result = Cache::read('long_expiry_test', 'memcache');
$expecting = $data;
$this->assertEquals($expecting, $result);
Cache::config('memcache', array('duration' => 3600));
}
/**
* testDeleteCache method
*
* @return void
*/
public function testDeleteCache() {
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('delete_test', $data, 'memcache');
$this->assertTrue($result);
$result = Cache::delete('delete_test', 'memcache');
$this->assertTrue($result);
}
/**
* testDecrement method
*
* @return void
*/
public function testDecrement() {
$result = Cache::write('test_decrement', 5, 'memcache');
$this->assertTrue($result);
$result = Cache::decrement('test_decrement', 1, 'memcache');
$this->assertEquals(4, $result);
$result = Cache::read('test_decrement', 'memcache');
$this->assertEquals(4, $result);
$result = Cache::decrement('test_decrement', 2, 'memcache');
$this->assertEquals(2, $result);
$result = Cache::read('test_decrement', 'memcache');
$this->assertEquals(2, $result);
}
/**
* testIncrement method
*
* @return void
*/
public function testIncrement() {
$result = Cache::write('test_increment', 5, 'memcache');
$this->assertTrue($result);
$result = Cache::increment('test_increment', 1, 'memcache');
$this->assertEquals(6, $result);
$result = Cache::read('test_increment', 'memcache');
$this->assertEquals(6, $result);
$result = Cache::increment('test_increment', 2, 'memcache');
$this->assertEquals(8, $result);
$result = Cache::read('test_increment', 'memcache');
$this->assertEquals(8, $result);
}
/**
* test that configurations don't conflict, when a file engine is declared after a memcache one.
*
* @return void
*/
public function testConfigurationConflict() {
Cache::config('long_memcache', array(
'engine' => 'Memcache',
'duration' => '+2 seconds',
'servers' => array('127.0.0.1:11211'),
));
Cache::config('short_memcache', array(
'engine' => 'Memcache',
'duration' => '+1 seconds',
'servers' => array('127.0.0.1:11211'),
));
Cache::config('some_file', array('engine' => 'File'));
$this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcache'));
$this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcache'));
$this->assertEquals('yay', Cache::read('duration_test', 'long_memcache'), 'Value was not read %s');
$this->assertEquals('boo', Cache::read('short_duration_test', 'short_memcache'), 'Value was not read %s');
sleep(1);
$this->assertEquals('yay', Cache::read('duration_test', 'long_memcache'), 'Value was not read %s');
sleep(2);
$this->assertFalse(Cache::read('short_duration_test', 'short_memcache'), 'Cache was not invalidated %s');
$this->assertFalse(Cache::read('duration_test', 'long_memcache'), 'Value did not expire %s');
Cache::delete('duration_test', 'long_memcache');
Cache::delete('short_duration_test', 'short_memcache');
}
/**
* test clearing memcache.
*
* @return void
*/
public function testClear() {
Cache::config('memcache2', array(
'engine' => 'Memcache',
'prefix' => 'cake2_',
'duration' => 3600
));
Cache::write('some_value', 'cache1', 'memcache');
$result = Cache::clear(true, 'memcache');
$this->assertTrue($result);
$this->assertEquals('cache1', Cache::read('some_value', 'memcache'));
Cache::write('some_value', 'cache2', 'memcache2');
$result = Cache::clear(false, 'memcache');
$this->assertTrue($result);
$this->assertFalse(Cache::read('some_value', 'memcache'));
$this->assertEquals('cache2', Cache::read('some_value', 'memcache2'));
Cache::clear(false, 'memcache2');
}
/**
* test that a 0 duration can successfully write.
*
* @return void
*/
public function testZeroDuration() {
Cache::config('memcache', array('duration' => 0));
$result = Cache::write('test_key', 'written!', 'memcache');
$this->assertTrue($result);
$result = Cache::read('test_key', 'memcache');
$this->assertEquals('written!', $result);
}
/**
* test that durations greater than 30 days never expire
*
* @return void
*/
public function testLongDurationEqualToZero() {
$memcache = new TestMemcacheEngine();
$memcache->settings['compress'] = false;
$mock = $this->getMock('Memcache');
$memcache->setMemcache($mock);
$mock->expects($this->once())
->method('set')
->with('key', 'value', false, 0);
$value = 'value';
$memcache->write('key', $value, 50 * DAY);
}
/**
* Tests that configuring groups for stored keys return the correct values when read/written
* Shows that altering the group value is equivalent to deleting all keys under the same
* group
*
* @return void
*/
public function testGroupReadWrite() {
Cache::config('memcache_groups', array(
'engine' => 'Memcache',
'duration' => 3600,
'groups' => array('group_a', 'group_b'),
'prefix' => 'test_'
));
Cache::config('memcache_helper', array(
'engine' => 'Memcache',
'duration' => 3600,
'prefix' => 'test_'
));
$this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'memcache_groups'));
Cache::increment('group_a', 1, 'memcache_helper');
$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
$this->assertTrue(Cache::write('test_groups', 'value2', 'memcache_groups'));
$this->assertEquals('value2', Cache::read('test_groups', 'memcache_groups'));
Cache::increment('group_b', 1, 'memcache_helper');
$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
$this->assertTrue(Cache::write('test_groups', 'value3', 'memcache_groups'));
$this->assertEquals('value3', Cache::read('test_groups', 'memcache_groups'));
}
/**
* Tests that deleteing from a groups-enabled config is possible
*
* @return void
*/
public function testGroupDelete() {
Cache::config('memcache_groups', array(
'engine' => 'Memcache',
'duration' => 3600,
'groups' => array('group_a', 'group_b')
));
$this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'memcache_groups'));
$this->assertTrue(Cache::delete('test_groups', 'memcache_groups'));
$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
}
/**
* Test clearing a cache group
*
* @return void
*/
public function testGroupClear() {
Cache::config('memcache_groups', array(
'engine' => 'Memcache',
'duration' => 3600,
'groups' => array('group_a', 'group_b')
));
$this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
$this->assertTrue(Cache::clearGroup('group_a', 'memcache_groups'));
$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
$this->assertTrue(Cache::write('test_groups', 'value2', 'memcache_groups'));
$this->assertTrue(Cache::clearGroup('group_b', 'memcache_groups'));
$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
}
}

View file

@ -0,0 +1,776 @@
<?php
/**
* MemcachedEngineTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Cache.Engine
* @since CakePHP(tm) v 2.5.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Cache', 'Cache');
App::uses('MemcachedEngine', 'Cache/Engine');
/**
* Class TestMemcachedEngine
*
* @package Cake.Test.Case.Cache.Engine
*/
class TestMemcachedEngine extends MemcachedEngine {
/**
* public accessor to _parseServerString
*
* @param string $server
* @return array
*/
public function parseServerString($server) {
return $this->_parseServerString($server);
}
public function setMemcached($memcached) {
$this->_Memcached = $memcached;
}
public function getMemcached() {
return $this->_Memcached;
}
}
/**
* MemcachedEngineTest class
*
* @package Cake.Test.Case.Cache.Engine
*/
class MemcachedEngineTest extends CakeTestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->skipIf(!class_exists('Memcached'), 'Memcached is not installed or configured properly.');
Cache::config('memcached', array(
'engine' => 'Memcached',
'prefix' => 'cake_',
'duration' => 3600
));
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
Cache::drop('memcached');
Cache::drop('memcached_groups');
Cache::drop('memcached_helper');
Cache::config('default');
}
/**
* testSettings method
*
* @return void
*/
public function testSettings() {
$settings = Cache::settings('memcached');
unset($settings['path']);
$expecting = array(
'prefix' => 'cake_',
'duration' => 3600,
'probability' => 100,
'servers' => array('127.0.0.1'),
'persistent' => false,
'compress' => false,
'engine' => 'Memcached',
'login' => null,
'password' => null,
'groups' => array(),
'serialize' => 'php'
);
$this->assertEquals($expecting, $settings);
}
/**
* testCompressionSetting method
*
* @return void
*/
public function testCompressionSetting() {
$Memcached = new TestMemcachedEngine();
$Memcached->init(array(
'engine' => 'Memcached',
'servers' => array('127.0.0.1:11211'),
'compress' => false
));
$this->assertFalse($Memcached->getMemcached()->getOption(Memcached::OPT_COMPRESSION));
$MemcachedCompressed = new TestMemcachedEngine();
$MemcachedCompressed->init(array(
'engine' => 'Memcached',
'servers' => array('127.0.0.1:11211'),
'compress' => true
));
$this->assertTrue($MemcachedCompressed->getMemcached()->getOption(Memcached::OPT_COMPRESSION));
}
/**
* test accepts only valid serializer engine
*
* @return void
*/
public function testInvalidSerializerSetting() {
$Memcached = new TestMemcachedEngine();
$settings = array(
'engine' => 'Memcached',
'servers' => array('127.0.0.1:11211'),
'persistent' => false,
'serialize' => 'invalid_serializer'
);
$this->setExpectedException(
'CacheException', 'invalid_serializer is not a valid serializer engine for Memcached'
);
$Memcached->init($settings);
}
/**
* testPhpSerializerSetting method
*
* @return void
*/
public function testPhpSerializerSetting() {
$Memcached = new TestMemcachedEngine();
$settings = array(
'engine' => 'Memcached',
'servers' => array('127.0.0.1:11211'),
'persistent' => false,
'serialize' => 'php'
);
$Memcached->init($settings);
$this->assertEquals(Memcached::SERIALIZER_PHP, $Memcached->getMemcached()->getOption(Memcached::OPT_SERIALIZER));
}
/**
* testJsonSerializerSetting method
*
* @return void
*/
public function testJsonSerializerSetting() {
$this->skipIf(
!Memcached::HAVE_JSON,
'Memcached extension is not compiled with json support'
);
$Memcached = new TestMemcachedEngine();
$settings = array(
'engine' => 'Memcached',
'servers' => array('127.0.0.1:11211'),
'persistent' => false,
'serialize' => 'json'
);
$Memcached->init($settings);
$this->assertEquals(Memcached::SERIALIZER_JSON, $Memcached->getMemcached()->getOption(Memcached::OPT_SERIALIZER));
}
/**
* testIgbinarySerializerSetting method
*
* @return void
*/
public function testIgbinarySerializerSetting() {
$this->skipIf(
!Memcached::HAVE_IGBINARY,
'Memcached extension is not compiled with igbinary support'
);
$Memcached = new TestMemcachedEngine();
$settings = array(
'engine' => 'Memcached',
'servers' => array('127.0.0.1:11211'),
'persistent' => false,
'serialize' => 'igbinary'
);
$Memcached->init($settings);
$this->assertEquals(Memcached::SERIALIZER_IGBINARY, $Memcached->getMemcached()->getOption(Memcached::OPT_SERIALIZER));
}
/**
* testMsgpackSerializerSetting method
*
* @return void
*/
public function testMsgpackSerializerSetting() {
$this->skipIf(
!defined('Memcached::HAVE_MSGPACK') || !Memcached::HAVE_MSGPACK,
'Memcached extension is not compiled with msgpack support'
);
$Memcached = new TestMemcachedEngine();
$settings = array(
'engine' => 'Memcached',
'servers' => array('127.0.0.1:11211'),
'persistent' => false,
'serialize' => 'msgpack'
);
$Memcached->init($settings);
$this->assertEquals(Memcached::SERIALIZER_MSGPACK, $Memcached->getMemcached()->getOption(Memcached::OPT_SERIALIZER));
}
/**
* testJsonSerializerThrowException method
*
* @return void
*/
public function testJsonSerializerThrowException() {
$this->skipIf(
Memcached::HAVE_JSON,
'Memcached extension is compiled with json support'
);
$Memcached = new TestMemcachedEngine();
$settings = array(
'engine' => 'Memcached',
'servers' => array('127.0.0.1:11211'),
'persistent' => false,
'serialize' => 'json'
);
$this->setExpectedException(
'CacheException', 'Memcached extension is not compiled with json support'
);
$Memcached->init($settings);
}
/**
* testMsgpackSerializerThrowException method
*
* @return void
*/
public function testMsgpackSerializerThrowException() {
$this->skipIf(
defined('Memcached::HAVE_MSGPACK') && Memcached::HAVE_MSGPACK,
'Memcached extension is compiled with msgpack support'
);
$Memcached = new TestMemcachedEngine();
$settings = array(
'engine' => 'Memcached',
'servers' => array('127.0.0.1:11211'),
'persistent' => false,
'serialize' => 'msgpack'
);
$this->setExpectedException(
'CacheException', 'msgpack is not a valid serializer engine for Memcached'
);
$Memcached->init($settings);
}
/**
* testIgbinarySerializerThrowException method
*
* @return void
*/
public function testIgbinarySerializerThrowException() {
$this->skipIf(
Memcached::HAVE_IGBINARY,
'Memcached extension is compiled with igbinary support'
);
$Memcached = new TestMemcachedEngine();
$settings = array(
'engine' => 'Memcached',
'servers' => array('127.0.0.1:11211'),
'persistent' => false,
'serialize' => 'igbinary'
);
$this->setExpectedException(
'CacheException', 'Memcached extension is not compiled with igbinary support'
);
$Memcached->init($settings);
}
/**
* test using authentication without memcached installed with SASL support
* throw an exception
*
* @return void
*/
public function testSaslAuthException() {
$Memcached = new TestMemcachedEngine();
$settings = array(
'engine' => 'Memcached',
'servers' => array('127.0.0.1:11211'),
'persistent' => false,
'login' => 'test',
'password' => 'password'
);
$this->skipIf(
method_exists($Memcached->getMemcached(), 'setSaslAuthData'),
'Memcached extension is installed with SASL support'
);
$this->setExpectedException(
'CacheException', 'Memcached extension is not build with SASL support'
);
$Memcached->init($settings);
}
/**
* testSettings method
*
* @return void
*/
public function testMultipleServers() {
$servers = array('127.0.0.1:11211', '127.0.0.1:11222');
$available = true;
$Memcached = new Memcached();
foreach ($servers as $server) {
list($host, $port) = explode(':', $server);
//@codingStandardsIgnoreStart
if (!$Memcached->addServer($host, $port)) {
$available = false;
}
//@codingStandardsIgnoreEnd
}
$this->skipIf(!$available, 'Need memcached servers at ' . implode(', ', $servers) . ' to run this test.');
$Memcached = new MemcachedEngine();
$Memcached->init(array('engine' => 'Memcached', 'servers' => $servers));
$settings = $Memcached->settings();
$this->assertEquals($settings['servers'], $servers);
Cache::drop('dual_server');
}
/**
* test connecting to an ipv6 server.
*
* @return void
*/
public function testConnectIpv6() {
$Memcached = new MemcachedEngine();
$result = $Memcached->init(array(
'prefix' => 'cake_',
'duration' => 200,
'engine' => 'Memcached',
'servers' => array(
'[::1]:11211'
)
));
$this->assertTrue($result);
}
/**
* test non latin domains.
*
* @return void
*/
public function testParseServerStringNonLatin() {
$Memcached = new TestMemcachedEngine();
$result = $Memcached->parseServerString('schülervz.net:13211');
$this->assertEquals(array('schülervz.net', '13211'), $result);
$result = $Memcached->parseServerString('sülül:1111');
$this->assertEquals(array('sülül', '1111'), $result);
}
/**
* test unix sockets.
*
* @return void
*/
public function testParseServerStringUnix() {
$Memcached = new TestMemcachedEngine();
$result = $Memcached->parseServerString('unix:///path/to/memcachedd.sock');
$this->assertEquals(array('unix:///path/to/memcachedd.sock', 0), $result);
}
/**
* testReadAndWriteCache method
*
* @return void
*/
public function testReadAndWriteCache() {
Cache::set(array('duration' => 1), null, 'memcached');
$result = Cache::read('test', 'memcached');
$expecting = '';
$this->assertEquals($expecting, $result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('test', $data, 'memcached');
$this->assertTrue($result);
$result = Cache::read('test', 'memcached');
$expecting = $data;
$this->assertEquals($expecting, $result);
Cache::delete('test', 'memcached');
}
/**
* testExpiry method
*
* @return void
*/
public function testExpiry() {
Cache::set(array('duration' => 1), 'memcached');
$result = Cache::read('test', 'memcached');
$this->assertFalse($result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 'memcached');
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test', 'memcached');
$this->assertFalse($result);
Cache::set(array('duration' => "+1 second"), 'memcached');
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 'memcached');
$this->assertTrue($result);
sleep(3);
$result = Cache::read('other_test', 'memcached');
$this->assertFalse($result);
Cache::config('memcached', array('duration' => '+1 second'));
$result = Cache::read('other_test', 'memcached');
$this->assertFalse($result);
Cache::config('memcached', array('duration' => '+29 days'));
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('long_expiry_test', $data, 'memcached');
$this->assertTrue($result);
sleep(2);
$result = Cache::read('long_expiry_test', 'memcached');
$expecting = $data;
$this->assertEquals($expecting, $result);
Cache::config('memcached', array('duration' => 3600));
}
/**
* testDeleteCache method
*
* @return void
*/
public function testDeleteCache() {
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('delete_test', $data, 'memcached');
$this->assertTrue($result);
$result = Cache::delete('delete_test', 'memcached');
$this->assertTrue($result);
}
/**
* testDecrement method
*
* @return void
*/
public function testDecrement() {
$result = Cache::write('test_decrement', 5, 'memcached');
$this->assertTrue($result);
$result = Cache::decrement('test_decrement', 1, 'memcached');
$this->assertEquals(4, $result);
$result = Cache::read('test_decrement', 'memcached');
$this->assertEquals(4, $result);
$result = Cache::decrement('test_decrement', 2, 'memcached');
$this->assertEquals(2, $result);
$result = Cache::read('test_decrement', 'memcached');
$this->assertEquals(2, $result);
Cache::delete('test_decrement', 'memcached');
}
/**
* test decrementing compressed keys
*
* @return void
*/
public function testDecrementCompressedKeys() {
Cache::config('compressed_memcached', array(
'engine' => 'Memcached',
'duration' => '+2 seconds',
'servers' => array('127.0.0.1:11211'),
'compress' => true
));
$result = Cache::write('test_decrement', 5, 'compressed_memcached');
$this->assertTrue($result);
$result = Cache::decrement('test_decrement', 1, 'compressed_memcached');
$this->assertEquals(4, $result);
$result = Cache::read('test_decrement', 'compressed_memcached');
$this->assertEquals(4, $result);
$result = Cache::decrement('test_decrement', 2, 'compressed_memcached');
$this->assertEquals(2, $result);
$result = Cache::read('test_decrement', 'compressed_memcached');
$this->assertEquals(2, $result);
Cache::delete('test_decrement', 'compressed_memcached');
}
/**
* testIncrement method
*
* @return void
*/
public function testIncrement() {
$result = Cache::write('test_increment', 5, 'memcached');
$this->assertTrue($result);
$result = Cache::increment('test_increment', 1, 'memcached');
$this->assertEquals(6, $result);
$result = Cache::read('test_increment', 'memcached');
$this->assertEquals(6, $result);
$result = Cache::increment('test_increment', 2, 'memcached');
$this->assertEquals(8, $result);
$result = Cache::read('test_increment', 'memcached');
$this->assertEquals(8, $result);
Cache::delete('test_increment', 'memcached');
}
/**
* test incrementing compressed keys
*
* @return void
*/
public function testIncrementCompressedKeys() {
Cache::config('compressed_memcached', array(
'engine' => 'Memcached',
'duration' => '+2 seconds',
'servers' => array('127.0.0.1:11211'),
'compress' => true
));
$result = Cache::write('test_increment', 5, 'compressed_memcached');
$this->assertTrue($result);
$result = Cache::increment('test_increment', 1, 'compressed_memcached');
$this->assertEquals(6, $result);
$result = Cache::read('test_increment', 'compressed_memcached');
$this->assertEquals(6, $result);
$result = Cache::increment('test_increment', 2, 'compressed_memcached');
$this->assertEquals(8, $result);
$result = Cache::read('test_increment', 'compressed_memcached');
$this->assertEquals(8, $result);
Cache::delete('test_increment', 'compressed_memcached');
}
/**
* test that configurations don't conflict, when a file engine is declared after a memcached one.
*
* @return void
*/
public function testConfigurationConflict() {
Cache::config('long_memcached', array(
'engine' => 'Memcached',
'duration' => '+2 seconds',
'servers' => array('127.0.0.1:11211'),
));
Cache::config('short_memcached', array(
'engine' => 'Memcached',
'duration' => '+1 seconds',
'servers' => array('127.0.0.1:11211'),
));
Cache::config('some_file', array('engine' => 'File'));
$this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcached'));
$this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcached'));
$this->assertEquals('yay', Cache::read('duration_test', 'long_memcached'), 'Value was not read %s');
$this->assertEquals('boo', Cache::read('short_duration_test', 'short_memcached'), 'Value was not read %s');
sleep(1);
$this->assertEquals('yay', Cache::read('duration_test', 'long_memcached'), 'Value was not read %s');
sleep(2);
$this->assertFalse(Cache::read('short_duration_test', 'short_memcached'), 'Cache was not invalidated %s');
$this->assertFalse(Cache::read('duration_test', 'long_memcached'), 'Value did not expire %s');
Cache::delete('duration_test', 'long_memcached');
Cache::delete('short_duration_test', 'short_memcached');
}
/**
* test clearing memcached.
*
* @return void
*/
public function testClear() {
Cache::config('memcached2', array(
'engine' => 'Memcached',
'prefix' => 'cake2_',
'duration' => 3600
));
Cache::write('some_value', 'cache1', 'memcached');
$result = Cache::clear(true, 'memcached');
$this->assertTrue($result);
$this->assertEquals('cache1', Cache::read('some_value', 'memcached'));
Cache::write('some_value', 'cache2', 'memcached2');
$result = Cache::clear(false, 'memcached');
$this->assertTrue($result);
$this->assertFalse(Cache::read('some_value', 'memcached'));
$this->assertEquals('cache2', Cache::read('some_value', 'memcached2'));
Cache::clear(false, 'memcached2');
}
/**
* test that a 0 duration can successfully write.
*
* @return void
*/
public function testZeroDuration() {
Cache::config('memcached', array('duration' => 0));
$result = Cache::write('test_key', 'written!', 'memcached');
$this->assertTrue($result);
$result = Cache::read('test_key', 'memcached');
$this->assertEquals('written!', $result);
}
/**
* test that durations greater than 30 days never expire
*
* @return void
*/
public function testLongDurationEqualToZero() {
$memcached = new TestMemcachedEngine();
$memcached->settings['compress'] = false;
$mock = $this->getMock('Memcached');
$memcached->setMemcached($mock);
$mock->expects($this->once())
->method('set')
->with('key', 'value', 0);
$value = 'value';
$memcached->write('key', $value, 50 * DAY);
}
/**
* Tests that configuring groups for stored keys return the correct values when read/written
* Shows that altering the group value is equivalent to deleting all keys under the same
* group
*
* @return void
*/
public function testGroupReadWrite() {
Cache::config('memcached_groups', array(
'engine' => 'Memcached',
'duration' => 3600,
'groups' => array('group_a', 'group_b'),
'prefix' => 'test_'
));
Cache::config('memcached_helper', array(
'engine' => 'Memcached',
'duration' => 3600,
'prefix' => 'test_'
));
$this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'memcached_groups'));
Cache::increment('group_a', 1, 'memcached_helper');
$this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
$this->assertTrue(Cache::write('test_groups', 'value2', 'memcached_groups'));
$this->assertEquals('value2', Cache::read('test_groups', 'memcached_groups'));
Cache::increment('group_b', 1, 'memcached_helper');
$this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
$this->assertTrue(Cache::write('test_groups', 'value3', 'memcached_groups'));
$this->assertEquals('value3', Cache::read('test_groups', 'memcached_groups'));
}
/**
* Tests that deleteing from a groups-enabled config is possible
*
* @return void
*/
public function testGroupDelete() {
Cache::config('memcached_groups', array(
'engine' => 'Memcached',
'duration' => 3600,
'groups' => array('group_a', 'group_b')
));
$this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'memcached_groups'));
$this->assertTrue(Cache::delete('test_groups', 'memcached_groups'));
$this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
}
/**
* Test clearing a cache group
*
* @return void
*/
public function testGroupClear() {
Cache::config('memcached_groups', array(
'engine' => 'Memcached',
'duration' => 3600,
'groups' => array('group_a', 'group_b')
));
$this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups'));
$this->assertTrue(Cache::clearGroup('group_a', 'memcached_groups'));
$this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
$this->assertTrue(Cache::write('test_groups', 'value2', 'memcached_groups'));
$this->assertTrue(Cache::clearGroup('group_b', 'memcached_groups'));
$this->assertFalse(Cache::read('test_groups', 'memcached_groups'));
}
}

View file

@ -0,0 +1,383 @@
<?php
/**
* RedisEngineTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package Cake.Test.Case.Cache.Engine
* @since CakePHP(tm) v 2.2
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Cache', 'Cache');
App::uses('RedisEngine', 'Cache/Engine');
/**
* RedisEngineTest class
*
* @package Cake.Test.Case.Cache.Engine
*/
class RedisEngineTest extends CakeTestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->skipIf(!class_exists('Redis'), 'Redis is not installed or configured properly.');
$this->_cacheDisable = Configure::read('Cache.disable');
Configure::write('Cache.disable', false);
Cache::config('redis', array(
'engine' => 'Redis',
'prefix' => 'cake_',
'duration' => 3600
));
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
Configure::write('Cache.disable', $this->_cacheDisable);
Cache::drop('');
Cache::drop('redis_groups');
Cache::drop('redis_helper');
Cache::config('default');
}
/**
* testSettings method
*
* @return void
*/
public function testSettings() {
$settings = Cache::settings('redis');
$expecting = array(
'prefix' => 'cake_',
'duration' => 3600,
'probability' => 100,
'groups' => array(),
'engine' => 'Redis',
'server' => '127.0.0.1',
'port' => 6379,
'timeout' => 0,
'persistent' => true,
'password' => false,
'database' => 0,
);
$this->assertEquals($expecting, $settings);
}
/**
* testConnect method
*
* @return void
*/
public function testConnect() {
$Redis = new RedisEngine();
$this->assertTrue($Redis->init(Cache::settings('redis')));
}
/**
* testMultiDatabaseOperations method
*
* @return void
*/
public function testMultiDatabaseOperations() {
Cache::config('redisdb0', array(
'engine' => 'Redis',
'prefix' => 'cake2_',
'duration' => 3600,
'persistent' => false,
));
Cache::config('redisdb1', array(
'engine' => 'Redis',
'database' => 1,
'prefix' => 'cake2_',
'duration' => 3600,
'persistent' => false,
));
$result = Cache::write('save_in_0', true, 'redisdb0');
$exist = Cache::read('save_in_0', 'redisdb0');
$this->assertTrue($result);
$this->assertTrue($exist);
$result = Cache::write('save_in_1', true, 'redisdb1');
$this->assertTrue($result);
$exist = Cache::read('save_in_0', 'redisdb1');
$this->assertFalse($exist);
$exist = Cache::read('save_in_1', 'redisdb1');
$this->assertTrue($exist);
Cache::delete('save_in_0', 'redisdb0');
$exist = Cache::read('save_in_0', 'redisdb0');
$this->assertFalse($exist);
Cache::delete('save_in_1', 'redisdb1');
$exist = Cache::read('save_in_1', 'redisdb1');
$this->assertFalse($exist);
Cache::drop('redisdb0');
Cache::drop('redisdb1');
}
/**
* testReadAndWriteCache method
*
* @return void
*/
public function testReadAndWriteCache() {
Cache::set(array('duration' => 1), null, 'redis');
$result = Cache::read('test', 'redis');
$expecting = '';
$this->assertEquals($expecting, $result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('test', $data, 'redis');
$this->assertTrue($result);
$result = Cache::read('test', 'redis');
$expecting = $data;
$this->assertEquals($expecting, $result);
$data = array(1, 2, 3);
$this->assertTrue(Cache::write('array_data', $data, 'redis'));
$this->assertEquals($data, Cache::read('array_data', 'redis'));
Cache::delete('test', 'redis');
}
/**
* testExpiry method
*
* @return void
*/
public function testExpiry() {
Cache::set(array('duration' => 1), 'redis');
$result = Cache::read('test', 'redis');
$this->assertFalse($result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 'redis');
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test', 'redis');
$this->assertFalse($result);
Cache::set(array('duration' => "+1 second"), 'redis');
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 'redis');
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test', 'redis');
$this->assertFalse($result);
Cache::config('redis', array('duration' => '+1 second'));
sleep(2);
$result = Cache::read('other_test', 'redis');
$this->assertFalse($result);
Cache::config('redis', array('duration' => '+29 days'));
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('long_expiry_test', $data, 'redis');
$this->assertTrue($result);
sleep(2);
$result = Cache::read('long_expiry_test', 'redis');
$expecting = $data;
$this->assertEquals($expecting, $result);
Cache::config('redis', array('duration' => 3600));
}
/**
* testDeleteCache method
*
* @return void
*/
public function testDeleteCache() {
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('delete_test', $data, 'redis');
$this->assertTrue($result);
$result = Cache::delete('delete_test', 'redis');
$this->assertTrue($result);
}
/**
* testDecrement method
*
* @return void
*/
public function testDecrement() {
Cache::delete('test_decrement', 'redis');
$result = Cache::write('test_decrement', 5, 'redis');
$this->assertTrue($result);
$result = Cache::decrement('test_decrement', 1, 'redis');
$this->assertEquals(4, $result);
$result = Cache::read('test_decrement', 'redis');
$this->assertEquals(4, $result);
$result = Cache::decrement('test_decrement', 2, 'redis');
$this->assertEquals(2, $result);
$result = Cache::read('test_decrement', 'redis');
$this->assertEquals(2, $result);
}
/**
* testIncrement method
*
* @return void
*/
public function testIncrement() {
Cache::delete('test_increment', 'redis');
$result = Cache::increment('test_increment', 1, 'redis');
$this->assertEquals(1, $result);
$result = Cache::read('test_increment', 'redis');
$this->assertEquals(1, $result);
$result = Cache::increment('test_increment', 2, 'redis');
$this->assertEquals(3, $result);
$result = Cache::read('test_increment', 'redis');
$this->assertEquals(3, $result);
}
/**
* test clearing redis.
*
* @return void
*/
public function testClear() {
Cache::config('redis2', array(
'engine' => 'Redis',
'prefix' => 'cake2_',
'duration' => 3600
));
Cache::write('some_value', 'cache1', 'redis');
$result = Cache::clear(true, 'redis');
$this->assertTrue($result);
$this->assertEquals('cache1', Cache::read('some_value', 'redis'));
Cache::write('some_value', 'cache2', 'redis2');
$result = Cache::clear(false, 'redis');
$this->assertTrue($result);
$this->assertFalse(Cache::read('some_value', 'redis'));
$this->assertEquals('cache2', Cache::read('some_value', 'redis2'));
Cache::clear(false, 'redis2');
}
/**
* test that a 0 duration can successfully write.
*
* @return void
*/
public function testZeroDuration() {
Cache::config('redis', array('duration' => 0));
$result = Cache::write('test_key', 'written!', 'redis');
$this->assertTrue($result);
$result = Cache::read('test_key', 'redis');
$this->assertEquals('written!', $result);
}
/**
* Tests that configuring groups for stored keys return the correct values when read/written
* Shows that altering the group value is equivalent to deleting all keys under the same
* group
*
* @return void
*/
public function testGroupReadWrite() {
Cache::config('redis_groups', array(
'engine' => 'Redis',
'duration' => 3600,
'groups' => array('group_a', 'group_b'),
'prefix' => 'test_'
));
Cache::config('redis_helper', array(
'engine' => 'Redis',
'duration' => 3600,
'prefix' => 'test_'
));
$this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
Cache::increment('group_a', 1, 'redis_helper');
$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
$this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
$this->assertEquals('value2', Cache::read('test_groups', 'redis_groups'));
Cache::increment('group_b', 1, 'redis_helper');
$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
$this->assertTrue(Cache::write('test_groups', 'value3', 'redis_groups'));
$this->assertEquals('value3', Cache::read('test_groups', 'redis_groups'));
}
/**
* Tests that deleteing from a groups-enabled config is possible
*
* @return void
*/
public function testGroupDelete() {
Cache::config('redis_groups', array(
'engine' => 'Redis',
'duration' => 3600,
'groups' => array('group_a', 'group_b')
));
$this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'redis_groups'));
$this->assertTrue(Cache::delete('test_groups', 'redis_groups'));
$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
}
/**
* Test clearing a cache group
*
* @return void
*/
public function testGroupClear() {
Cache::config('redis_groups', array(
'engine' => 'Redis',
'duration' => 3600,
'groups' => array('group_a', 'group_b')
));
$this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups'));
$this->assertTrue(Cache::clearGroup('group_a', 'redis_groups'));
$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
$this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups'));
$this->assertTrue(Cache::clearGroup('group_b', 'redis_groups'));
$this->assertFalse(Cache::read('test_groups', 'redis_groups'));
}
}

View file

@ -0,0 +1,262 @@
<?php
/**
* WincacheEngineTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Cache.Engine
* @since CakePHP(tm) v 1.2.0.5434
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Cache', 'Cache');
/**
* WincacheEngineTest class
*
* @package Cake.Test.Case.Cache.Engine
*/
class WincacheEngineTest extends CakeTestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->skipIf(!function_exists('wincache_ucache_set'), 'Wincache is not installed or configured properly.');
$this->_cacheDisable = Configure::read('Cache.disable');
Configure::write('Cache.disable', false);
Cache::config('wincache', array('engine' => 'Wincache', 'prefix' => 'cake_'));
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
Configure::write('Cache.disable', $this->_cacheDisable);
Cache::drop('wincache');
Cache::drop('wincache_groups');
Cache::config('default');
}
/**
* testReadAndWriteCache method
*
* @return void
*/
public function testReadAndWriteCache() {
Cache::set(array('duration' => 1), 'wincache');
$result = Cache::read('test', 'wincache');
$expecting = '';
$this->assertEquals($expecting, $result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('test', $data, 'wincache');
$this->assertTrue($result);
$result = Cache::read('test', 'wincache');
$expecting = $data;
$this->assertEquals($expecting, $result);
Cache::delete('test', 'wincache');
}
/**
* testExpiry method
*
* @return void
*/
public function testExpiry() {
Cache::set(array('duration' => 1), 'wincache');
$result = Cache::read('test', 'wincache');
$this->assertFalse($result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 'wincache');
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test', 'wincache');
$this->assertFalse($result);
Cache::set(array('duration' => 1), 'wincache');
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 'wincache');
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test', 'wincache');
$this->assertFalse($result);
sleep(2);
$result = Cache::read('other_test', 'wincache');
$this->assertFalse($result);
}
/**
* testDeleteCache method
*
* @return void
*/
public function testDeleteCache() {
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('delete_test', $data, 'wincache');
$this->assertTrue($result);
$result = Cache::delete('delete_test', 'wincache');
$this->assertTrue($result);
}
/**
* testDecrement method
*
* @return void
*/
public function testDecrement() {
$this->skipIf(
!function_exists('wincache_ucache_dec'),
'No wincache_ucache_dec() function, cannot test decrement().'
);
$result = Cache::write('test_decrement', 5, 'wincache');
$this->assertTrue($result);
$result = Cache::decrement('test_decrement', 1, 'wincache');
$this->assertEquals(4, $result);
$result = Cache::read('test_decrement', 'wincache');
$this->assertEquals(4, $result);
$result = Cache::decrement('test_decrement', 2, 'wincache');
$this->assertEquals(2, $result);
$result = Cache::read('test_decrement', 'wincache');
$this->assertEquals(2, $result);
}
/**
* testIncrement method
*
* @return void
*/
public function testIncrement() {
$this->skipIf(
!function_exists('wincache_ucache_inc'),
'No wincache_inc() function, cannot test increment().'
);
$result = Cache::write('test_increment', 5, 'wincache');
$this->assertTrue($result);
$result = Cache::increment('test_increment', 1, 'wincache');
$this->assertEquals(6, $result);
$result = Cache::read('test_increment', 'wincache');
$this->assertEquals(6, $result);
$result = Cache::increment('test_increment', 2, 'wincache');
$this->assertEquals(8, $result);
$result = Cache::read('test_increment', 'wincache');
$this->assertEquals(8, $result);
}
/**
* test the clearing of cache keys
*
* @return void
*/
public function testClear() {
wincache_ucache_set('not_cake', 'safe');
Cache::write('some_value', 'value', 'wincache');
$result = Cache::clear(false, 'wincache');
$this->assertTrue($result);
$this->assertFalse(Cache::read('some_value', 'wincache'));
$this->assertEquals('safe', wincache_ucache_get('not_cake'));
}
/**
* Tests that configuring groups for stored keys return the correct values when read/written
* Shows that altering the group value is equivalent to deleting all keys under the same
* group
*
* @return void
*/
public function testGroupsReadWrite() {
Cache::config('wincache_groups', array(
'engine' => 'Wincache',
'duration' => 0,
'groups' => array('group_a', 'group_b'),
'prefix' => 'test_'
));
$this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'wincache_groups'));
wincache_ucache_inc('test_group_a');
$this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
$this->assertTrue(Cache::write('test_groups', 'value2', 'wincache_groups'));
$this->assertEquals('value2', Cache::read('test_groups', 'wincache_groups'));
wincache_ucache_inc('test_group_b');
$this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
$this->assertTrue(Cache::write('test_groups', 'value3', 'wincache_groups'));
$this->assertEquals('value3', Cache::read('test_groups', 'wincache_groups'));
}
/**
* Tests that deleteing from a groups-enabled config is possible
*
* @return void
*/
public function testGroupDelete() {
Cache::config('wincache_groups', array(
'engine' => 'Wincache',
'duration' => 0,
'groups' => array('group_a', 'group_b'),
'prefix' => 'test_'
));
$this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'wincache_groups'));
$this->assertTrue(Cache::delete('test_groups', 'wincache_groups'));
$this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
}
/**
* Test clearing a cache group
*
* @return void
*/
public function testGroupClear() {
Cache::config('wincache_groups', array(
'engine' => 'Wincache',
'duration' => 0,
'groups' => array('group_a', 'group_b'),
'prefix' => 'test_'
));
$this->assertTrue(Cache::write('test_groups', 'value', 'wincache_groups'));
$this->assertTrue(Cache::clearGroup('group_a', 'wincache_groups'));
$this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
$this->assertTrue(Cache::write('test_groups', 'value2', 'wincache_groups'));
$this->assertTrue(Cache::clearGroup('group_b', 'wincache_groups'));
$this->assertFalse(Cache::read('test_groups', 'wincache_groups'));
}
}

View file

@ -0,0 +1,271 @@
<?php
/**
* XcacheEngineTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Cache.Engine
* @since CakePHP(tm) v 1.2.0.5434
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Cache', 'Cache');
/**
* XcacheEngineTest class
*
* @package Cake.Test.Case.Cache.Engine
*/
class XcacheEngineTest extends CakeTestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
if (!function_exists('xcache_set')) {
$this->markTestSkipped('Xcache is not installed or configured properly');
}
$this->_cacheDisable = Configure::read('Cache.disable');
Configure::write('Cache.disable', false);
Cache::config('xcache', array('engine' => 'Xcache', 'prefix' => 'cake_'));
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
Configure::write('Cache.disable', $this->_cacheDisable);
Cache::drop('xcache');
Cache::drop('xcache_groups');
Cache::config('default');
}
/**
* testSettings method
*
* @return void
*/
public function testSettings() {
$settings = Cache::settings();
$expecting = array(
'prefix' => 'cake_',
'duration' => 3600,
'probability' => 100,
'engine' => 'Xcache',
);
$this->assertTrue(isset($settings['PHP_AUTH_USER']));
$this->assertTrue(isset($settings['PHP_AUTH_PW']));
unset($settings['PHP_AUTH_USER'], $settings['PHP_AUTH_PW']);
$this->assertEquals($settings, $expecting);
}
/**
* testReadAndWriteCache method
*
* @return void
*/
public function testReadAndWriteCache() {
Cache::set(array('duration' => 1));
$result = Cache::read('test');
$expecting = '';
$this->assertEquals($expecting, $result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('test', $data);
$this->assertTrue($result);
$result = Cache::read('test');
$expecting = $data;
$this->assertEquals($expecting, $result);
Cache::delete('test');
}
/**
* testExpiry method
*
* @return void
*/
public function testExpiry() {
Cache::set(array('duration' => 1));
$result = Cache::read('test');
$this->assertFalse($result);
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data);
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test');
$this->assertFalse($result);
Cache::set(array('duration' => "+1 second"));
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data);
$this->assertTrue($result);
sleep(2);
$result = Cache::read('other_test');
$this->assertFalse($result);
}
/**
* testDeleteCache method
*
* @return void
*/
public function testDeleteCache() {
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('delete_test', $data);
$this->assertTrue($result);
$result = Cache::delete('delete_test');
$this->assertTrue($result);
}
/**
* testClearCache method
*
* @return void
*/
public function testClearCache() {
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('clear_test_1', $data);
$this->assertTrue($result);
$result = Cache::write('clear_test_2', $data);
$this->assertTrue($result);
$result = Cache::clear();
$this->assertTrue($result);
}
/**
* testDecrement method
*
* @return void
*/
public function testDecrement() {
$result = Cache::write('test_decrement', 5);
$this->assertTrue($result);
$result = Cache::decrement('test_decrement');
$this->assertEquals(4, $result);
$result = Cache::read('test_decrement');
$this->assertEquals(4, $result);
$result = Cache::decrement('test_decrement', 2);
$this->assertEquals(2, $result);
$result = Cache::read('test_decrement');
$this->assertEquals(2, $result);
}
/**
* testIncrement method
*
* @return void
*/
public function testIncrement() {
$result = Cache::write('test_increment', 5);
$this->assertTrue($result);
$result = Cache::increment('test_increment');
$this->assertEquals(6, $result);
$result = Cache::read('test_increment');
$this->assertEquals(6, $result);
$result = Cache::increment('test_increment', 2);
$this->assertEquals(8, $result);
$result = Cache::read('test_increment');
$this->assertEquals(8, $result);
}
/**
* Tests that configuring groups for stored keys return the correct values when read/written
* Shows that altering the group value is equivalent to deleting all keys under the same
* group
*
* @return void
*/
public function testGroupsReadWrite() {
Cache::config('xcache_groups', array(
'engine' => 'Xcache',
'duration' => 0,
'groups' => array('group_a', 'group_b'),
'prefix' => 'test_'
));
$this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'xcache_groups'));
xcache_inc('test_group_a', 1);
$this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
$this->assertTrue(Cache::write('test_groups', 'value2', 'xcache_groups'));
$this->assertEquals('value2', Cache::read('test_groups', 'xcache_groups'));
xcache_inc('test_group_b', 1);
$this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
$this->assertTrue(Cache::write('test_groups', 'value3', 'xcache_groups'));
$this->assertEquals('value3', Cache::read('test_groups', 'xcache_groups'));
}
/**
* Tests that deleteing from a groups-enabled config is possible
*
* @return void
*/
public function testGroupDelete() {
Cache::config('xcache_groups', array(
'engine' => 'Xcache',
'duration' => 0,
'groups' => array('group_a', 'group_b'),
'prefix' => 'test_'
));
$this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'xcache_groups'));
$this->assertTrue(Cache::delete('test_groups', 'xcache_groups'));
$this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
}
/**
* Test clearing a cache group
*
* @return void
*/
public function testGroupClear() {
Cache::config('xcache_groups', array(
'engine' => 'Xcache',
'duration' => 0,
'groups' => array('group_a', 'group_b'),
'prefix' => 'test_'
));
$this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
$this->assertTrue(Cache::clearGroup('group_a', 'xcache_groups'));
$this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
$this->assertTrue(Cache::write('test_groups', 'value2', 'xcache_groups'));
$this->assertTrue(Cache::clearGroup('group_b', 'xcache_groups'));
$this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
}
}