From 1c3d82ef831dde184d91531bee936055edc0c328 Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 13 Aug 2013 22:47:26 -0400 Subject: [PATCH] Remove dependency on Configure from Cache. Not using Configure to setup caching has a couple benefits. * Using Configure creates magic at a distance. Setting things in one class creates effects in another. * Cache engines are always configured the same way. * Dropped cache engines don't magically re-create themselves. * It is still simple to create cache adapters using configuration files, or inject instances directly. With this change we still lazily load cache adapters once they are used. Add a CacheRegistry to make Cache similar to log and other registy based classes in CakePHP. --- App/Config/cache.php | 38 +++--- lib/Cake/Cache/Cache.php | 124 +++++++++--------- lib/Cake/Cache/CacheRegistry.php | 103 +++++++++++++++ lib/Cake/Test/TestCase/Cache/CacheTest.php | 84 +++++------- .../TestCase/Cache/Engine/ApcEngineTest.php | 10 +- .../TestCase/Cache/Engine/FileEngineTest.php | 34 ++--- .../Cache/Engine/MemcacheEngineTest.php | 22 ++-- .../TestCase/Cache/Engine/RedisEngineTest.php | 12 +- .../Cache/Engine/WincacheEngineTest.php | 8 +- .../Cache/Engine/XcacheEngineTest.php | 8 +- lib/Cake/Test/init.php | 33 +++-- 11 files changed, 288 insertions(+), 188 deletions(-) create mode 100644 lib/Cake/Cache/CacheRegistry.php diff --git a/App/Config/cache.php b/App/Config/cache.php index 657172b9c87..2d17f7c4fb0 100644 --- a/App/Config/cache.php +++ b/App/Config/cache.php @@ -14,7 +14,7 @@ */ namespace App\Config; -use Cake\Core\Configure; +use Cake\Cache\Cache; /** * Turn off all caching application-wide. @@ -33,6 +33,16 @@ */ //Configure::write('Cache.check', true); +/** + * Enable cache view prefixes. + * + * If set it will be prepended to the cache name for view file caching. This is + * helpful if you deploy the same application via multiple subdomains and languages, + * for instance. Each version can then have its own view cache namespace. + * Note: The final cache file name will then be `prefix_cachefilename`. + */ + //Configure::write('Cache.viewPrefix', 'prefix'); + // In development mode, caches should expire quickly. $duration = '+999 days'; if (Configure::read('debug') >= 1) { @@ -48,29 +58,33 @@ // Prefix each application on the same server with a different string, to avoid Memcache and APC conflicts. $prefix = 'myapp_'; + +$cacheConfigs = []; + /** * Configure the cache used for general framework caching. Path information, * object listings, and translation cache files are stored with this configuration. */ -Configure::write('Cache._cake_core_', [ +$cacheConfigs['_cake_core_'] = [ 'engine' => $engine, 'prefix' => $prefix . 'cake_core_', 'path' => CACHE . 'persistent' . DS, 'serialize' => ($engine === 'File'), 'duration' => $duration -]); +]; + /** * Configure the cache for model and datasource caches. This cache configuration * is used to store schema descriptions, and table listings in connections. */ -Configure::write('Cache._cake_model_', [ +$cacheConfigs['_cake_model_'] = [ 'engine' => $engine, 'prefix' => $prefix . 'cake_model_', 'path' => CACHE . 'models' . DS, 'serialize' => ($engine === 'File'), 'duration' => $duration -]); +]; /** * Cache Engine Configuration @@ -145,14 +159,8 @@ * 'persistent' => true, // [optional] set this to false for non-persistent connections * )); */ -Configure::write('Cache.default', array('engine' => 'File')); +$cacheConfigs['default'] = [ + 'engine' => 'File' +]; -/** - * Enable cache view prefixes. - * - * If set it will be prepended to the cache name for view file caching. This is - * helpful if you deploy the same application via multiple subdomains and languages, - * for instance. Each version can then have its own view cache namespace. - * Note: The final cache file name will then be `prefix_cachefilename`. - */ - //Configure::write('Cache.viewPrefix', 'prefix'); +Cache::config($cacheConfigs); diff --git a/lib/Cake/Cache/Cache.php b/lib/Cake/Cache/Cache.php index c5716c9aaa6..9844ff34cd9 100644 --- a/lib/Cake/Cache/Cache.php +++ b/lib/Cake/Cache/Cache.php @@ -32,7 +32,7 @@ * A sample configuration would be: * * {{{ - * Configure::write('Cache.shared', array( + * Cache::config('shared', array( * 'engine' => 'Cake\Cache\Engine\ApcEngine', * 'prefix' => 'my_app_' * )); @@ -83,7 +83,18 @@ class Cache { /** - * Cache configuration stack + * Configuraiton backup. + * + * Keeps the permanent/default settings for each cache engine. + * These settings are used to reset the engines after temporary modification. + * + * @var array + */ + protected static $_restore = array(); + +/** + * Cache configuration. + * * Keeps the permanent/default settings for each cache engine. * These settings are used to reset the engines after temporary modification. * @@ -106,22 +117,32 @@ class Cache { protected static $_reset = false; /** - * Engine instances keyed by configuration name. + * Cache Registry used for creating and using cache adapters. * - * @var array + * @var Cake\Cache\CacheRegistry */ - protected static $_engines = array(); + protected static $_registry; /** - * Deprecated method. Will be removed in 3.0.0 + * This method can be used to define cache adapters for an application + * during the bootstrapping process. You can use this method to add new cache adapters + * at runtime as well. New cache configurations will be constructed upon the next write. + * + * To change an adapter's configuration at runtime, first drop the adapter and then + * reconfigure it. * - * @deprecated + * Adapters will not be constructed until the first operation is done. + * + * @param string|array $key The name of the cache config, or an array of multiple configs. + * @param array $config An array of name => config data for adapter. + * @return void */ - public static function config($name = null, $settings = array()) { - trigger_error( - __d('cake_dev', 'You must use Configure::write() to define cache configuration. Or use engine() to inject new adapter.'), - E_USER_WARNING - ); + public static function config($key, $config = null) { + if ($config !== null && is_string($key)) { + static::$_config[$key] = $config; + return; + } + static::$_config = array_merge(static::$_config, $key); } /** @@ -132,35 +153,22 @@ public static function config($name = null, $settings = array()) { * @throws Cake\Error\Exception */ protected static function _buildEngine($name) { - $config = Configure::read('Cache.' . $name); - if (empty($config['engine'])) { - return false; - } - $cacheClass = App::classname($config['engine'], 'Cache/Engine', 'Engine'); - if (!$cacheClass) { - throw new Error\Exception( - __d('cake_dev', 'Cache engine %s is not available.', $name) - ); - } - if (!is_subclass_of($cacheClass, 'Cake\Cache\CacheEngine')) { - throw new Error\Exception(__d('cake_dev', 'Cache engines must use Cake\Cache\CacheEngine as a base class.')); - } - $engine = new $cacheClass(); - if (!$engine->init($config)) { - throw new Error\Exception( - __d('cake_dev', 'Cache engine %s is not properly configured.', $name) - ); + if (empty(static::$_registry)) { + static::$_registry = new CacheRegistry(); } - if ($engine->settings['probability'] && time() % $engine->settings['probability'] === 0) { - $engine->gc(); + if (empty(static::$_config[$name]['engine'])) { + return false; } - static::$_engines[$name] = $engine; + $config = static::$_config[$name]; + $config['className'] = $config['engine']; + + static::$_registry->load($name, $config); if (!empty($config['groups'])) { foreach ($config['groups'] as $group) { static::$_groups[$group][] = $name; - sort(static::$_groups[$group]); static::$_groups[$group] = array_unique(static::$_groups[$group]); + sort(static::$_groups[$group]); } } @@ -168,14 +176,12 @@ protected static function _buildEngine($name) { } /** - * Returns an array containing the connected Cache engines. - * This will not return engines that are configured, but have not - * been used yet. + * Returns an array containing the configured Cache engines. * - * @return array Array of connected Cache config names. + * @return array Array of configured Cache config names. */ public static function configured() { - return array_keys(static::$_engines); + return array_keys(static::$_config); } /** @@ -188,47 +194,39 @@ public static function configured() { * @return boolean success of the removal, returns false when the config does not exist. */ public static function drop($config) { - if (isset(static::$_engines[$config])) { - unset(static::$_engines[$config]); - unset(static::$_config[$config]); - return true; + if (!isset(static::$_registry->{$config})) { + return false; } - return false; + static::$_registry->unload($config); + unset(static::$_config[$config], static::$_restore[$config]); + return true; } /** * Fetch the engine attached to a specific configuration name. - * If the engine does not exist, configuration data will be read from - * `Configure`. * * If the cache engine & configuration are missing an error will be * triggered. * - * @param string $config The configuration name you want an engine. - * @param Cake\Cache\CacheEngine $engine An engine instance if you are manually - * injecting a cache engine. + * @param string $config The configuration name you want an engine for. * @return Cake\Cache\Engine */ - public static function engine($config, CacheEngine $engine = null) { + public static function engine($config) { if (Configure::read('Cache.disable')) { return false; } - if (isset(static::$_engines[$config])) { - return static::$_engines[$config]; + if (isset(static::$_registry->{$config})) { + return static::$_registry->{$config}; } - if (!$engine && !static::_buildEngine($config, $engine)) { + if (!static::_buildEngine($config)) { $message = __d( 'cake_dev', - 'The "%s" cache configuration does not exist, nor could configuration be found at "Cache.%s".', - $config, + 'The "%s" cache configuration does not exist.', $config ); trigger_error($message, E_USER_WARNING); } - if ($engine) { - static::$_engines[$config] = $engine; - } - return static::$_engines[$config]; + return static::$_registry->{$config}; } /** @@ -269,8 +267,8 @@ public static function set($settings = array(), $value = null, $config = 'defaul return false; } - if (empty(static::$_config[$config])) { - static::$_config[$config] = $engine->settings(); + if (empty(static::$_restore[$config])) { + static::$_restore[$config] = $engine->settings(); } if (!empty($settings)) { @@ -292,11 +290,11 @@ public static function set($settings = array(), $value = null, $config = 'defaul * @return void */ protected static function _modifySettings($engine, $config, $settings) { - $restore = static::$_config[$config]; + $restore = static::$_restore[$config]; if (empty($settings)) { static::$_reset = false; $settings = $restore; - unset(static::$_config[$config]); + unset(static::$_restore[$config]); } else { $settings = array_merge($restore, $settings); if (isset($settings['duration']) && !is_numeric($settings['duration'])) { diff --git a/lib/Cake/Cache/CacheRegistry.php b/lib/Cake/Cache/CacheRegistry.php new file mode 100644 index 00000000000..0481cf2ff44 --- /dev/null +++ b/lib/Cake/Cache/CacheRegistry.php @@ -0,0 +1,103 @@ +init($settings)) { + throw new Error\Exception( + __d('cake_dev', 'Cache engine %s is not properly configured.', $class) + ); + } + if ($instance->settings['probability'] && time() % $instance->settings['probability'] === 0) { + $instance->gc(); + } + return $instance; + } + +/** + * Remove a single adapter from the registry. + * + * @param string $name The adapter name. + * @return void + */ + public function unload($name) { + unset($this->_loaded[$name]); + } + + +} diff --git a/lib/Cake/Test/TestCase/Cache/CacheTest.php b/lib/Cake/Test/TestCase/Cache/CacheTest.php index 2416c3aaaf5..379561b4d9b 100644 --- a/lib/Cake/Test/TestCase/Cache/CacheTest.php +++ b/lib/Cake/Test/TestCase/Cache/CacheTest.php @@ -36,7 +36,8 @@ public function setUp() { parent::setUp(); Configure::write('Cache.disable', false); - Configure::write('Cache.default', [ + Cache::drop('tests'); + Cache::config('default', [ 'engine' => 'File', 'path' => TMP . 'tests' ]); @@ -53,8 +54,8 @@ public function testEngine() { 'path' => TMP . 'tests', 'prefix' => 'cake_test_' ]; - Configure::write('Cache.test_config', $settings); - $engine = Cache::engine('test_config'); + Cache::config('tests', $settings); + $engine = Cache::engine('tests'); $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine); } @@ -66,8 +67,8 @@ public function testEngine() { */ public function testConfigInvalidEngine() { $settings = array('engine' => 'Imaginary'); - Configure::write('Cache.imaginary', $settings); - Cache::engine('imaginary'); + Cache::config('tests', $settings); + Cache::engine('tests'); } /** @@ -77,17 +78,15 @@ public function testConfigInvalidEngine() { */ public function testNonFatalErrorsWithCachedisable() { Configure::write('Cache.disable', true); - Configure::write('Cache.test', [ + Cache::config('tests', [ 'engine' => 'File', 'path' => TMP, 'prefix' => 'error_test_' ]); - Cache::write('no_save', 'Noooo!', 'test'); - Cache::read('no_save', 'test'); - Cache::delete('no_save', 'test'); + Cache::write('no_save', 'Noooo!', 'tests'); + Cache::read('no_save', 'tests'); + Cache::delete('no_save', 'tests'); Cache::set('duration', '+10 minutes'); - - Configure::write('Cache.disable', false); } /** @@ -104,12 +103,12 @@ public function testConfigWithLibAndPluginEngines() { Plugin::load('TestPlugin'); $settings = ['engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_']; - Configure::write('Cache.libEngine', $settings); + Cache::config('libEngine', $settings); $engine = Cache::engine('libEngine'); $this->assertInstanceOf('\TestApp\Cache\Engine\TestAppCacheEngine', $engine); $settings = ['engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_']; - $result = Configure::write('Cache.pluginLibEngine', $settings); + $result = Cache::config('pluginLibEngine', $settings); $engine = Cache::engine('pluginLibEngine'); $this->assertInstanceOf('\TestPlugin\Cache\Engine\TestPluginCacheEngine', $engine); @@ -127,20 +126,16 @@ public function testConfigWithLibAndPluginEngines() { */ public function testInvalidConfig() { // In debug mode it would auto create the folder. - $debug = Configure::read('debug'); Configure::write('debug', 0); - Cache::config('invalid', array( + Cache::config('tests', array( 'engine' => 'File', 'duration' => '+1 year', 'prefix' => 'testing_invalid_', 'path' => 'data/', 'serialize' => true, - 'random' => 'wii' )); - Cache::read('Test', 'invalid'); - - Configure::write('debug', $debug); + Cache::read('Test', 'tests'); } /** @@ -181,10 +176,10 @@ public function testDecrementNonExistingConfig() { */ public function testAttemptingToConfigureANonCacheEngineClass() { $this->getMock('\StdClass', array(), array(), 'RubbishEngine'); - Configure::write('Cache.wrong', array( + Cache::config('tests', array( 'engine' => '\RubbishEngine' )); - Cache::engine('wrong'); + Cache::engine('tests'); } /** @@ -194,7 +189,7 @@ public function testAttemptingToConfigureANonCacheEngineClass() { */ public function testSetEngineValid() { $engine = $this->getMockForAbstractClass('\Cake\Cache\CacheEngine'); - Cache::engine('test', $engine); + Cache::config('test', ['engine' => $engine]); $this->assertSame($engine, Cache::engine('test')); } @@ -204,13 +199,13 @@ public function testSetEngineValid() { * @return void */ public function testConfigSettingDefaultConfigKey() { - Configure::write('Cache.test_name', [ + Cache::config('tests', [ 'engine' => 'File', - 'prefix' => 'test_name_' + 'prefix' => 'tests_' ]); - Cache::write('value_one', 'I am cached', 'test_name'); - $result = Cache::read('value_one', 'test_name'); + Cache::write('value_one', 'I am cached', 'tests'); + $result = Cache::read('value_one', 'tests'); $this->assertEquals('I am cached', $result); $result = Cache::read('value_one'); @@ -220,10 +215,10 @@ public function testConfigSettingDefaultConfigKey() { $result = Cache::read('value_one'); $this->assertEquals('I am in default config!', $result); - $result = Cache::read('value_one', 'test_name'); + $result = Cache::read('value_one', 'tests'); $this->assertEquals('I am cached', $result); - Cache::delete('value_one', 'test_name'); + Cache::delete('value_one', 'tests'); Cache::delete('value_one', 'default'); } @@ -231,7 +226,7 @@ public function testConfigSettingDefaultConfigKey() { * testGroupConfigs method */ public function testGroupConfigs() { - Configure::write('Cache.latest', [ + Cache::config('latest', [ 'duration' => 300, 'engine' => 'File', 'groups' => ['posts', 'comments'], @@ -248,7 +243,7 @@ public function testGroupConfigs() { $result = Cache::groupConfigs('posts'); $this->assertEquals(['posts' => ['latest']], $result); - Configure::write('Cache.page', [ + Cache::config('page', [ 'duration' => 86400, 'engine' => 'File', 'groups' => ['posts', 'archive'], @@ -266,7 +261,7 @@ public function testGroupConfigs() { $result = Cache::groupConfigs('archive'); $this->assertEquals(['archive' => ['page']], $result); - Configure::write('Cache.archive', [ + Cache::config('archive', [ 'duration' => 86400 * 30, 'engine' => 'File', 'groups' => ['posts', 'archive', 'comments'], @@ -296,10 +291,6 @@ public function testConfigured() { $result = Cache::configured(); $this->assertContains('_cake_core_', $result); $this->assertNotContains('default', $result, 'Unconnected engines should not display.'); - - Cache::engine('default'); - $result = Cache::configured(); - $this->assertContains('default', $result, 'default should exist now.'); } /** @@ -322,10 +313,7 @@ public function testDrop() { $result = Cache::drop('default'); $this->assertTrue($result, 'Built engines should be dropped'); - $config = Configure::read('Cache.default'); - $this->assertEquals('File', $config['engine'], 'Config data should not be removed.'); - - Configure::write('Cache.unconfigTest', [ + Cache::config('unconfigTest', [ 'engine' => 'TestAppCache' ]); $this->assertInstanceOf( @@ -343,7 +331,7 @@ public function testDrop() { * @return void */ public function testDropChangeConfig() { - Configure::write('Cache.tests', [ + Cache::config('tests', [ 'engine' => 'File', ]); $result = Cache::engine('tests'); @@ -354,7 +342,7 @@ public function testDropChangeConfig() { Cache::drop('tests'); - Configure::write('Cache.tests', [ + Cache::config('tests', [ 'engine' => 'File', 'extra' => 'value' ]); @@ -395,7 +383,7 @@ public function testWriteTriggerError() { 'Plugin' => array(CAKE . 'Test/TestApp/Plugin/') ), App::RESET); Configure::write('App.namespace', 'TestApp'); - Configure::write('Cache.test_trigger', [ + Cache::config('test_trigger', [ 'engine' => 'TestAppCache', 'prefix' => '' ]); @@ -420,7 +408,7 @@ public function testWriteTriggerError() { */ public function testCacheDisable() { Configure::write('Cache.disable', false); - Configure::write('Cache.test_cache_disable_1', [ + Cache::config('test_cache_disable_1', [ 'engine' => 'File', 'path' => TMP . 'tests' ]); @@ -439,7 +427,7 @@ public function testCacheDisable() { $this->assertSame(Cache::read('key_3', 'test_cache_disable_1'), 'hello'); Configure::write('Cache.disable', true); - Configure::write('Cache.test_cache_disable_2', [ + Cache::config('test_cache_disable_2', [ 'engine' => 'File', 'path' => TMP . 'tests' ]); @@ -489,11 +477,10 @@ public function testSet() { * @return void */ public function testSetModifySettings() { - Configure::write('Cache.tests', [ + Cache::config('tests', [ 'engine' => 'File', 'duration' => '+1 minute' ]); - Cache::drop('tests'); $result = Cache::set(['duration' => '+1 year'], 'tests'); $this->assertEquals(strtotime('+1 year') - time(), $result['duration']); @@ -511,11 +498,10 @@ public function testSetModifySettings() { * @return void */ public function testSetModifyAndResetSettings() { - Configure::write('Cache.tests', [ + Cache::config('tests', [ 'engine' => 'File', 'duration' => '+1 minute' ]); - Cache::drop('tests'); $result = Cache::set('duration', '+1 year', 'tests'); $this->assertEquals(strtotime('+1 year') - time(), $result['duration']); @@ -529,7 +515,7 @@ public function testSetModifyAndResetSettings() { * @return void */ public function testSetOnAlternateConfigs() { - Configure::write('Cache.file_config', [ + Cache::config('file_config', [ 'engine' => 'File', 'prefix' => 'test_file_' ]); diff --git a/lib/Cake/Test/TestCase/Cache/Engine/ApcEngineTest.php b/lib/Cake/Test/TestCase/Cache/Engine/ApcEngineTest.php index 9b8f1933df7..75fbab4f439 100644 --- a/lib/Cake/Test/TestCase/Cache/Engine/ApcEngineTest.php +++ b/lib/Cake/Test/TestCase/Cache/Engine/ApcEngineTest.php @@ -43,7 +43,7 @@ public function setUp() { } Configure::write('Cache.disable', false); - Configure::write('Cache.apc', ['engine' => 'Apc', 'prefix' => 'cake_']); + Cache::config('apc', ['engine' => 'Apc', 'prefix' => 'cake_']); } /** @@ -86,7 +86,7 @@ public function testReadAndWriteCache() { * @return void */ public function testReadWriteDurationZero() { - Configure::write('apc', ['engine' => 'Apc', 'duration' => 0, 'prefix' => 'cake_']); + Cache::config('apc', ['engine' => 'Apc', 'duration' => 0, 'prefix' => 'cake_']); Cache::write('zero', 'Should save', 'apc'); sleep(1); @@ -214,7 +214,7 @@ public function testClear() { * @return void */ public function testGroupsReadWrite() { - Configure::write('Cache.apc_groups', [ + Cache::config('apc_groups', [ 'engine' => 'Apc', 'duration' => 0, 'groups' => array('group_a', 'group_b'), @@ -240,7 +240,7 @@ public function testGroupsReadWrite() { * @return void */ public function testGroupDelete() { - Configure::write('Cache.apc_groups', array( + Cache::config('apc_groups', array( 'engine' => 'Apc', 'duration' => 0, 'groups' => array('group_a', 'group_b'), @@ -259,7 +259,7 @@ public function testGroupDelete() { * @return void */ public function testGroupClear() { - Configure::write('Cache.apc_groups', array( + Cache::config('apc_groups', array( 'engine' => 'Apc', 'duration' => 0, 'groups' => array('group_a', 'group_b'), diff --git a/lib/Cake/Test/TestCase/Cache/Engine/FileEngineTest.php b/lib/Cake/Test/TestCase/Cache/Engine/FileEngineTest.php index 844b1a8434b..f02e5d01f0f 100644 --- a/lib/Cake/Test/TestCase/Cache/Engine/FileEngineTest.php +++ b/lib/Cake/Test/TestCase/Cache/Engine/FileEngineTest.php @@ -45,7 +45,7 @@ class FileEngineTest extends TestCase { public function setUp() { parent::setUp(); Configure::write('Cache.disable', false); - Configure::write('Cache.file_test', [ + Cache::config('file_test', [ 'engine' => 'File', 'path' => CACHE ]); @@ -162,7 +162,7 @@ public function testDeleteCache() { * @return void */ public function testSerialize() { - Configure::write('Cache.file_test', ['engine' => 'File', 'serialize' => true]); + Cache::config('file_test', ['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); @@ -181,7 +181,7 @@ public function testSerialize() { * @return void */ public function testClear() { - Configure::write('Cache.file_test', ['engine' => 'File', 'duration' => 1]); + Cache::config('file_test', ['engine' => 'File', 'duration' => 1]); $data = 'this is a test of the emergency broadcasting system'; $write = Cache::write('serialize_test1', $data, 'file_test'); @@ -299,7 +299,7 @@ public function testKeyPath() { * @return void */ public function testRemoveWindowsSlashesFromCache() { - Configure::write('Cache.windows_test', [ + Cache::config('windows_test', [ 'engine' => 'File', 'isWindows' => true, 'prefix' => null, @@ -349,13 +349,13 @@ public function testRemoveWindowsSlashesFromCache() { * @return void */ public function testWriteQuotedString() { - Configure::write('Cache.file_test', array('engine' => 'File', 'path' => TMP . 'tests')); + 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'"); - Configure::write('Cache.file_test', array('isWindows' => true, 'path' => TMP . 'tests')); + 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'"); @@ -371,7 +371,7 @@ public function testWriteQuotedString() { public function testPathDoesNotExist() { $this->skipIf(is_dir(TMP . 'tests' . DS . 'autocreate'), 'Cannot run if test directory exists.'); - Configure::write('Cache.autocreate', array( + Cache::config('autocreate', array( 'engine' => 'File', 'path' => TMP . 'tests' . DS . 'autocreate' )); @@ -388,7 +388,7 @@ public function testMaskSetting() { if (DS === '\\') { $this->markTestSkipped('File permission testing does not work on Windows.'); } - Configure::write('Cache.mask_test', ['engine' => 'File', 'path' => TMP . 'tests']); + Cache::config('mask_test', ['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/cake_masking_test')), -4); @@ -397,7 +397,7 @@ public function testMaskSetting() { Cache::delete('masking_test', 'mask_test'); Cache::drop('mask_test'); - Configure::write('Cache.mask_test', ['engine' => 'File', 'mask' => 0666, 'path' => TMP . 'tests']); + Cache::config('mask_test', ['engine' => 'File', 'mask' => 0666, 'path' => TMP . 'tests']); $write = Cache::write('masking_test', $data, 'mask_test'); $result = substr(sprintf('%o', fileperms(TMP . 'tests/cake_masking_test')), -4); $expected = '0666'; @@ -405,7 +405,7 @@ public function testMaskSetting() { Cache::delete('masking_test', 'mask_test'); Cache::drop('mask_test'); - Configure::write('Cache.mask_test', ['engine' => 'File', 'mask' => 0644, 'path' => TMP . 'tests']); + Cache::config('mask_test', ['engine' => 'File', 'mask' => 0644, 'path' => TMP . 'tests']); $write = Cache::write('masking_test', $data, 'mask_test'); $result = substr(sprintf('%o', fileperms(TMP . 'tests/cake_masking_test')), -4); $expected = '0644'; @@ -413,7 +413,7 @@ public function testMaskSetting() { Cache::delete('masking_test', 'mask_test'); Cache::drop('mask_test'); - Configure::write('Cache.mask_test', ['engine' => 'File', 'mask' => 0640, 'path' => TMP . 'tests']); + Cache::config('mask_test', ['engine' => 'File', 'mask' => 0640, 'path' => TMP . 'tests']); $write = Cache::write('masking_test', $data, 'mask_test'); $result = substr(sprintf('%o', fileperms(TMP . 'tests/cake_masking_test')), -4); $expected = '0640'; @@ -428,7 +428,7 @@ public function testMaskSetting() { * @return void */ public function testGroupsReadWrite() { - Configure::write('Cache.file_groups', [ + Cache::config('file_groups', [ 'engine' => 'File', 'duration' => 3600, 'groups' => array('group_a', 'group_b') @@ -444,7 +444,7 @@ public function testGroupsReadWrite() { * Test that clearing with repeat writes works properly */ public function testClearingWithRepeatWrites() { - Configure::write('Cache.repeat', [ + Cache::config('repeat', [ 'engine' => 'File', 'groups' => array('users') ]); @@ -473,7 +473,7 @@ public function testClearingWithRepeatWrites() { * @return void */ public function testGroupDelete() { - Configure::write('Cache.file_groups', [ + Cache::config('file_groups', [ 'engine' => 'File', 'duration' => 3600, 'groups' => array('group_a', 'group_b') @@ -491,17 +491,17 @@ public function testGroupDelete() { * @return void */ public function testGroupClear() { - Configure::write('Cache.file_groups', [ + Cache::config('file_groups', [ 'engine' => 'File', 'duration' => 3600, 'groups' => array('group_a', 'group_b') ]); - Configure::write('Cache.file_groups2', [ + Cache::config('file_groups2', [ 'engine' => 'File', 'duration' => 3600, 'groups' => array('group_b') ]); - Configure::write('Cache.file_groups3', [ + Cache::config('file_groups3', [ 'engine' => 'File', 'duration' => 3600, 'groups' => array('group_b'), diff --git a/lib/Cake/Test/TestCase/Cache/Engine/MemcacheEngineTest.php b/lib/Cake/Test/TestCase/Cache/Engine/MemcacheEngineTest.php index f0ae05a3233..df5188f8756 100644 --- a/lib/Cake/Test/TestCase/Cache/Engine/MemcacheEngineTest.php +++ b/lib/Cake/Test/TestCase/Cache/Engine/MemcacheEngineTest.php @@ -63,7 +63,7 @@ public function setUp() { $this->skipIf(!class_exists('Memcache'), 'Memcache is not installed or configured properly.'); Configure::write('Cache.disable', false); - Configure::write('Cache.memcache', array( + Cache::config('memcache', array( 'engine' => 'Memcache', 'prefix' => 'cake_', 'duration' => 3600 @@ -97,7 +97,7 @@ public function testSettings() { 'servers' => array('127.0.0.1'), 'persistent' => true, 'compress' => false, - 'engine' => 'Memcache', + 'engine' => 'Cake\Cache\Engine\MemcacheEngine', 'groups' => array() ); $this->assertEquals($expecting, $settings); @@ -317,17 +317,17 @@ public function testIncrement() { * @return void */ public function testConfigurationConflict() { - Configure::write('Cache.long_memcache', [ + Cache::config('long_memcache', [ 'engine' => 'Memcache', 'duration' => '+2 seconds', 'servers' => ['127.0.0.1:11211'], ]); - Configure::write('Cache.short_memcache', [ + Cache::config('short_memcache', [ 'engine' => 'Memcache', 'duration' => '+1 seconds', 'servers' => ['127.0.0.1:11211'], ]); - Configure::write('Cache.some_file', ['engine' => 'File']); + Cache::config('some_file', ['engine' => 'File']); $this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcache')); $this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcache')); @@ -352,7 +352,7 @@ public function testConfigurationConflict() { * @return void */ public function testClear() { - Configure::write('Cache.memcache2', [ + Cache::config('memcache2', [ 'engine' => 'Memcache', 'prefix' => 'cake2_', 'duration' => 3600 @@ -378,7 +378,7 @@ public function testClear() { * @return void */ public function testZeroDuration() { - Configure::write('Cache.memcache', [ + Cache::config('memcache', [ 'engine' => 'Memcache', 'prefix' => 'cake_', 'duration' => 0 @@ -417,13 +417,13 @@ public function testLongDurationEqualToZero() { * @return void */ public function testGroupReadWrite() { - Configure::write('Cache.memcache_groups', [ + Cache::config('memcache_groups', [ 'engine' => 'Memcache', 'duration' => 3600, 'groups' => ['group_a', 'group_b'], 'prefix' => 'test_' ]); - Configure::write('Cache.memcache_helper', [ + Cache::config('memcache_helper', [ 'engine' => 'Memcache', 'duration' => 3600, 'prefix' => 'test_' @@ -448,7 +448,7 @@ public function testGroupReadWrite() { * @return void */ public function testGroupDelete() { - Configure::write('Cache.memcache_groups', [ + Cache::config('memcache_groups', [ 'engine' => 'Memcache', 'duration' => 3600, 'groups' => ['group_a', 'group_b'] @@ -466,7 +466,7 @@ public function testGroupDelete() { * @return void */ public function testGroupClear() { - Configure::write('Cache.memcache_groups', [ + Cache::config('memcache_groups', [ 'engine' => 'Memcache', 'duration' => 3600, 'groups' => ['group_a', 'group_b'] diff --git a/lib/Cake/Test/TestCase/Cache/Engine/RedisEngineTest.php b/lib/Cake/Test/TestCase/Cache/Engine/RedisEngineTest.php index 0555f8478f3..4bec237e7f5 100644 --- a/lib/Cake/Test/TestCase/Cache/Engine/RedisEngineTest.php +++ b/lib/Cake/Test/TestCase/Cache/Engine/RedisEngineTest.php @@ -36,7 +36,7 @@ public function setUp() { $this->skipIf(!class_exists('Redis'), 'Redis is not installed or configured properly.'); Configure::write('Cache.disable', false); - Configure::write('Cache.redis', array( + Cache::config('redis', array( 'engine' => 'Cake\Cache\Engine\RedisEngine', 'prefix' => 'cake_', 'duration' => 3600 @@ -223,7 +223,7 @@ public function testIncrement() { * @return void */ public function testClear() { - Configure::write('Cache.redis2', array( + Cache::config('redis2', array( 'engine' => 'Redis', 'prefix' => 'cake2_', 'duration' => 3600 @@ -265,13 +265,13 @@ public function testZeroDuration() { * @return void */ public function testGroupReadWrite() { - Configure::write('Cache.redis_groups', [ + Cache::config('redis_groups', [ 'engine' => 'Redis', 'duration' => 3600, 'groups' => ['group_a', 'group_b'], 'prefix' => 'test_' ]); - Configure::write('Cache.redis_helper', [ + Cache::config('redis_helper', [ 'engine' => 'Redis', 'duration' => 3600, 'prefix' => 'test_' @@ -296,7 +296,7 @@ public function testGroupReadWrite() { * @return void */ public function testGroupDelete() { - Configure::write('Cache.redis_groups', [ + Cache::config('redis_groups', [ 'engine' => 'Redis', 'duration' => 3600, 'groups' => ['group_a', 'group_b'] @@ -314,7 +314,7 @@ public function testGroupDelete() { * @return void */ public function testGroupClear() { - Configure::write('Cache.redis_groups', [ + Cache::config('redis_groups', [ 'engine' => 'Redis', 'duration' => 3600, 'groups' => ['group_a', 'group_b'] diff --git a/lib/Cake/Test/TestCase/Cache/Engine/WincacheEngineTest.php b/lib/Cake/Test/TestCase/Cache/Engine/WincacheEngineTest.php index 93a0976d082..f4210f1693d 100644 --- a/lib/Cake/Test/TestCase/Cache/Engine/WincacheEngineTest.php +++ b/lib/Cake/Test/TestCase/Cache/Engine/WincacheEngineTest.php @@ -39,7 +39,7 @@ public function setUp() { parent::setUp(); $this->skipIf(!function_exists('wincache_ucache_set'), 'Wincache is not installed or configured properly.'); Configure::write('Cache.disable', false); - Configure::write('Cache.wincache', ['engine' => 'Wincache', 'prefix' => 'cake_']); + Cache::config('wincache', ['engine' => 'Wincache', 'prefix' => 'cake_']); } /** @@ -201,7 +201,7 @@ public function testClear() { * @return void */ public function testGroupsReadWrite() { - Configure::write('Cache.wincache_groups', [ + Cache::config('wincache_groups', [ 'engine' => 'Wincache', 'duration' => 0, 'groups' => ['group_a', 'group_b'], @@ -227,7 +227,7 @@ public function testGroupsReadWrite() { * @return void */ public function testGroupDelete() { - Configure::write('Cache.wincache_groups', [ + Cache::config('wincache_groups', [ 'engine' => 'Wincache', 'duration' => 0, 'groups' => ['group_a', 'group_b'], @@ -246,7 +246,7 @@ public function testGroupDelete() { * @return void */ public function testGroupClear() { - Configure::write('Cache.wincache_groups', [ + Cache::config('wincache_groups', [ 'engine' => 'Wincache', 'duration' => 0, 'groups' => ['group_a', 'group_b'], diff --git a/lib/Cake/Test/TestCase/Cache/Engine/XcacheEngineTest.php b/lib/Cake/Test/TestCase/Cache/Engine/XcacheEngineTest.php index 88864efbd63..fe4a35ca348 100644 --- a/lib/Cake/Test/TestCase/Cache/Engine/XcacheEngineTest.php +++ b/lib/Cake/Test/TestCase/Cache/Engine/XcacheEngineTest.php @@ -40,7 +40,7 @@ public function setUp() { $this->markTestSkipped('Xcache is not installed or configured properly'); } Configure::write('Cache.disable', false); - Configure::write('Cache.xcache', ['engine' => 'Xcache', 'prefix' => 'cake_']); + Cache::config('xcache', ['engine' => 'Xcache', 'prefix' => 'cake_']); } /** @@ -209,7 +209,7 @@ public function testIncrement() { * @return void */ public function testGroupsReadWrite() { - Configure::write('Cache.xcache_groups', [ + Cache::config('xcache_groups', [ 'engine' => 'Xcache', 'duration' => 0, 'groups' => ['group_a', 'group_b'], @@ -235,7 +235,7 @@ public function testGroupsReadWrite() { * @return void */ public function testGroupDelete() { - Configure::write('Cache.xcache_groups', [ + Cache::config('xcache_groups', [ 'engine' => 'Xcache', 'duration' => 0, 'groups' => ['group_a', 'group_b'], @@ -254,7 +254,7 @@ public function testGroupDelete() { * @return void */ public function testGroupClear() { - Configure::write('Cache.xcache_groups', [ + Cache::config('xcache_groups', [ 'engine' => 'Xcache', 'duration' => 0, 'groups' => ['group_a', 'group_b'], diff --git a/lib/Cake/Test/init.php b/lib/Cake/Test/init.php index 7b654cfedc6..f32a30a83a0 100644 --- a/lib/Cake/Test/init.php +++ b/lib/Cake/Test/init.php @@ -13,6 +13,8 @@ */ use Cake\Core\Configure; +use Cake\Cache\Cache; +use Cake\Log\Log; define('DS', DIRECTORY_SEPARATOR); define('ROOT', dirname(dirname(dirname(__DIR__)))); @@ -55,10 +57,12 @@ 'cssBaseUrl' => 'css/', ]); -Configure::write('Cache._cake_core_', [ - 'engine' => 'File', - 'prefix' => 'cake_core_', - 'serialize' => true +Cache::config([ + '_cake_core_' => [ + 'engine' => 'File', + 'prefix' => 'cake_core_', + 'serialize' => true + ] ]); Configure::write('Datasource.test', [ @@ -73,16 +77,17 @@ 'defaults' => 'php' ]); -Configure::write('Log.debug', [ - 'engine' => 'Cake\Log\Engine\FileLog', - 'levels' => ['notice', 'info', 'debug'], - 'file' => 'debug', -]); - -Configure::write('Log.error', [ - 'engine' => 'Cake\Log\Engine\FileLog', - 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'], - 'file' => 'error', +Log::config([ + 'debug' => [ + 'engine' => 'Cake\Log\Engine\FileLog', + 'levels' => ['notice', 'info', 'debug'], + 'file' => 'debug', + ], + 'error' => [ + 'engine' => 'Cake\Log\Engine\FileLog', + 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'], + 'file' => 'error', + ] ]); $autoloader = new Cake\Core\ClassLoader('TestApp', dirname(__DIR__) . '/Test');