Skip to content

Commit

Permalink
Throwing more specific exception in the Cache library
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Aug 30, 2014
1 parent aa4f1b5 commit dc55818
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
19 changes: 11 additions & 8 deletions src/Cache/Cache.php
Expand Up @@ -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
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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));
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Cache/CacheRegistry.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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)) {
Expand All @@ -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))
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/ObjectRegistry.php
Expand Up @@ -95,7 +95,7 @@ abstract protected function _resolveClassName($class);
* @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);

Expand Down
20 changes: 8 additions & 12 deletions tests/TestCase/Cache/CacheTest.php
Expand Up @@ -45,6 +45,7 @@ public function setUp() {
public function tearDown() {
parent::tearDown();
Cache::drop('tests');
Cache::drop('test_trigger');
}

/**
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -181,7 +182,7 @@ public function testConfigVariants($config) {
/**
* testConfigInvalidEngine method
*
* @expectedException \Cake\Core\Exception\Exception
* @expectedException BadMethodCallException
* @return void
*/
public function testConfigInvalidEngine() {
Expand Down Expand Up @@ -285,7 +286,7 @@ public function testGroupConfigs() {

/**
* testGroupConfigsThrowsException method
* @expectedException \Cake\Core\Exception\Exception
* @expectedException InvalidArgumentException
*/
public function testGroupConfigsThrowsException() {
Cache::groupConfigs('bogus');
Expand Down Expand Up @@ -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() {
Expand All @@ -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');
}

/**
Expand Down

0 comments on commit dc55818

Please sign in to comment.