Skip to content
Permalink
Browse files

Throwing more specific exception in the Cache library

  • Loading branch information...
lorenzo committed Aug 30, 2014
1 parent aa4f1b5 commit dc55818b02f796d7b3988c60ccbe67902f81c0e3
Showing with 27 additions and 28 deletions.
  1. +11 −8 src/Cache/Cache.php
  2. +7 −7 src/Cache/CacheRegistry.php
  3. +1 −1 src/Core/ObjectRegistry.php
  4. +8 −12 tests/TestCase/Cache/CacheTest.php
@@ -15,8 +15,9 @@
namespace Cake\Cache;
use Cake\Cache\Engine\NullEngine;
use Cake\Core\Exception\Exception;
use Cake\Core\StaticConfigTrait;
use InvalidArgumentException;
use RuntimeException;
/**
* Cache provides a consistent interface to Caching in your application. It allows you
@@ -61,7 +62,6 @@
* @param string $name Name of the configuration
* @param array $config Optional associative array of settings passed to the engine
* @return array [engine, settings] on success, false on failure
* @throws \Cake\Core\Exception\Exception
*/
class Cache {
@@ -100,14 +100,17 @@ class Cache {
*
* @param string $name Name of the config array that needs an engine instance built
* @return void
* @throws \Cake\Core\Exception\Exception When a cache engine cannot be created.
* @throws \InvalidArgumentException When a cache engine cannot be created.
*/
protected static function _buildEngine($name) {
if (empty(static::$_registry)) {
static::$_registry = new CacheRegistry();
}
if (empty(static::$_config[$name]['className'])) {
throw new Exception(sprintf('The "%s" cache configuration does not exist.', $name));
throw new InvalidArgumentException(
sprintf('The "%s" cache configuration does not exist.', $name)
);
}
$config = static::$_config[$name];
@@ -213,14 +216,14 @@ public static function write($key, $value, $config = 'default') {
* @param array $data An array of data to be stored in the cache
* @param string $config Optional string configuration name to write to. Defaults to 'default'
* @return array of bools for each key provided, indicating true for success or false for fail
* @throws Cake\Core\Exception\Exception
* @throws RuntimeException
*/
public static function writeMany($data, $config = 'default') {
$engine = static::engine($config);
$return = $engine->writeMany($data);
foreach ($return as $key => $success) {
if ($success === false && !empty($data[$key])) {
throw new Exception(sprintf(
throw new RuntimeException(sprintf(
'%s cache was unable to write \'%s\' to %s cache',
$config,
$key,
@@ -394,7 +397,7 @@ public static function clearGroup($group, $config = 'default') {
*
* @param string $group group name or null to retrieve all group mappings
* @return array map of group and all configuration that has the same group
* @throws \Cake\Core\Exception\Exception
* @throws \InvalidArgumentException
*/
public static function groupConfigs($group = null) {
if ($group === null) {
@@ -405,7 +408,7 @@ public static function groupConfigs($group = null) {
return [$group => self::$_groups[$group]];
}
throw new Exception(sprintf('Invalid cache group %s', $group));
throw new InvalidArgumentException(sprintf('Invalid cache group %s', $group));
}
/**
@@ -15,8 +15,9 @@
namespace Cake\Cache;
use Cake\Core\App;
use Cake\Core\Exception\Exception;
use Cake\Core\ObjectRegistry;
use RuntimeException;
use BadMethodCallException;
/**
* An object registry for cache engines.
@@ -48,10 +49,10 @@ protected function _resolveClassName($class) {
* @param string $class The classname that is missing.
* @param string $plugin The plugin the cache is missing in.
* @return void
* @throws \Cake\Core\Exception\Exception
* @throws \BadMethodCallException
*/
protected function _throwMissingClassError($class, $plugin) {
throw new Exception(sprintf('Cache engine %s is not available.', $class));
throw new BadMethodCallException(sprintf('Cache engine %s is not available.', $class));
}
/**
@@ -63,8 +64,7 @@ protected function _throwMissingClassError($class, $plugin) {
* @param string $alias The alias of the object.
* @param array $config An array of settings to use for the cache engine.
* @return CacheEngine The constructed CacheEngine class.
* @throws \Cake\Core\Exception\Exception when an object doesn't implement
* the correct interface.
* @throws \RuntimeException when an object doesn't implement the correct interface.
*/
protected function _create($class, $alias, $config) {
if (is_object($class)) {
@@ -77,13 +77,13 @@ protected function _create($class, $alias, $config) {
}
if (!($instance instanceof CacheEngine)) {
throw new Exception(
throw new RuntimeException(
'Cache engines must use Cake\Cache\CacheEngine as a base class.'
);
}
if (!$instance->init($config)) {
throw new Exception(
throw new RuntimeException(
sprintf('Cache engine %s is not properly configured.', get_class($instance))
);
}
@@ -95,7 +95,7 @@ public function load($objectName, $config = []) {
* @param string $class The class that is missing.
* @param string $plugin The plugin $class is missing from.
* @return void
* @throws \Cake\Core\Exception\Exception
* @throws \Exception
*/
abstract protected function _throwMissingClassError($class, $plugin);
@@ -45,6 +45,7 @@ public function setUp() {
public function tearDown() {
parent::tearDown();
Cache::drop('tests');
Cache::drop('test_trigger');
}
/**
@@ -115,7 +116,7 @@ public function testConfigWithLibAndPluginEngines() {
/**
* Test write from a config that is undefined.
*
* @expectedException \Cake\Core\Exception\Exception
* @expectedException InvalidArgumentException
* @return void
*/
public function testWriteNonExistingConfig() {
@@ -125,7 +126,7 @@ public function testWriteNonExistingConfig() {
/**
* Test write from a config that is undefined.
*
* @expectedException \Cake\Core\Exception\Exception
* @expectedException InvalidArgumentException
* @return void
*/
public function testIncrementNonExistingConfig() {
@@ -135,7 +136,7 @@ public function testIncrementNonExistingConfig() {
/**
* Test write from a config that is undefined.
*
* @expectedException \Cake\Core\Exception\Exception
* @expectedException InvalidArgumentException
* @return void
*/
public function testDecrementNonExistingConfig() {
@@ -181,7 +182,7 @@ public function testConfigVariants($config) {
/**
* testConfigInvalidEngine method
*
* @expectedException \Cake\Core\Exception\Exception
* @expectedException BadMethodCallException
* @return void
*/
public function testConfigInvalidEngine() {
@@ -285,7 +286,7 @@ public function testGroupConfigs() {
/**
* testGroupConfigsThrowsException method
* @expectedException \Cake\Core\Exception\Exception
* @expectedException InvalidArgumentException
*/
public function testGroupConfigsThrowsException() {
Cache::groupConfigs('bogus');
@@ -416,6 +417,7 @@ public function testDeleteMany() {
/**
* Test that failed writes cause errors to be triggered.
*
* @expectedException PHPUnit_Framework_Error
* @return void
*/
public function testWriteTriggerError() {
@@ -425,13 +427,7 @@ public function testWriteTriggerError() {
'prefix' => ''
]);
try {
Cache::write('fail', 'value', 'test_trigger');
$this->fail('No exception thrown');
} catch (\PHPUnit_Framework_Error $e) {
$this->assertTrue(true);
}
Cache::drop('test_trigger');
Cache::write('fail', 'value', 'test_trigger');
}
/**

0 comments on commit dc55818

Please sign in to comment.
You can’t perform that action at this time.