Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

3.0 cache log config changes #1534

Merged
merged 9 commits into from

5 participants

@markstory
Owner

After @lorenzo 's comments on my last pull request (#1517) I've added the ability to read config data back out of Cache and Log. In addition to that I've done a few other things.

  • Made it simpler to inject built objects into Cache/Log.
  • Made it simple to use a factory function to help with more complicated engine construction.
  • Renamed 'engine' to 'className'. This is more conformant to the rest of CakePHP. engine will continue working though for now.

I also removed Cache.disable from Configure. I've always disliked this configure value. It is now part of the Cache class so it doesn't need to interact with Configure making the check cheaper, simpler and Cache more self contained.

I think there is an opportunity to extract config() and drop() into a trait if there are a few more similar uses. With only two uses it doesn't make much sense right now. But if another class re-uses the same pattern it will be useful.

I spent some time and drafted up some ideas on how config methods on other classes could work as well. Any feedback on that is more than welcome.

markstory added some commits
@markstory markstory Expand abilities of Cache::config()
Allow various types of configuration to be provided. The new variants
allow for simpler dependency injection and still afford configuration
files.

Reconfiguring an adapter is now not allowed, as the configuration
changes were never propagated to the adapter. This results in a silent
failure that is hard to detect.

Move/remove tests around to make sense with the new API's
f074fd8
@markstory markstory Update failing cache tests.
Remove 'engine' from the settings in each engine. It provides no value
and is never referenced outside of tests.
ab70825
@markstory markstory Cache should remove old settings when upgrading settings
When converting engine => className the old key should be removed.
cfaef56
@markstory markstory Update Log::config() to support more formats.
Make Log::config() accept the same data as Cache.
d942d30
@markstory markstory Fix tests that were relying on re-declaring cache/log adapters. b1a2f86
@markstory markstory Remove Cache.disable from Configure.
Give it a set of named methods on Cache. This configure value always
felt out of place and fits much better with Cache's feature set.
7cf7901
@jippi
Collaborator

:+1:

except: there seem to be a lack of those newlines we also talked about in the other PR

and

there isn't any way to change "_enabled" without calling a method, thus not allowing cake to be configured / bootstrap from say, and environment ini file ?

