Browse Source

Throwing more specific exception in the Cache library

Jose Lorenzo Rodriguez 11 years ago
parent
commit
dc55818b02
4 changed files 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

+ 11 - 8
src/Cache/Cache.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 @@ use Cake\Core\StaticConfigTrait;
  * @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 @@ class Cache {
  * @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 @@ class Cache {
  *
  * @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 @@ class Cache {
 			return [$group => self::$_groups[$group]];
 		}
 
-		throw new Exception(sprintf('Invalid cache group %s', $group));
+		throw new InvalidArgumentException(sprintf('Invalid cache group %s', $group));
 	}
 
 /**

+ 7 - 7
src/Cache/CacheRegistry.php

@@ -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 @@ class CacheRegistry extends ObjectRegistry {
  * @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 @@ class CacheRegistry extends ObjectRegistry {
  * @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 @@ class CacheRegistry extends ObjectRegistry {
 		}
 
 		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))
 			);
 		}

+ 1 - 1
src/Core/ObjectRegistry.php

@@ -95,7 +95,7 @@ abstract class ObjectRegistry {
  * @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);
 

+ 8 - 12
tests/TestCase/Cache/CacheTest.php

@@ -45,6 +45,7 @@ class CacheTest extends TestCase {
 	public function tearDown() {
 		parent::tearDown();
 		Cache::drop('tests');
+		Cache::drop('test_trigger');
 	}
 
 /**
@@ -115,7 +116,7 @@ class CacheTest extends TestCase {
 /**
  * Test write from a config that is undefined.
  *
- * @expectedException \Cake\Core\Exception\Exception
+ * @expectedException InvalidArgumentException
  * @return void
  */
 	public function testWriteNonExistingConfig() {
@@ -125,7 +126,7 @@ class CacheTest extends TestCase {
 /**
  * Test write from a config that is undefined.
  *
- * @expectedException \Cake\Core\Exception\Exception
+ * @expectedException InvalidArgumentException
  * @return void
  */
 	public function testIncrementNonExistingConfig() {
@@ -135,7 +136,7 @@ class CacheTest extends TestCase {
 /**
  * Test write from a config that is undefined.
  *
- * @expectedException \Cake\Core\Exception\Exception
+ * @expectedException InvalidArgumentException
  * @return void
  */
 	public function testDecrementNonExistingConfig() {
@@ -181,7 +182,7 @@ class CacheTest extends TestCase {
 /**
  * testConfigInvalidEngine method
  *
- * @expectedException \Cake\Core\Exception\Exception
+ * @expectedException BadMethodCallException
  * @return void
  */
 	public function testConfigInvalidEngine() {
@@ -285,7 +286,7 @@ class CacheTest extends TestCase {
 
 /**
  * testGroupConfigsThrowsException method
- * @expectedException \Cake\Core\Exception\Exception
+ * @expectedException InvalidArgumentException
  */
 	public function testGroupConfigsThrowsException() {
 		Cache::groupConfigs('bogus');
@@ -416,6 +417,7 @@ class CacheTest extends TestCase {
 /**
  * Test that failed writes cause errors to be triggered.
  *
+ * @expectedException PHPUnit_Framework_Error
  * @return void
  */
 	public function testWriteTriggerError() {
@@ -425,13 +427,7 @@ class CacheTest extends TestCase {
 			'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');
 	}
 
 /**