@lorenzo lorenzo commented on the diff
lib/Cake/Cache/Cache.php
((52 lines not shown))
return;
}
-
- static::$_config = array_merge(static::$_config, $key);
+ if (isset(static::$_config[$key])) {
+ throw new Error\Exception(__d('cake_dev', 'Cannot reconfigure existing adapter "%s"', $key));
@lorenzo Owner
lorenzo added a note

I think this is a great idea and for the same reasons I think Cache::set() should get the axe

@markstory Owner

I'm glad I'm not the only person who hates Cache::set(). That method is all sorts of silly. I'm on board for removing it if others are. It adds a pile of complexity to Cache for almost no benefit other than making the calling code more complicated as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@lorenzo
Owner

Good job mark! There is one failing test in travis that you might want to take a look to, something related to auto creating the tmp folders

@markstory
Owner

@jippi Would you ever setup a deployed app with caching off? I couldn't think of a useful scenario where you'd want to configure a deployed application with caching disabled.

@markstory
Owner

@lorenzo What did you think of the other examples/proposals in the wiki page?

@jippi
Collaborator

@markstory I could do that, or have it disabled in dev and enable it in prod - its also just for consistency that you can configure everything in a simple manner without using php

@lorenzo
Owner

@markstory I think it is heading a good direction, the only thing that i could not understand is what is the difference between Email::configTransport and Email::config

@jrbasso
Collaborator

The config method from Log and Cache are almost identical. What do you think in make it a trait? Probably other classes will be able to use it too.

@markstory
Owner

@jippi What if Cache::enabled() took a parameter to set the state? Kind of like a generic get/set that could be populated with data from a config file?

<?php
Cache::enabled(Configure::read('App.cacheEnabled'));
@markstory
Owner

@jrbasso Yes they are almost identical. Once there is a third copy I'll extract it into a trait.

@lorenzo I thought it might be simpler code/API wise if transports and delivery profiles were separate. That way if your application had multiple delivery profiles that re-used the same transport you would be able to avoid duplicating the transport settings multiple times.

@lorenzo
Owner

@markstory That's a cool idea, I always hated to configure the same transport for different profiles. I also like the Cache::enabled() thing better

@ADmad
Collaborator

@lorenzo In 2.x we also have the problem that delivery config bled into transport. Eg. Doing $CakeEmail->config(array('log' => true)) the log key ends up in transport config too.

@jippi
Collaborator

@markstory could work, but would require php to configure the app, and not just a configure load of an ini file

@markstory
Owner

@lorenzo Yeah having to duplicate transport settings was painful. Also the problem @ADmad pointed out created ambiguity in what would happen when Email::config() was called.

@jippi The current cache and log config methods will each require at least 1 line of PHP code. For example if you were using Configure's features to load/use configuration files you would have to do something like:

<?php
// app/Config/bootstrap.php
Configure::config('default', new IniReader());
Configure::load('caching');
Configure::load('logging');

Log::config(Configure::read('Log'));
Cache::config(Configure::read('Log'));
Cache::enabled(Configure::read('Cache.enabled'));

The point of this and the last set of changes was to remove the magic, ability to get data out of sync and spooky action at a distance that not having to use any methods caused. I feel this code while 3 lines longer is far more transparent and consistent.

markstory added some commits
@markstory markstory Use the correct function to rm directories.
Use correct functions and fix failing tests on travis.ci. Directories
were being left behind after tests were complete as parent::tearDown()
restores debug which allows directory to be created in teardown. Shuffle
parent and clear() calls to prevent this.
4d3d54f
@markstory markstory Add tests for enable/disable/enabled e600e54
@markstory markstory Fix coding standards errors. 0ecdd6f
@markstory
Owner

All non CS tests are now passing on this branch.

@lorenzo lorenzo merged commit 53b3d6c into cakephp:3.0
@markstory markstory deleted the markstory:3.0-cache-log-config-changes branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 17, 2013
  1. @markstory

    Expand abilities of Cache::config()

    markstory authored
    Allow various types of configuration to be provided. The new variants
    allow for simpler dependency injection and still afford configuration
    files.
    
    Reconfiguring an adapter is now not allowed, as the configuration
    changes were never propagated to the adapter. This results in a silent
    failure that is hard to detect.
    
    Move/remove tests around to make sense with the new API's
  2. @markstory

    Update failing cache tests.

    markstory authored
    Remove 'engine' from the settings in each engine. It provides no value
    and is never referenced outside of tests.
  3. @markstory

    Cache should remove old settings when upgrading settings

    markstory authored
    When converting engine => className the old key should be removed.
  4. @markstory

    Update Log::config() to support more formats.

    markstory authored
    Make Log::config() accept the same data as Cache.
  5. @markstory
  6. @markstory

    Remove Cache.disable from Configure.

    markstory authored
    Give it a set of named methods on Cache. This configure value always
    felt out of place and fits much better with Cache's feature set.
  7. @markstory

    Use the correct function to rm directories.

    markstory authored
    Use correct functions and fix failing tests on travis.ci. Directories
    were being left behind after tests were complete as parent::tearDown()
    restores debug which allows directory to be created in teardown. Shuffle
    parent and clear() calls to prevent this.
  8. @markstory
  9. @markstory
This page is out of date. Refresh to see the latest.
Showing with 440 additions and 264 deletions.
  1. +1 −1  App/Config/cache.php
  2. +88 −17 lib/Cake/Cache/Cache.php
  3. +6 −3 lib/Cake/Cache/CacheRegistry.php
  4. +0 −1  lib/Cake/Cache/Engine/ApcEngine.php
  5. +0 −1  lib/Cake/Cache/Engine/FileEngine.php
  6. +0 −1  lib/Cake/Cache/Engine/MemcacheEngine.php
  7. +0 −1  lib/Cake/Cache/Engine/RedisEngine.php
  8. +0 −1  lib/Cake/Cache/Engine/WincacheEngine.php
  9. +0 −1  lib/Cake/Cache/Engine/XcacheEngine.php
  10. +2 −1  lib/Cake/Console/Command/BakeShell.php
  11. +4 −4 lib/Cake/Console/Command/SchemaShell.php
  12. +2 −1  lib/Cake/Console/Command/Task/BakeTask.php
  13. +1 −1  lib/Cake/Console/Command/UpgradeShell.php
  14. +2 −2 lib/Cake/Console/Shell.php
  15. +1 −1  lib/Cake/Console/Templates/skel/Config/cache.php
  16. +62 −23 lib/Cake/Log/Log.php
  17. +5 −2 lib/Cake/Log/LogEngineRegistry.php
  18. +3 −7 lib/Cake/Test/TestCase/BasicsTest.php
  19. +119 −137 lib/Cake/Test/TestCase/Cache/CacheTest.php
  20. +2 −1  lib/Cake/Test/TestCase/Cache/Engine/ApcEngineTest.php
  21. +45 −10 lib/Cake/Test/TestCase/Cache/Engine/FileEngineTest.php
  22. +3 −4 lib/Cake/Test/TestCase/Cache/Engine/MemcacheEngineTest.php
  23. +1 −2  lib/Cake/Test/TestCase/Cache/Engine/RedisEngineTest.php
  24. +1 −1  lib/Cake/Test/TestCase/Cache/Engine/WincacheEngineTest.php
  25. +1 −2  lib/Cake/Test/TestCase/Cache/Engine/XcacheEngineTest.php
  26. +1 −1  lib/Cake/Test/TestCase/Console/ShellTest.php
  27. +19 −9 lib/Cake/Test/TestCase/Core/ConfigureTest.php
  28. +61 −9 lib/Cake/Test/TestCase/Log/LogTest.php
  29. +1 −8 lib/Cake/Test/TestCase/Model/ModelWriteTest.php
  30. +2 −4 lib/Cake/Test/TestCase/Network/Email/EmailTest.php
  31. +3 −5 lib/Cake/Test/TestCase/Routing/DispatcherTest.php
  32. +2 −1  lib/Cake/Test/TestCase/View/Helper/CacheHelperTest.php
  33. +2 −1  lib/Cake/basics.php
View
2  App/Config/cache.php
@@ -21,7 +21,7 @@
* Turn off all caching application-wide.
*
*/
- //Configure::write('Cache.disable', true);
+ // Cache::disable();
/**
* Enable cache checking.
View
105 lib/Cake/Cache/Cache.php
@@ -83,6 +83,13 @@
class Cache {
/**
+ * Flag for tracking whether or not caching is enabled.
+ *
+ * @var boolean
+ */
+ protected static $_enabled = true;
+
+/**
* Configuraiton backup.
*
* Keeps the permanent/default settings for each cache engine.
@@ -125,25 +132,62 @@ class Cache {
/**
* 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.
+ * or read existing configuration.
*
* To change an adapter's configuration at runtime, first drop the adapter and then
* reconfigure it.
*
* Adapters will not be constructed until the first operation is done.
*
+ * ### Usage
+ *
+ * Reading config data back:
+ *
+ * `Cache::config('default');`
+ *
+ * Setting a cache engine up.
+ *
+ * `Cache::config('default', $settings);`
+ *
+ * Injecting a constructed adapter in:
+ *
+ * `Cache::config('default', $instance);`
+ *
+ * Using a factory function to get an adapter:
+ *
+ * `Cache::config('default', function () { return new FileEngine(); });`
+ *
+ * Configure multiple adapters at once:
+ *
+ * `Cache::config($arrayOfConfig);`
+ *
* @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
+ * @return mixed null when adding configuration and an array of configuration data when reading.
+ * @throws Cake\Error\Exception When trying to modify an existing config.
*/
public static function config($key, $config = null) {
- if ($config !== null && is_string($key)) {
- static::$_config[$key] = $config;
+ // Read config.
+ if ($config === null && is_string($key)) {
+ return isset(static::$_config[$key]) ? static::$_config[$key] : null;
+ }
+ if ($config === null && is_array($key)) {
+ foreach ($key as $name => $settings) {
+ static::config($name, $settings);
+ }
return;
}
-
- static::$_config = array_merge(static::$_config, $key);
+ if (isset(static::$_config[$key])) {
+ throw new Error\Exception(__d('cake_dev', 'Cannot reconfigure existing adapter "%s"', $key));
@lorenzo Owner
lorenzo added a note

I think this is a great idea and for the same reasons I think Cache::set() should get the axe

@markstory Owner

I'm glad I'm not the only person who hates Cache::set(). That method is all sorts of silly. I'm on board for removing it if others are. It adds a pile of complexity to Cache for almost no benefit other than making the calling code more complicated as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+ if (is_object($config)) {
+ $config = ['className' => $config];
+ }
+ if (isset($config['engine']) && empty($config['className'])) {
+ $config['className'] = $config['engine'];
+ unset($config['engine']);
+ }
+ static::$_config[$key] = $config;
}
/**
@@ -156,13 +200,11 @@ protected static function _buildEngine($name) {
if (empty(static::$_registry)) {
static::$_registry = new CacheRegistry();
}
- if (empty(static::$_config[$name]['engine'])) {
+ if (empty(static::$_config[$name]['className'])) {
throw new Error\Exception(__d('cake_dev', 'The "%s" cache configuration does not exist.', $name));
}
$config = static::$_config[$name];
- $config['className'] = $config['engine'];
-
static::$_registry->load($name, $config);
if (!empty($config['groups'])) {
@@ -186,18 +228,16 @@ public static function configured() {
/**
* Drops a constructed cache engine.
*
- * The engine's configuration will remain in Configure. If you wish to re-configure a
- * cache engine you should drop it, change configuration and then re-use it.
+ * If you wish to re-configure a cache engine you should drop it,
+ * change configuration and then re-use it.
*
* @param string $config A currently configured cache config you wish to remove.
* @return boolean success of the removal, returns false when the config does not exist.
*/
public static function drop($config) {
- if (!isset(static::$_registry->{$config})) {
- return false;
+ if (isset(static::$_registry->{$config})) {
+ static::$_registry->unload($config);
}
-
- static::$_registry->unload($config);
unset(static::$_config[$config], static::$_restore[$config]);
return true;
}
@@ -212,7 +252,7 @@ public static function drop($config) {
* @return Cake\Cache\Engine
*/
public static function engine($config) {
- if (Configure::read('Cache.disable')) {
+ if (!static::$_enabled) {
return false;
}
@@ -561,4 +601,35 @@ public static function groupConfigs($group = null) {
throw new Error\Exception(__d('cake_dev', 'Invalid cache group %s', $group));
}
+/**
+ * Re-enable caching.
+ *
+ * If caching has been disabled with Cache::disable() this method will reverse that effect.
+ *
+ * @return void
+ */
+ public static function enable() {
+ static::$_enabled = true;
+ }
+
+/**
+ * Disable caching.
+ *
+ * When disabled all cache operations will return null.
+ *
+ * @return void
+ */
+ public static function disable() {
+ static::$_enabled = false;
+ }
+
+/**
+ * Check whether or not caching is enabled.
+ *
+ * @return boolean
+ */
+ public static function enabled() {
+ return static::$_enabled;
+ }
+
}
View
9 lib/Cake/Cache/CacheRegistry.php
@@ -58,18 +58,21 @@ protected function _throwMissingClassError($class, $plugin) {
* Create the cache engine instance.
*
* Part of the template method for Cake\Utility\ObjectRegistry::load()
- * @param string|CacheEngine $class The classname or object to make.
+ * @param string|CacheEngine|Closure $class The classname or object to make, or a closure factory.
* @param array $settings An array of settings to use for the cache engine.
* @return CacheEngine The constructed CacheEngine class.
* @throws Cake\Error\Exception when an object doesn't implement
* the correct interface.
*/
protected function _create($class, $settings) {
- if (is_object($class)) {
+ $isObject = is_object($class);
+ if ($isObject && $class instanceof \Closure) {
+ $instance = $class();
+ } elseif ($isObject) {
$instance = $class;
}
- unset($settings['engine'], $settings['className']);
+ unset($settings['className']);
if (!isset($instance)) {
$instance = new $class($settings);
}
View
1  lib/Cake/Cache/Engine/ApcEngine.php
@@ -45,7 +45,6 @@ public function init($settings = array()) {
if (!isset($settings['prefix'])) {
$settings['prefix'] = Inflector::slug(APP_DIR) . '_';
}
- $settings += array('engine' => __CLASS__);
parent::init($settings);
return function_exists('apc_dec');
}
View
1  lib/Cake/Cache/Engine/FileEngine.php
@@ -75,7 +75,6 @@ class FileEngine extends CacheEngine {
*/
public function init($settings = array()) {
$settings += array(
- 'engine' => __CLASS__,
'path' => CACHE,
'prefix' => 'cake_',
'lock' => true,
View
1  lib/Cake/Cache/Engine/MemcacheEngine.php
@@ -70,7 +70,6 @@ public function init($settings = array()) {
$settings['prefix'] = Inflector::slug(APP_DIR) . '_';
}
$settings += array(
- 'engine' => __CLASS__,
'servers' => array('127.0.0.1'),
'compress' => false,
'persistent' => true
View
1  lib/Cake/Cache/Engine/RedisEngine.php
@@ -57,7 +57,6 @@ public function init($settings = array()) {
return false;
}
parent::init(array_merge(array(
- 'engine' => __CLASS__,
'prefix' => null,
'server' => '127.0.0.1',
'port' => 6379,
View
1  lib/Cake/Cache/Engine/WincacheEngine.php
@@ -47,7 +47,6 @@ public function init($settings = array()) {
if (!isset($settings['prefix'])) {
$settings['prefix'] = Inflector::slug(APP_DIR) . '_';
}
- $settings += array('engine' => __CLASS__);
parent::init($settings);
return function_exists('wincache_ucache_info');
}
View
1  lib/Cake/Cache/Engine/XcacheEngine.php
@@ -51,7 +51,6 @@ class XcacheEngine extends CacheEngine {
public function init($settings = array()) {
if (php_sapi_name() !== 'cli') {
parent::init(array_merge(array(
- 'engine' => __CLASS__,
'prefix' => Inflector::slug(APP_DIR) . '_',
'PHP_AUTH_USER' => 'user',
'PHP_AUTH_PW' => 'password'
View
3  lib/Cake/Console/Command/BakeShell.php
@@ -14,6 +14,7 @@
*/
namespace Cake\Console\Command;
+use Cake\Cache\Cache;
use Cake\Console\Shell;
use Cake\Core\App;
use Cake\Core\Configure;
@@ -54,7 +55,7 @@ class BakeShell extends Shell {
public function startup() {
parent::startup();
Configure::write('debug', 2);
- Configure::write('Cache.disable', 1);
+ Cache::disable();
$task = Inflector::classify($this->command);
if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig'))) {
View
8 lib/Cake/Console/Command/SchemaShell.php
@@ -15,6 +15,7 @@
namespace Cake\Console\Command;
use Cake\Console\Shell;
+use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Model\ConnectionManager;
use Cake\Model\Schema;
@@ -60,7 +61,7 @@ public function startup() {
throw new \Cake\Error\Exception('Schema shell is not working at this time.');
- Configure::write('Cache.disable', 1);
+ Cache::disable();
$name = $path = $connection = $plugin = null;
if (!empty($this->params['name'])) {
@@ -152,13 +153,12 @@ public function generate() {
}
}
- $cacheDisable = Configure::read('Cache.disable');
- Configure::write('Cache.disable', true);
+ Cache::enable();
$content = $this->Schema->read($options);
$content['file'] = $this->params['file'];
- Configure::write('Cache.disable', $cacheDisable);
+ Cache::disable();
if (!empty($this->params['exclude']) && !empty($content)) {
$excluded = String::tokenize($this->params['exclude']);
View
3  lib/Cake/Console/Command/Task/BakeTask.php
@@ -19,6 +19,7 @@
namespace Cake\Console\Command\Task;
use Cake\Console\Shell;
+use Cake\Cache\Cache;
use Cake\Core\Configure;
/**
@@ -57,7 +58,7 @@ class BakeTask extends Shell {
*/
public function startup() {
Configure::write('debug', 2);
- Configure::write('Cache.disable', 1);
+ Cache::disable();
parent::startup();
}
View
2  lib/Cake/Console/Command/UpgradeShell.php
@@ -134,7 +134,7 @@ protected function _replaceUses($file) {
$matches = $this->_mapClassName($matches);
if (count($matches) === 4) {
$use = $matches[3] . '\\' . $matches[2] . '\\' . $matches[1];
- } else if ($matches[2] == 'Vendor') {
+ } elseif ($matches[2] == 'Vendor') {
$this->out(
__d('cake_console', '<info>Skip %s as it is a vendor library.</info>', $matches[1]),
1,
View
4 lib/Cake/Console/Shell.php
@@ -880,9 +880,9 @@ protected function _pluginPath($pluginName) {
* @return void
*/
protected function _useLogger($enable = true) {
+ Log::drop('stdout');
+ Log::drop('stderr');
if (!$enable) {
- Log::drop('stdout');
- Log::drop('stderr');
return;
}
$stdout = new ConsoleLog([
View
2  lib/Cake/Console/Templates/skel/Config/cache.php
@@ -20,7 +20,7 @@
* Turn off all caching application-wide.
*
*/
- //Configure::write('Cache.disable', true);
+ // Cache::disable();
/**
* Enable cache checking.
View
85 lib/Cake/Log/Log.php
@@ -28,17 +28,17 @@
* A sample configuration would look like:
*
* {{{
- * Log::config('my_log', ['engine' => 'FileLog']);
+ * Log::config('my_log', ['className' => 'FileLog']);
* }}}
*
- * You can define the engine as any fully namespaced classname or use a short hand
+ * You can define the className as any fully namespaced classname or use a short hand
* classname to use loggers in the `App\Log\Engine` & `Cake\Log\Engine` namespaces.
* You can also use plugin short hand to use logging classes provided by plugins.
*
* Log adapters are required to implement `Cake\Log\LogInterface`, and there is a
* built-in base class (`Cake\Log\Engine\BaseLog`) that can be used for custom loggers.
*
- * Outside of the `engine` key, all other configuration values will be passed to the
+ * Outside of the `className` key, all other configuration values will be passed to the
* logging adapter's constructor as an array.
*
* ### Logging levels
@@ -48,7 +48,7 @@
*
* {{{
* Log::config('default', [
- * 'engine' => 'File',
+ * 'className' => 'File',
* 'path' => LOGS,
* 'levels' => ['error', 'critical', 'alert', 'emergency']
* ]);
@@ -66,7 +66,7 @@
*
* {{{
* Log::config('payments', [
- * 'engine' => 'File',
+ * 'className' => 'File',
* 'scopes' => ['payment', 'order']
* ]);
* }}}
@@ -226,42 +226,81 @@ public static function levels() {
}
/**
- * Set configuration information for loggers.
+ * This method can be used to define logging adapters for an application
+ * or read existing configuration.
*
- * This method can be used to define loggers for an application
- * during the bootstrapping process. You can use this method to add new loggers
- * at runtime as well. New loggers will be constructed upon the next write.
- *
- * To change a logger's configuration at runtime, first drop the logger and then
+ * To change an adapter's configuration at runtime, first drop the adapter and then
* reconfigure it.
*
* Loggers will not be constructed until the first log message is written.
*
- * @param string|array $key The name of the logger, or an array of multiple logger configs.
- * @param array $config An array of name => config data for loggers.
- * @return void
+ * ### Usage
+ *
+ * Reading config data back:
+ *
+ * `Log::config('default');`
+ *
+ * Setting a cache engine up.
+ *
+ * `Log::config('default', $settings);`
+ *
+ * Injecting a constructed adapter in:
+ *
+ * `Log::config('default', $instance);`
+ *
+ * Using a factory function to get an adapter:
+ *
+ * `Log::config('default', function () { return new FileLog(); });`
+ *
+ * Configure multiple adapters at once:
+ *
+ * `Log::config($arrayOfConfig);`
+ *
+ * @param string|array $key The name of the logger config, or an array of multiple configs.
+ * @param array $config An array of name => config data for adapter.
+ * @return mixed null when adding configuration and an array of configuration data when reading.
+ * @throws Cake\Error\Exception When trying to modify an existing config.
* @see App/Config/logging.php
*/
public static function config($key, $config = null) {
- static::$_dirtyConfig = true;
- if ($config !== null && is_string($key)) {
- static::$_config[$key] = (array) $config;
+ // Read config.
+ if ($config === null && is_string($key)) {
+ return isset(static::$_config[$key]) ? static::$_config[$key] : null;
+ }
+ if ($config === null && is_array($key)) {
+ foreach ($key as $name => $settings) {
+ static::config($name, $settings);
+ }
return;
}
-
- static::$_config = array_merge(static::$_config, $key);
+ if (isset(static::$_config[$key])) {
+ throw new Error\Exception(__d('cake_dev', 'Cannot reconfigure existing adapter "%s"', $key));
+ }
+ static::$_dirtyConfig = true;
+ if (is_object($config)) {
+ $config = ['className' => $config];
+ }
+ if (isset($config['engine']) && empty($config['className'])) {
+ $config['className'] = $config['engine'];
+ unset($config['engine']);
+ }
+ static::$_config[$key] = $config;
}
/**
- * Removes a stream from the active streams. Once a stream has been removed
- * it will no longer have messages sent to it.
+ * Removes a stream from the active streams.
+ *
+ * Once a stream has been removed it will no longer have messages sent to it.
+ * The original configuration data will also be removed. You can use this method
+ * when reconfiguring a logger or when you want to remove a logger.
*
- * @param string $streamName Key name of a configured stream to remove.
+ * @param string $streamName Key name of a configured logger to remove.
* @return void
*/
public static function drop($streamName) {
static::_init();
static::$_registry->unload($streamName);
+ unset(static::$_config[$streamName]);
}
/**
@@ -285,7 +324,7 @@ public static function enabled($streamName) {
* @deprecated This method will be removed in 3.0 stable.
*/
public static function enable($streamName) {
- throw new Error\Exception(__d('cake_dev', 'Log::enable() is deprecated. Use Log::engine() instead.'));
+ throw new Error\Exception(__d('cake_dev', 'Log::enable() is deprecated. Use Log::config() instead.'));
}
/**
View
7 lib/Cake/Log/LogEngineRegistry.php
@@ -57,14 +57,17 @@ protected function _throwMissingClassError($class, $plugin) {
* Create the logger instance.
*
* Part of the template method for Cake\Utility\ObjectRegistry::load()
- * @param string|LogInterface $class The classname or object to make.
+ * @param string|LogInterface|Closure $class The classname or object to make, or a closure factory
* @param array $settings An array of settings to use for the logger.
* @return LogEngine The constructed logger class.
* @throws Cake\Error\Exception when an object doesn't implement
* the correct interface.
*/
protected function _create($class, $settings) {
- if (is_object($class)) {
+ $isObject = is_object($class);
+ if ($isObject && $class instanceof \Closure) {
+ $instance = $class();
+ } elseif ($isObject) {
$instance = $class;
}
View
10 lib/Cake/Test/TestCase/BasicsTest.php
@@ -19,6 +19,7 @@
*/
namespace Cake\Test\TestCase;
+use Cake\Cache\Cache;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Log\Log;
@@ -281,17 +282,14 @@ public function testAm() {
* @return void
*/
public function testCache() {
- $_cacheDisable = Configure::read('Cache.disable');
- $this->skipIf($_cacheDisable, 'Cache is disabled, skipping cache() tests.');
-
- Configure::write('Cache.disable', true);
+ Cache::disable();
$result = cache('basics_test', 'simple cache write');
$this->assertNull($result);
$result = cache('basics_test');
$this->assertNull($result);
- Configure::write('Cache.disable', false);
+ Cache::enable();
$result = cache('basics_test', 'simple cache write');
$this->assertTrue((boolean)$result);
$this->assertTrue(file_exists(CACHE . 'basics_test'));
@@ -306,8 +304,6 @@ public function testCache() {
sleep(2);
$result = cache('basics_test', null, '+1 second');
$this->assertNull($result);
-
- Configure::write('Cache.disable', $_cacheDisable);
}
/**
View
256 lib/Cake/Test/TestCase/Cache/CacheTest.php
@@ -15,6 +15,7 @@
namespace Cake\Test\TestCase\Cache;
use Cake\Cache\Cache;
+use Cake\Cache\Engine\FileEngine;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
@@ -34,41 +35,25 @@ class CacheTest extends TestCase {
*/
public function setUp() {
parent::setUp();
- Configure::write('Cache.disable', false);
-
- Cache::drop('tests');
- Cache::config('default', [
- 'engine' => 'File',
- 'path' => TMP . 'tests'
- ]);
+ Cache::enable();
}
/**
- * testEngine method
+ * tearDown method
*
* @return void
*/
- public function testEngine() {
- $settings = [
- 'engine' => 'File',
- 'path' => TMP . 'tests',
- 'prefix' => 'cake_test_'
- ];
- Cache::config('tests', $settings);
- $engine = Cache::engine('tests');
- $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
+ public function tearDown() {
+ parent::tearDown();
+ Cache::drop('tests');
}
-/**
- * testConfigInvalidEngine method
- *
- * @expectedException Cake\Error\Exception
- * @return void
- */
- public function testConfigInvalidEngine() {
- $settings = array('engine' => 'Imaginary');
- Cache::config('tests', $settings);
- Cache::engine('tests');
+ protected function _configCache() {
+ Cache::config('tests', [
+ 'engine' => 'File',
+ 'path' => TMP,
+ 'prefix' => 'test_'
+ ]);
}
/**
@@ -77,11 +62,8 @@ public function testConfigInvalidEngine() {
* @return void
*/
public function testNonFatalErrorsWithCachedisable() {
- Configure::write('Cache.disable', true);
- Cache::config('tests', [
- 'engine' => 'File',
- 'path' => TMP, 'prefix' => 'error_test_'
- ]);
+ Cache::disable();
+ $this->_configCache();
Cache::write('no_save', 'Noooo!', 'tests');
Cache::read('no_save', 'tests');
@@ -119,26 +101,6 @@ public function testConfigWithLibAndPluginEngines() {
}
/**
- * Test reading from a config that is undefined.
- *
- * @expectedException PHPUnit_Framework_Error_Warning
- * @return void
- */
- public function testInvalidConfig() {
- // In debug mode it would auto create the folder.
- Configure::write('debug', 0);
-
- Cache::config('tests', array(
- 'engine' => 'File',
- 'duration' => '+1 year',
- 'prefix' => 'testing_invalid_',
- 'path' => 'data/',
- 'serialize' => true,
- ));
- Cache::read('Test', 'tests');
- }
-
-/**
* Test write from a config that is undefined.
*
* @expectedException Cake\Error\Exception
@@ -169,57 +131,96 @@ public function testDecrementNonExistingConfig() {
}
/**
+ * Data provider for valid config data sets.
+ *
+ * @return array
+ */
+ public static function configProvider() {
+ return [
+ 'Array of data using engine key.' => [[
+ 'engine' => 'File',
+ 'path' => TMP . 'tests',
+ 'prefix' => 'cake_test_'
+ ]],
+ 'Array of data using classname key.' => [[
+ 'className' => 'File',
+ 'path' => TMP . 'tests',
+ 'prefix' => 'cake_test_'
+ ]],
+ 'Direct instance' => [new FileEngine()],
+ 'Closure factory' => [function () {
+ return new FileEngine();
+ }],
+ ];
+ }
+/**
+ * testConfig method
+ *
+ * @dataProvider configProvider
+ * @return void
+ */
+ public function testConfigVariants($settings) {
+ $this->assertNotContains('test', Cache::configured(), 'test config should not exist.');
+ Cache::config('tests', $settings);
+
+ $engine = Cache::engine('tests');
+ $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
+ $this->assertContains('tests', Cache::configured());
+ }
+
+/**
+ * testConfigInvalidEngine method
+ *
+ * @expectedException Cake\Error\Exception
+ * @return void
+ */
+ public function testConfigInvalidEngine() {
+ $settings = array('engine' => 'Imaginary');
+ Cache::config('test', $settings);
+ Cache::engine('test');
+ }
+
+/**
* test that trying to configure classes that don't extend CacheEngine fail.
*
* @expectedException Cake\Error\Exception
* @return void
*/
- public function testAttemptingToConfigureANonCacheEngineClass() {
+ public function testConfigInvalidObject() {
$this->getMock('\StdClass', array(), array(), 'RubbishEngine');
- Cache::config('tests', array(
+ Cache::config('test', array(
'engine' => '\RubbishEngine'
));
Cache::engine('tests');
}
/**
- * Test that engine() can be used to inject instances.
+ * Ensure you cannot reconfigure a cache adapter.
*
+ * @expectedException Cake\Error\Exception
* @return void
*/
- public function testSetEngineValid() {
- $engine = $this->getMockForAbstractClass('\Cake\Cache\CacheEngine');
- Cache::config('test', ['engine' => $engine]);
- $this->assertSame($engine, Cache::engine('test'));
+ public function testConfigErrorOnReconfigure() {
+ Cache::config('tests', ['engine' => 'File', 'path' => TMP]);
+ Cache::config('tests', ['engine' => 'Apc']);
}
/**
- * test that calling config() sets the 'default' configuration up.
+ * Test reading configuration.
*
* @return void
*/
- public function testConfigSettingDefaultConfigKey() {
- Cache::config('tests', [
+ public function testConfigRead() {
+ $settings = [
'engine' => 'File',
- 'prefix' => 'tests_'
- ]);
-
- 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');
- $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', 'tests');
- $this->assertEquals('I am cached', $result);
-
- Cache::delete('value_one', 'tests');
- Cache::delete('value_one', 'default');
+ 'path' => TMP,
+ 'prefix' => 'cake_'
+ ];
+ Cache::config('tests', $settings);
+ $expected = $settings;
+ $expected['className'] = $settings['engine'];
+ unset($expected['engine']);
+ $this->assertEquals($expected, Cache::config('tests'));
}
/**
@@ -307,11 +308,7 @@ public function testDrop() {
Configure::write('App.namespace', 'TestApp');
$result = Cache::drop('some_config_that_does_not_exist');
- $this->assertFalse($result);
-
- Cache::engine('default');
- $result = Cache::drop('default');
- $this->assertTrue($result, 'Built engines should be dropped');
+ $this->assertTrue($result, 'Drop should succeed.');
Cache::config('unconfigTest', [
'engine' => 'TestAppCache'
@@ -320,56 +317,30 @@ public function testDrop() {
'TestApp\Cache\Engine\TestAppCacheEngine',
Cache::engine('unconfigTest')
);
-
$this->assertTrue(Cache::drop('unconfigTest'));
}
/**
- * Test that dropping a cache config refreshes its configuration and
- * creates a new instance.
- *
- * @return void
- */
- public function testDropChangeConfig() {
- Cache::config('tests', [
- 'engine' => 'File',
- ]);
- $result = Cache::engine('tests');
- $settings = Cache::settings('tests');
-
- $this->assertEquals(CACHE, $settings['path']);
- $id = spl_object_hash($result);
-
- Cache::drop('tests');
-
- Cache::config('tests', [
- 'engine' => 'File',
- 'extra' => 'value'
- ]);
- $result = Cache::engine('tests');
- $this->assertNotEquals($id, spl_object_hash($result));
- }
-
-/**
* testWriteEmptyValues method
*
* @return void
*/
public function testWriteEmptyValues() {
- Cache::write('App.falseTest', false);
- $this->assertSame(Cache::read('App.falseTest'), false);
+ $this->_configCache();
+ Cache::write('App.falseTest', false, 'tests');
+ $this->assertSame(Cache::read('App.falseTest', 'tests'), false);
- Cache::write('App.trueTest', true);
- $this->assertSame(Cache::read('App.trueTest'), true);
+ Cache::write('App.trueTest', true, 'tests');
+ $this->assertSame(Cache::read('App.trueTest', 'tests'), true);
- Cache::write('App.nullTest', null);
- $this->assertSame(Cache::read('App.nullTest'), null);
+ Cache::write('App.nullTest', null, 'tests');
+ $this->assertSame(Cache::read('App.nullTest', 'tests'), null);
- Cache::write('App.zeroTest', 0);
- $this->assertSame(Cache::read('App.zeroTest'), 0);
+ Cache::write('App.zeroTest', 0, 'tests');
+ $this->assertSame(Cache::read('App.zeroTest', 'tests'), 0);
- Cache::write('App.zeroTest2', '0');
- $this->assertSame(Cache::read('App.zeroTest2'), '0');
+ Cache::write('App.zeroTest2', '0', 'tests');
+ $this->assertSame(Cache::read('App.zeroTest2', 'tests'), '0');
}
/**
@@ -407,7 +378,7 @@ public function testWriteTriggerError() {
* @return void
*/
public function testCacheDisable() {
- Configure::write('Cache.disable', false);
+ Cache::enable();
Cache::config('test_cache_disable_1', [
'engine' => 'File',
'path' => TMP . 'tests'
@@ -416,17 +387,17 @@ public function testCacheDisable() {
$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);
+ Cache::disable();
$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);
+ Cache::enable();
$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::disable();
Cache::config('test_cache_disable_2', [
'engine' => 'File',
'path' => TMP . 'tests'
@@ -435,12 +406,12 @@ public function testCacheDisable() {
$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);
+ Cache::enable();
$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);
+ Cache::disable();
$this->assertFalse(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
$this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
@@ -452,22 +423,21 @@ public function testCacheDisable() {
* @return void
*/
public function testSet() {
- $_cacheSet = Cache::set();
+ $this->_configCache();
- Cache::set(array('duration' => '+1 year'));
- $data = Cache::read('test_cache');
+ Cache::set(array('duration' => '+1 year'), 'tests');
+ $data = Cache::read('test_cache', 'tests');
$this->assertFalse($data);
$data = 'this is just a simple test of the cache system';
- $write = Cache::write('test_cache', $data);
+ $write = Cache::write('test_cache', $data, 'tests');
$this->assertTrue($write);
- Cache::set(array('duration' => '+1 year'));
- $data = Cache::read('test_cache');
+ Cache::set(array('duration' => '+1 year'), 'tests');
+ $data = Cache::read('test_cache', 'tests');
$this->assertEquals('this is just a simple test of the cache system', $data);
- Cache::delete('test_cache');
- Cache::set($_cacheSet);
+ Cache::delete('test_cache', 'tests');
}
/**
@@ -526,4 +496,16 @@ public function testSetOnAlternateConfigs() {
$this->assertEquals(strtotime('+1 year') - time(), $settings['duration']);
}
+/**
+ * Test toggling enabled state of cache.
+ *
+ * @return void
+ */
+ public function testEnableDisableEnabled() {
+ $this->assertNull(Cache::enable());
+ $this->assertTrue(Cache::enabled(), 'Should be on');
+ $this->assertNull(Cache::disable());
+ $this->assertFalse(Cache::enabled(), 'Should be off');
+ }
+
}
View
3  lib/Cake/Test/TestCase/Cache/Engine/ApcEngineTest.php
@@ -42,7 +42,7 @@ public function setUp() {
$this->skipIf(!ini_get('apc.enable_cli'), 'APC is not enabled for the CLI.');
}
- Configure::write('Cache.disable', false);
+ Cache::enable();
Cache::config('apc', ['engine' => 'Apc', 'prefix' => 'cake_']);
}
@@ -86,6 +86,7 @@ public function testReadAndWriteCache() {
* @return void
*/
public function testReadWriteDurationZero() {
+ Cache::drop('apc');
Cache::config('apc', ['engine' => 'Apc', 'duration' => 0, 'prefix' => 'cake_']);
Cache::write('zero', 'Should save', 'apc');
sleep(1);
View
55 lib/Cake/Test/TestCase/Cache/Engine/FileEngineTest.php
@@ -44,11 +44,13 @@ class FileEngineTest extends TestCase {
*/
public function setUp() {
parent::setUp();
- Configure::write('Cache.disable', false);
+ Cache::enable();
+ Cache::drop('file_test');
Cache::config('file_test', [
'engine' => 'File',
- 'path' => CACHE
+ 'path' => TMP . 'tests',
]);
+ Cache::clear(false, 'file_test');
}
/**
@@ -57,7 +59,6 @@ public function setUp() {
* @return void
*/
public function tearDown() {
- Cache::clear(false, 'file_test');
Cache::drop('file_test');
Cache::drop('file_groups');
Cache::drop('file_groups2');
@@ -82,7 +83,7 @@ public function testReadAndWriteCache() {
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('test', $data, 'file_test');
- $this->assertTrue(file_exists(CACHE . 'cake_test'));
+ $this->assertTrue(file_exists(TMP . 'tests/cake_test'));
$result = Cache::read('test', 'file_test');
$expecting = $data;
@@ -162,6 +163,7 @@ public function testDeleteCache() {
* @return void
*/
public function testSerialize() {
+ Cache::drop('file_test');
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');
@@ -181,6 +183,7 @@ public function testSerialize() {
* @return void
*/
public function testClear() {
+ Cache::drop('file_test');
Cache::config('file_test', ['engine' => 'File', 'duration' => 1]);
$data = 'this is a test of the emergency broadcasting system';
@@ -284,7 +287,7 @@ public function testClearWithNoKeys() {
public function testKeyPath() {
$result = Cache::write('views.countries.something', 'here', 'file_test');
$this->assertTrue($result);
- $this->assertTrue(file_exists(CACHE . 'cake_views_countries_something'));
+ $this->assertTrue(file_exists(TMP . 'tests/cake_views_countries_something'));
$result = Cache::read('views.countries.something', 'file_test');
$this->assertEquals('here', $result);
@@ -349,13 +352,18 @@ public function testRemoveWindowsSlashesFromCache() {
* @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'));
+ Cache::drop('file_test');
+ Cache::config('file_test', array(
+ 'className' => 'File',
+ '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'");
@@ -370,13 +378,40 @@ public function testWriteQuotedString() {
*/
public function testPathDoesNotExist() {
$this->skipIf(is_dir(TMP . 'tests' . DS . 'autocreate'), 'Cannot run if test directory exists.');
+ Configure::write('debug', 2);
- Cache::config('autocreate', array(
+ Cache::drop('file_test');
+ Cache::config('file_test', array(
'engine' => 'File',
- 'path' => TMP . 'tests' . DS . 'autocreate'
+ 'path' => TMP . 'tests/autocreate'
));
+ Cache::read('Test', 'file_test');
+
+ $this->assertTrue(file_exists(TMP . 'tests/autocreate'), 'Dir should exist.');
- Cache::drop('autocreate');
+ // Cleanup
+ rmdir(TMP . 'tests/autocreate');
+ }
+
+/**
+ * Test that under debug 0 directories do not get made.
+ *
+ * @expectedException PHPUnit_Framework_Error_Warning
+ * @return void
+ */
+ public function testPathDoesNotExistDebugOff() {
+ $this->skipIf(is_dir(TMP . 'tests/autocreate'), 'Cannot run if test directory exists.');
+ Configure::write('debug', 0);
+
+ Cache::drop('file_groups');
+ Cache::config('file_groups', array(
+ 'engine' => 'File',
+ 'duration' => '+1 year',
+ 'prefix' => 'testing_invalid_',
+ 'path' => TMP . 'tests/autocreate',
+ ));
+ Cache::read('Test', 'file_groups');
+ Cache::drop('file_groups');
}
/**
View
7 lib/Cake/Test/TestCase/Cache/Engine/MemcacheEngineTest.php
@@ -26,7 +26,6 @@
/**
* Class TestMemcacheEngine
*
- * @package Cake.Test.Case.Cache.Engine
*/
class TestMemcacheEngine extends MemcacheEngine {
@@ -62,9 +61,9 @@ public function setUp() {
parent::setUp();
$this->skipIf(!class_exists('Memcache'), 'Memcache is not installed or configured properly.');
- Configure::write('Cache.disable', false);
+ Cache::enable();
Cache::config('memcache', array(
- 'engine' => 'Memcache',
+ 'className' => 'Memcache',
'prefix' => 'cake_',
'duration' => 3600
));
@@ -97,7 +96,6 @@ public function testSettings() {
'servers' => array('127.0.0.1'),
'persistent' => true,
'compress' => false,
- 'engine' => 'Cake\Cache\Engine\MemcacheEngine',
'groups' => array()
);
$this->assertEquals($expecting, $settings);
@@ -378,6 +376,7 @@ public function testClear() {
* @return void
*/
public function testZeroDuration() {
+ Cache::drop('memcache');
Cache::config('memcache', [
'engine' => 'Memcache',
'prefix' => 'cake_',
View
3  lib/Cake/Test/TestCase/Cache/Engine/RedisEngineTest.php
@@ -35,7 +35,7 @@ public function setUp() {
parent::setUp();
$this->skipIf(!class_exists('Redis'), 'Redis is not installed or configured properly.');
- Configure::write('Cache.disable', false);
+ Cache::enable();
Cache::config('redis', array(
'engine' => 'Cake\Cache\Engine\RedisEngine',
'prefix' => 'cake_',
@@ -67,7 +67,6 @@ public function testSettings() {
'duration' => 3600,
'probability' => 100,
'groups' => array(),
- 'engine' => 'Cake\Cache\Engine\RedisEngine',
'server' => '127.0.0.1',
'port' => 6379,
'timeout' => 0,
View
2  lib/Cake/Test/TestCase/Cache/Engine/WincacheEngineTest.php
@@ -38,7 +38,7 @@ class WincacheEngineTest extends TestCase {
public function setUp() {
parent::setUp();
$this->skipIf(!function_exists('wincache_ucache_set'), 'Wincache is not installed or configured properly.');
- Configure::write('Cache.disable', false);
+ Cache::enable();
Cache::config('wincache', ['engine' => 'Wincache', 'prefix' => 'cake_']);
}
View
3  lib/Cake/Test/TestCase/Cache/Engine/XcacheEngineTest.php
@@ -39,7 +39,7 @@ public function setUp() {
if (!function_exists('xcache_set')) {
$this->markTestSkipped('Xcache is not installed or configured properly');
}
- Configure::write('Cache.disable', false);
+ Cache::enable();
Cache::config('xcache', ['engine' => 'Xcache', 'prefix' => 'cake_']);
}
@@ -65,7 +65,6 @@ public function testSettings() {
'prefix' => 'cake_',
'duration' => 3600,
'probability' => 100,
- 'engine' => 'Xcache',
];
$this->assertTrue(isset($settings['PHP_AUTH_USER']));
$this->assertTrue(isset($settings['PHP_AUTH_PW']));
View
2  lib/Cake/Test/TestCase/Console/ShellTest.php
@@ -833,7 +833,7 @@ public function testFileAndConsoleLogging() {
['write'],
[['types' => 'error']]
);
- Log::config('console', ['engine' => $mock]);
+ Log::config('console', $mock);
$mock->expects($this->once())
->method('write')
->with('error', $this->Shell->testMessage);
View
28 lib/Cake/Test/TestCase/Core/ConfigureTest.php
@@ -42,7 +42,7 @@ class ConfigureTest extends TestCase {
*/
public function setUp() {
parent::setUp();
- Configure::write('Cache.disable', true);
+ Cache::disable();
App::build();
App::objects('Plugin', null, true);
}
@@ -339,18 +339,23 @@ public function testLoadPlugin() {
* @return void
*/
public function testStoreAndRestore() {
- Configure::write('Cache.disable', false);
+ Cache::enable();
+ Cache::config('configure', [
+ 'className' => 'File',
+ 'path' => TMP . 'tests'
+ ]);
Configure::write('Testing', 'yummy');
- $this->assertTrue(Configure::store('store_test', 'default'));
+ $this->assertTrue(Configure::store('store_test', 'configure'));
Configure::delete('Testing');
$this->assertNull(Configure::read('Testing'));
- Configure::restore('store_test', 'default');
+ Configure::restore('store_test', 'configure');
$this->assertEquals('yummy', Configure::read('Testing'));
- Cache::delete('store_test', 'default');
+ Cache::delete('store_test', 'configure');
+ Cache::drop('configure');
}
/**
@@ -359,18 +364,23 @@ public function testStoreAndRestore() {
* @return void
*/
public function testStoreAndRestoreWithData() {
- Configure::write('Cache.disable', false);
+ Cache::enable();
+ Cache::config('configure', [
+ 'className' => 'File',
+ 'path' => TMP . 'tests'
+ ]);
Configure::write('testing', 'value');
- Configure::store('store_test', 'default', array('store_test' => 'one'));
+ Configure::store('store_test', 'configure', array('store_test' => 'one'));
Configure::delete('testing');
$this->assertNull(Configure::read('store_test'), 'Calling store with data shouldn\'t modify runtime.');
- Configure::restore('store_test', 'default');
+ Configure::restore('store_test', 'configure');
$this->assertEquals('one', Configure::read('store_test'));
$this->assertNull(Configure::read('testing'), 'Values that were not stored are not restored.');
- Cache::delete('store_test', 'default');
+ Cache::delete('store_test', 'configure');
+ Cache::drop('configure');
}
/**
View
70 lib/Cake/Test/TestCase/Log/LogTest.php
@@ -121,27 +121,79 @@ public function testDrop() {
}
/**
- * Test that engine() throws an exception when adding an
+ * Provider for config() tests.
+ *
+ * @return array
+ */
+ public static function configProvider() {
+ return [
+ 'Array of data using engine key.' => [[
+ 'engine' => 'File',
+ 'path' => TMP . 'tests',
+ ]],
+ 'Array of data using classname key.' => [[
+ 'className' => 'File',
+ 'path' => TMP . 'tests',
+ ]],
+ 'Direct instance' => [new FileLog()],
+ 'Closure factory' => [function () {
+ return new FileLog();
+ }],
+ ];
+ }
+
+/**
+ * Test the various config call signatures.
+ *
+ * @dataProvider configProvider
+ * @return void
+ */
+ public function testConfigVariants($settings) {
+ Log::config('test', $settings);
+ $this->assertContains('test', Log::configured());
+ $this->assertInstanceOf('Cake\Log\Engine\FileLog', Log::engine('test'));
+ Log::drop('test');
+ }
+
+/**
+ * Test that config() throws an exception when adding an
* adapter with the wrong type.
*
* @expectedException Cake\Error\Exception
* @return void
*/
- public function testEngineInjectErrorOnWrongType() {
- Log::config('test', ['engine' => new \StdClass]);
+ public function testConfigInjectErrorOnWrongType() {
+ Log::config('test', new \StdClass);
Log::info('testing');
}
/**
- * Test that config() can inject instances
+ * Test that config() can read data back
*
* @return void
*/
- public function testConfigInjectInstance() {
- $logger = new FileLog();
- Log::config('test', ['engine' => $logger]);
- $result = Log::engine('test');
- $this->assertSame($logger, $result);
+ public function testConfigRead() {
+ $config = [
+ 'engine' => 'File',
+ 'path' => LOGS
+ ];
+ Log::config('tests', $config);
+
+ $expected = $config;
+ $expected['className'] = $config['engine'];
+ unset($expected['engine']);
+ $this->assertSame($expected, Log::config('tests'));
+ }
+
+/**
+ * Ensure you cannot reconfigure a log adapter.
+ *
+ * @expectedException Cake\Error\Exception
+ * @return void
+ */
+ public function testConfigErrorOnReconfigure() {
+ Log::config('tests', ['engine' => 'File', 'path' => TMP]);
+ Log::config('tests', ['engine' => 'Apc']);
}
/**
View
9 lib/Cake/Test/TestCase/Model/ModelWriteTest.php
@@ -260,12 +260,8 @@ public function testAllowSimulatedFields() {
* @return void
*/
public function testCacheClearOnSave() {
- $_back = array(
- 'check' => Configure::read('Cache.check'),
- 'disable' => Configure::read('Cache.disable'),
- );
Configure::write('Cache.check', true);
- Configure::write('Cache.disable', false);
+ Cache::enable();
$this->loadFixtures('OverallFavorite');
$OverallFavorite = new OverallFavorite();
@@ -286,9 +282,6 @@ public function testCacheClearOnSave() {
$this->assertFalse(file_exists(CACHE . 'views/some_dir_overallfavorites_index.php'));
$this->assertFalse(file_exists(CACHE . 'views/some_dir_overall_favorites_index.php'));
-
- Configure::write('Cache.check', $_back['check']);
- Configure::write('Cache.disable', $_back['disable']);
}
/**
View
6 lib/Cake/Test/TestCase/Network/Email/EmailTest.php
@@ -1128,7 +1128,7 @@ public function testSendWithLog() {
)
);
- Log::config('email', ['engine' => $log]);
+ Log::config('email', $log);
$this->CakeEmail->transport('Debug');
$this->CakeEmail->to('me@cakephp.org');
@@ -1158,9 +1158,7 @@ public function testSendWithLogAndScope() {
)
);
- Log::config('email', [
- 'engine' => $log,
- ]);
+ Log::config('email', $log);
$this->CakeEmail->transport('Debug');
$this->CakeEmail->to('me@cakephp.org');
View
8 lib/Cake/Test/TestCase/Routing/DispatcherTest.php
@@ -18,6 +18,7 @@
*/
namespace Cake\Test\TestCase\Routing;
+use Cake\Cache\Cache;
use Cake\Controller\Controller;
use Cake\Core\App;
use Cake\Core\Configure;
@@ -266,17 +267,13 @@ public function setUp() {
parent::setUp();
$_GET = array();
- $this->_app = Configure::read('App');
Configure::write('App.base', false);
Configure::write('App.baseUrl', false);
Configure::write('App.dir', 'app');
Configure::write('App.webroot', 'webroot');
Configure::write('App.namespace', 'TestApp');
- $this->_cache = Configure::read('Cache');
- Configure::write('Cache.disable', true);
-
- $this->_debug = Configure::read('debug');
+ Cache::disable();
App::build();
App::objects('Plugin', null, false);
@@ -944,6 +941,7 @@ public static function cacheActionProvider() {
* @return void
*/
public function testFullPageCachingDispatch($url) {
+ Cache::enable();
Configure::write('Cache.disable', false);
Configure::write('Cache.check', true);
Configure::write('debug', 2);
View
3  lib/Cake/Test/TestCase/View/Helper/CacheHelperTest.php
@@ -14,6 +14,7 @@
*/
namespace Cake\Test\TestCase\View\Helper;
+use Cake\Cache\Cache;
use Cake\Controller\Controller;
use Cake\Core\App;
use Cake\Core\Configure;
@@ -85,7 +86,7 @@ public function setUp() {
$View = new View($this->Controller);
$this->Cache = new CacheHelper($View);
Configure::write('Cache.check', true);
- Configure::write('Cache.disable', false);
+ Cache::enable();
App::build([
'View' => [CAKE . 'Test/TestApp/View/']
], App::RESET);
View
3  lib/Cake/basics.php
@@ -19,6 +19,7 @@
* @since CakePHP(tm) v 0.2.9
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
+use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\I18n\I18n;
use Cake\Log\Log;
@@ -417,7 +418,7 @@ function env($key) {
* @deprecated Will be removed in 3.0. Please use Cache::write() instead.
*/
function cache($path, $data = null, $expires = '+1 day', $target = 'cache') {
- if (Configure::read('Cache.disable')) {
+ if (!Cache::enabled()) {
return null;
}
$now = time();
Something went wrong with that request. Please try again.