Making all core classes throw CakeException subclasses, this allows developers to catch framework exceptions with one catch.

Adding package specific exceptions.
Replacing generic exceptions in the codebase with CakeException + package exceptions.
This commit is contained in:
mark_story 2010-12-11 19:01:07 -05:00
parent 6c0efb62e7
commit 44c080d5ad
28 changed files with 146 additions and 86 deletions

View file

@ -116,7 +116,7 @@ class ConsoleInputArgument {
return true; return true;
} }
if (!in_array($value, $this->_choices)) { if (!in_array($value, $this->_choices)) {
throw new InvalidArgumentException(sprintf( throw new ConsoleException(sprintf(
__('"%s" is not a valid value for %s. Please use one of "%s"'), __('"%s" is not a valid value for %s. Please use one of "%s"'),
$value, $this->_name, implode(', ', $this->_choices) $value, $this->_name, implode(', ', $this->_choices)
)); ));

View file

@ -142,7 +142,7 @@ class ConsoleInputOption {
return true; return true;
} }
if (!in_array($value, $this->_choices)) { if (!in_array($value, $this->_choices)) {
throw new InvalidArgumentException(sprintf( throw new ConsoleException(sprintf(
__('"%s" is not a valid value for --%s. Please use one of "%s"'), __('"%s" is not a valid value for --%s. Please use one of "%s"'),
$value, $this->_name, implode(', ', $this->_choices) $value, $this->_name, implode(', ', $this->_choices)
)); ));

View file

@ -457,7 +457,7 @@ class ConsoleOptionParser {
} }
foreach ($this->_args as $i => $arg) { foreach ($this->_args as $i => $arg) {
if ($arg->isRequired() && !isset($args[$i]) && empty($params['help'])) { if ($arg->isRequired() && !isset($args[$i]) && empty($params['help'])) {
throw new RuntimeException( throw new ConsoleException(
__('Missing required arguments. %s is required.', $arg->name()) __('Missing required arguments. %s is required.', $arg->name())
); );
} }
@ -552,7 +552,7 @@ class ConsoleOptionParser {
*/ */
protected function _parseOption($name, $params) { protected function _parseOption($name, $params) {
if (!isset($this->_options[$name])) { if (!isset($this->_options[$name])) {
throw new InvalidArgumentException(__('Unknown option `%s`', $name)); throw new ConsoleException(__('Unknown option `%s`', $name));
} }
$option = $this->_options[$name]; $option = $this->_options[$name];
$isBoolean = $option->isBoolean(); $isBoolean = $option->isBoolean();
@ -586,7 +586,7 @@ class ConsoleOptionParser {
} }
$next = count($args); $next = count($args);
if (!isset($this->_args[$next])) { if (!isset($this->_args[$next])) {
throw new InvalidArgumentException(__('Too many arguments.')); throw new ConsoleException(__('Too many arguments.'));
} }
if ($this->_args[$next]->validChoice($argument)) { if ($this->_args[$next]->validChoice($argument)) {

View file

@ -102,7 +102,7 @@ class ShellDispatcher {
protected function _initEnvironment() { protected function _initEnvironment() {
if (!$this->__bootstrap()) { if (!$this->__bootstrap()) {
$message = "Unable to load CakePHP core.\nMake sure " . DS . 'cake' . DS . 'libs exists in ' . CAKE_CORE_INCLUDE_PATH; $message = "Unable to load CakePHP core.\nMake sure " . DS . 'cake' . DS . 'libs exists in ' . CAKE_CORE_INCLUDE_PATH;
throw new RuntimeException($message); throw new CakeException($message);
} }
if (!isset($this->args[0]) || !isset($this->params['working'])) { if (!isset($this->args[0]) || !isset($this->params['working'])) {
@ -110,7 +110,7 @@ class ShellDispatcher {
"Please make sure that " . DIRECTORY_SEPARATOR . "cake" . DIRECTORY_SEPARATOR . "console is in your system path,\n" . "Please make sure that " . DIRECTORY_SEPARATOR . "cake" . DIRECTORY_SEPARATOR . "console is in your system path,\n" .
"and check the cookbook for the correct usage of this command.\n" . "and check the cookbook for the correct usage of this command.\n" .
"(http://book.cakephp.org/)"; "(http://book.cakephp.org/)";
throw new RuntimeException($message); throw new CakeException($message);
} }
$this->shiftArgs(); $this->shiftArgs();

View file

@ -67,7 +67,7 @@ class Cache {
* @param string $name Name of the configuration * @param string $name Name of the configuration
* @param array $settings Optional associative array of settings passed to the engine * @param array $settings Optional associative array of settings passed to the engine
* @return array(engine, settings) on success, false on failure * @return array(engine, settings) on success, false on failure
* @throws Exception * @throws CacheException
*/ */
public static function config($name = null, $settings = array()) { public static function config($name = null, $settings = array()) {
if (is_array($name)) { if (is_array($name)) {
@ -113,10 +113,10 @@ class Cache {
return false; return false;
} }
$cacheClass = $class . 'Engine'; $cacheClass = $class . 'Engine';
self::$_engines[$name] = new $cacheClass(); if (!is_subclass_of($cacheClass, 'CacheEngine')) {
if (!self::$_engines[$name] instanceof CacheEngine) { throw new CacheException(__('Cache engines must use CacheEngine as a base class.'));
throw new Exception(__('Cache engines must use CacheEngine as a base class.'));
} }
self::$_engines[$name] = new $cacheClass();
if (self::$_engines[$name]->init($config)) { if (self::$_engines[$name]->init($config)) {
if (time() % self::$_engines[$name]->settings['probability'] === 0) { if (time() % self::$_engines[$name]->settings['probability'] === 0) {
self::$_engines[$name]->gc(); self::$_engines[$name]->gc();

View file

@ -249,20 +249,20 @@ class FileEngine extends CacheEngine {
* Not implemented * Not implemented
* *
* @return void * @return void
* @throws BadMethodCallException * @throws CacheException
*/ */
public function decrement($key, $offset = 1) { public function decrement($key, $offset = 1) {
throw new BadMethodCallException(__('Files cannot be atomically decremented.')); throw new CacheException(__('Files cannot be atomically decremented.'));
} }
/** /**
* Not implemented * Not implemented
* *
* @return void * @return void
* @throws BadMethodCallException * @throws CacheException
*/ */
public function increment($key, $offset = 1) { public function increment($key, $offset = 1) {
throw new BadMethodCallException(__('Files cannot be atomically incremented.')); throw new CacheException(__('Files cannot be atomically incremented.'));
} }
/** /**

View file

@ -148,11 +148,11 @@ class MemcacheEngine extends CacheEngine {
* @param integer $offset How much to increment * @param integer $offset How much to increment
* @param integer $duration How long to cache the data, in seconds * @param integer $duration How long to cache the data, in seconds
* @return New incremented value, false otherwise * @return New incremented value, false otherwise
* @throws RuntimeException when you try to increment with compress = true * @throws CacheException when you try to increment with compress = true
*/ */
public function increment($key, $offset = 1) { public function increment($key, $offset = 1) {
if ($this->settings['compress']) { if ($this->settings['compress']) {
throw new RuntimeException( throw new CacheException(
__('Method increment() not implemented for compressed cache in %s', __CLASS__) __('Method increment() not implemented for compressed cache in %s', __CLASS__)
); );
} }
@ -166,11 +166,11 @@ class MemcacheEngine extends CacheEngine {
* @param integer $offset How much to substract * @param integer $offset How much to substract
* @param integer $duration How long to cache the data, in seconds * @param integer $duration How long to cache the data, in seconds
* @return New decremented value, false otherwise * @return New decremented value, false otherwise
* @throws RuntimeException when you try to decrement with compress = true * @throws CacheException when you try to decrement with compress = true
*/ */
public function decrement($key, $offset = 1) { public function decrement($key, $offset = 1) {
if ($this->settings['compress']) { if ($this->settings['compress']) {
throw new RuntimeException( throw new CacheException(
__('Method decrement() not implemented for compressed cache in %s', __CLASS__) __('Method decrement() not implemented for compressed cache in %s', __CLASS__)
); );
} }

View file

@ -97,18 +97,18 @@ class CakeLog {
* @param string $key The keyname for this logger, used to remove the logger later. * @param string $key The keyname for this logger, used to remove the logger later.
* @param array $config Array of configuration information for the logger * @param array $config Array of configuration information for the logger
* @return boolean success of configuration. * @return boolean success of configuration.
* @throws Exception * @throws CakeLogException
*/ */
public static function config($key, $config) { public static function config($key, $config) {
if (empty($config['engine'])) { if (empty($config['engine'])) {
throw new Exception(__('Missing logger classname')); throw new CakeLogException(__('Missing logger classname'));
} }
$loggerName = $config['engine']; $loggerName = $config['engine'];
unset($config['engine']); unset($config['engine']);
$className = self::_getLogger($loggerName); $className = self::_getLogger($loggerName);
$logger = new $className($config); $logger = new $className($config);
if (!$logger instanceof CakeLogInterface) { if (!$logger instanceof CakeLogInterface) {
throw new Exception(sprintf( throw new CakeLogException(sprintf(
__('logger class %s does not implement a write method.'), $loggerName __('logger class %s does not implement a write method.'), $loggerName
)); ));
} }
@ -134,7 +134,7 @@ class CakeLog {
} }
} }
if (!class_exists($loggerName)) { if (!class_exists($loggerName)) {
throw new Exception(__('Could not load class %s', $loggerName)); throw new CakeLogException(__('Could not load class %s', $loggerName));
} }
return $loggerName; return $loggerName;
} }

View file

@ -426,7 +426,7 @@ class CakeRequest implements ArrayAccess {
$type = strtolower(substr($name, 2)); $type = strtolower(substr($name, 2));
return $this->is($type); return $this->is($type);
} }
throw new BadMethodCallException(sprintf('Method %s does not exist', $name)); throw new CakeException(__('Method %s does not exist', $name));
} }
/** /**

View file

@ -448,13 +448,14 @@ class CakeResponse {
* *
* @param integer $code * @param integer $code
* @return integer current status code * @return integer current status code
* @throws CakeException When an unknown status code is reached.
*/ */
public function statusCode($code = null) { public function statusCode($code = null) {
if (is_null($code)) { if (is_null($code)) {
return $this->_status; return $this->_status;
} }
if (!isset($this->_statusCodes[$code])) { if (!isset($this->_statusCodes[$code])) {
throw new OutOfRangeException(__('Unknown status code')); throw new CakeException(__('Unknown status code'));
} }
return $this->_status = $code; return $this->_status = $code;
} }

View file

@ -265,7 +265,7 @@ class CakeSession {
public static function delete($name) { public static function delete($name) {
if (self::check($name)) { if (self::check($name)) {
if (in_array($name, self::$watchKeys)) { if (in_array($name, self::$watchKeys)) {
trigger_error(__('Deleting session key {%s}', $name), E_USER_NOTICE); throw new CakeSessionException(__('Deleting session key {%s}', $name));
} }
self::__overwrite($_SESSION, Set::remove($_SESSION, $name)); self::__overwrite($_SESSION, Set::remove($_SESSION, $name));
return (self::check($name) == false); return (self::check($name) == false);
@ -426,7 +426,6 @@ class CakeSession {
*/ */
public static function ignore($var) { public static function ignore($var) {
if (!in_array($var, self::$watchKeys)) { if (!in_array($var, self::$watchKeys)) {
debug("NOT");
return; return;
} }
foreach (self::$watchKeys as $i => $key) { foreach (self::$watchKeys as $i => $key) {
@ -455,7 +454,7 @@ class CakeSession {
} }
foreach ($write as $key => $val) { foreach ($write as $key => $val) {
if (in_array($key, self::$watchKeys)) { if (in_array($key, self::$watchKeys)) {
trigger_error(__('Writing session key {%s}: %s', $key, var_export($val, true)), E_USER_NOTICE); throw new CakeSessionException(__('Writing session key {%s}: %s', $key, var_export($val, true)));
} }
self::__overwrite($_SESSION, Set::insert($_SESSION, $key, $val)); self::__overwrite($_SESSION, Set::insert($_SESSION, $key, $val));
if (Set::classicExtract($_SESSION, $key) !== $val) { if (Set::classicExtract($_SESSION, $key) !== $val) {
@ -495,7 +494,7 @@ class CakeSession {
* Sessions can be configured with a few shortcut names as well as have any number of ini settings declared. * Sessions can be configured with a few shortcut names as well as have any number of ini settings declared.
* *
* @return void * @return void
* @throws Exception Throws exceptions when ini_set() fails. * @throws CakeSessionException Throws exceptions when ini_set() fails.
*/ */
protected static function _configureSession() { protected static function _configureSession() {
$sessionConfig = Configure::read('Session'); $sessionConfig = Configure::read('Session');
@ -527,7 +526,7 @@ class CakeSession {
if (!empty($sessionConfig['ini']) && is_array($sessionConfig['ini'])) { if (!empty($sessionConfig['ini']) && is_array($sessionConfig['ini'])) {
foreach ($sessionConfig['ini'] as $setting => $value) { foreach ($sessionConfig['ini'] as $setting => $value) {
if (ini_set($setting, $value) === false) { if (ini_set($setting, $value) === false) {
throw new Exception(sprintf( throw new CakeSessionException(sprintf(
__('Unable to configure the session, setting %s failed.'), __('Unable to configure the session, setting %s failed.'),
$setting $setting
)); ));
@ -565,13 +564,13 @@ class CakeSession {
App::import('Core', 'session/' . $class); App::import('Core', 'session/' . $class);
} }
if (!class_exists($class)) { if (!class_exists($class)) {
throw new Exception(__('Could not load %s to handle the session.', $class)); throw new CakeSessionException(__('Could not load %s to handle the session.', $class));
} }
$handler = new $class(); $handler = new $class();
if ($handler instanceof CakeSessionHandlerInterface) { if ($handler instanceof CakeSessionHandlerInterface) {
return $handler; return $handler;
} }
throw new Exception(__('Chosen SessionHandler does not implement CakeSessionHandlerInterface it cannot be used with an engine key.')); throw new CakeSessionException(__('Chosen SessionHandler does not implement CakeSessionHandlerInterface it cannot be used with an engine key.'));
} }
/** /**

View file

@ -51,12 +51,12 @@ class PhpReader implements ConfigReaderInterface {
* @param string $key The identifier to read from. If the key has a . it will be treated * @param string $key The identifier to read from. If the key has a . it will be treated
* as a plugin prefix. * as a plugin prefix.
* @return array Parsed configuration values. * @return array Parsed configuration values.
* @throws RuntimeException when files don't exist or they don't contain `$config`. * @throws ConfigureException when files don't exist or they don't contain `$config`.
* InvalidArgumentException when files contain '..' as this could lead to abusive reads. * Or when files contain '..' as this could lead to abusive reads.
*/ */
public function read($key) { public function read($key) {
if (strpos($key, '..') !== false) { if (strpos($key, '..') !== false) {
throw new InvalidArgumentException(__('Cannot load configuration files with ../ in them.')); throw new ConfigureException(__('Cannot load configuration files with ../ in them.'));
} }
list($plugin, $key) = pluginSplit($key); list($plugin, $key) = pluginSplit($key);
@ -66,11 +66,11 @@ class PhpReader implements ConfigReaderInterface {
$file = $this->_path . $key . '.php'; $file = $this->_path . $key . '.php';
} }
if (!file_exists($file)) { if (!file_exists($file)) {
throw new RuntimeException(__('Could not load configuration file: ') . $file); throw new ConfigureException(__('Could not load configuration file: ') . $file);
} }
include $file; include $file;
if (!isset($config)) { if (!isset($config)) {
throw new RuntimeException( throw new ConfigureException(
sprintf(__('No variable $config found in %s.php'), $file) sprintf(__('No variable $config found in %s.php'), $file)
); );
} }

View file

@ -325,7 +325,7 @@ class Configure {
* @param string $key name of configuration resource to load. * @param string $key name of configuration resource to load.
* @param string $config Name of the configured reader to use to read the resource identfied by $key. * @param string $config Name of the configured reader to use to read the resource identfied by $key.
* @return mixed false if file not found, void if load successful. * @return mixed false if file not found, void if load successful.
* @throws Exception Will throw any exceptions the reader raises. * @throws ConfigureException Will throw any exceptions the reader raises.
*/ */
public static function load($key, $config = 'default') { public static function load($key, $config = 'default') {
if (!isset(self::$_readers[$config])) { if (!isset(self::$_readers[$config])) {

View file

@ -58,7 +58,7 @@ class AclComponent extends Component {
/** /**
* Constructor. Will return an instance of the correct ACL class as defined in `Configure::read('Acl.classname')` * Constructor. Will return an instance of the correct ACL class as defined in `Configure::read('Acl.classname')`
* *
* @throws Exception when Acl.classname could not be loaded. * @throws CakeException when Acl.classname could not be loaded.
*/ */
public function __construct(ComponentCollection $collection, $settings = array()) { public function __construct(ComponentCollection $collection, $settings = array()) {
parent::__construct($collection, $settings); parent::__construct($collection, $settings);
@ -68,7 +68,7 @@ class AclComponent extends Component {
list($plugin, $name) = pluginSplit($name); list($plugin, $name) = pluginSplit($name);
$name .= 'Component'; $name .= 'Component';
} else { } else {
throw new Exception(__('Could not find %s.', $name)); throw new CakeException(__('Could not find %s.', $name));
} }
} }
$this->adapter($name); $this->adapter($name);
@ -84,7 +84,7 @@ class AclComponent extends Component {
* *
* @param mixed $adapter Instance of AclBase or a string name of the class to use. (optional) * @param mixed $adapter Instance of AclBase or a string name of the class to use. (optional)
* @return mixed either null, or instance of AclBase * @return mixed either null, or instance of AclBase
* @throws Exception when the given class is not an AclBase * @throws CakeException when the given class is not an AclBase
*/ */
public function adapter($adapter = null) { public function adapter($adapter = null) {
if ($adapter) { if ($adapter) {
@ -92,7 +92,7 @@ class AclComponent extends Component {
$adapter = new $adapter(); $adapter = new $adapter();
} }
if (!$adapter instanceof AclInterface) { if (!$adapter instanceof AclInterface) {
throw new Exception(__('AclComponent adapters must implement AclInterface')); throw new CakeException(__('AclComponent adapters must implement AclInterface'));
} }
$this->_Instance = $adapter; $this->_Instance = $adapter;
$this->_Instance->initialize($this); $this->_Instance->initialize($this);

View file

@ -388,3 +388,61 @@ class MissingTableException extends CakeException {
class MissingModelException extends CakeException { class MissingModelException extends CakeException {
protected $_messageTemplate = 'Model %s could not be found.'; protected $_messageTemplate = 'Model %s could not be found.';
} }
/**
* Exception class for Cache. This exception will be thrown from Cache when it
* encounters an error.
*
* @package cake.libs
*/
class CacheException extends CakeException { }
/**
* Exception class for Router. This exception will be thrown from Router when it
* encounters an error.
*
* @package cake.libs
*/
class RouterException extends CakeException { }
/**
* Exception class for CakeLog. This exception will be thrown from CakeLog when it
* encounters an error.
*
* @package cake.libs
*/
class CakeLogException extends CakeException { }
/**
* Exception class for CakeSession. This exception will be thrown from CakeSession when it
* encounters an error.
*
* @package cake.libs
*/
class CakeSessionException extends CakeException { }
/**
* Exception class for Configure. This exception will be thrown from Configure when it
* encounters an error.
*
* @package cake.libs
*/
class ConfigureException extends CakeException { }
/**
* Exception class for Xml. This exception will be thrown from Xml when it
* encounters an error.
*
* @package cake.libs
*/
class XmlException extends CakeException { }
/**
* Exception class for Console libraries. This exception will be thrown from Console library
* classes when they encounter an error.
*
* @package cake.libs
*/
class ConsoleException extends CakeException { }

View file

@ -226,7 +226,7 @@ class Router {
* shifted into the passed arguments. As well as supplying patterns for routing parameters. * shifted into the passed arguments. As well as supplying patterns for routing parameters.
* @see routes * @see routes
* @return array Array of routes * @return array Array of routes
* @throws Exception * @throws RouterException
*/ */
public static function connect($route, $defaults = array(), $options = array()) { public static function connect($route, $defaults = array(), $options = array()) {
foreach (self::$_prefixes as $prefix) { foreach (self::$_prefixes as $prefix) {
@ -246,13 +246,12 @@ class Router {
$routeClass = 'CakeRoute'; $routeClass = 'CakeRoute';
if (isset($options['routeClass'])) { if (isset($options['routeClass'])) {
$routeClass = $options['routeClass']; $routeClass = $options['routeClass'];
if (!is_subclass_of($routeClass, 'CakeRoute')) {
throw new RouterException(__('Route classes must extend CakeRoute'));
}
unset($options['routeClass']); unset($options['routeClass']);
} }
$Route = new $routeClass($route, $defaults, $options); self::$routes[] = new $routeClass($route, $defaults, $options);
if (!$Route instanceof CakeRoute) {
throw new Exception(__('Route classes must extend CakeRoute'));
}
self::$routes[] =& $Route;
return self::$routes; return self::$routes;
} }

View file

@ -1136,11 +1136,12 @@ class FormHelper extends AppHelper {
* @param string $method Method name / input type to make. * @param string $method Method name / input type to make.
* @param array $params Parameters for the method call * @param array $params Parameters for the method call
* @return string Formatted input method. * @return string Formatted input method.
* @throws CakeException When there are no params for the method call.
*/ */
public function __call($method, $params) { public function __call($method, $params) {
$options = array(); $options = array();
if (empty($params)) { if (empty($params)) {
throw new Exception(__('Missing field name for FormHelper::%s', $method)); throw new CakeException(__('Missing field name for FormHelper::%s', $method));
} }
if (isset($params[1])) { if (isset($params[1])) {
$options = $params[1]; $options = $params[1];

View file

@ -87,7 +87,7 @@ class PaginatorHelper extends AppHelper {
* *
* @param View $View the view object the helper is attached to. * @param View $View the view object the helper is attached to.
* @param array $settings Array of settings. * @param array $settings Array of settings.
* @return void * @throws CakeException When the AjaxProvider helper does not implement a link method.
*/ */
function __construct(View $View, $settings = array()) { function __construct(View $View, $settings = array()) {
parent::__construct($View, $settings); parent::__construct($View, $settings);
@ -99,7 +99,7 @@ class PaginatorHelper extends AppHelper {
} }
$classname = $ajaxProvider . 'Helper'; $classname = $ajaxProvider . 'Helper';
if (!method_exists($classname, 'link')) { if (!method_exists($classname, 'link')) {
throw new Exception(sprintf( throw new CakeException(sprintf(
__('%s does not implement a link() method, it is incompatible with PaginatorHelper'), $classname __('%s does not implement a link() method, it is incompatible with PaginatorHelper'), $classname
)); ));
} }

View file

@ -385,6 +385,7 @@ class View extends Object {
* @param string $layout Layout to use * @param string $layout Layout to use
* @param string $file Custom filename for view * @param string $file Custom filename for view
* @return string Rendered Element * @return string Rendered Element
* @throws CakeException if there is an error in the view.
*/ */
public function render($action = null, $layout = null, $file = null) { public function render($action = null, $layout = null, $file = null) {
if ($this->hasRendered) { if ($this->hasRendered) {
@ -409,7 +410,7 @@ class View extends Object {
$layout = $this->layout; $layout = $this->layout;
} }
if ($this->output === false) { if ($this->output === false) {
throw new RuntimeException(__("Error in view %s, got no content.", $viewFileName)); throw new CakeException(__("Error in view %s, got no content.", $viewFileName));
} }
if ($layout && $this->autoLayout) { if ($layout && $this->autoLayout) {
$this->output = $this->renderLayout($this->output, $layout); $this->output = $this->renderLayout($this->output, $layout);
@ -428,6 +429,7 @@ class View extends Object {
* *
* @param string $content_for_layout Content to render in a view, wrapped by the surrounding layout. * @param string $content_for_layout Content to render in a view, wrapped by the surrounding layout.
* @return mixed Rendered output, or false on error * @return mixed Rendered output, or false on error
* @throws CakeException if there is an error in the view.
*/ */
public function renderLayout($content_for_layout, $layout = null) { public function renderLayout($content_for_layout, $layout = null) {
$layoutFileName = $this->_getLayoutFileName($layout); $layoutFileName = $this->_getLayoutFileName($layout);
@ -451,7 +453,7 @@ class View extends Object {
$this->output = $this->_render($layoutFileName); $this->output = $this->_render($layoutFileName);
if ($this->output === false) { if ($this->output === false) {
throw new RuntimeException(__("Error in layout %s, got no content.", $layoutFileName)); throw new CakeException(__("Error in layout %s, got no content.", $layoutFileName));
} }
$this->Helpers->trigger('afterLayout', array($layoutFileName)); $this->Helpers->trigger('afterLayout', array($layoutFileName));

View file

@ -73,7 +73,7 @@ class Xml {
* @param mixed $input XML string, a path to a file, an URL or an array * @param mixed $input XML string, a path to a file, an URL or an array
* @param array $options The options to use * @param array $options The options to use
* @return object SimpleXMLElement or DOMDocument * @return object SimpleXMLElement or DOMDocument
* @throws Exception * @throws XmlException
*/ */
public static function build($input, $options = array()) { public static function build($input, $options = array()) {
if (!is_array($options)) { if (!is_array($options)) {
@ -101,9 +101,9 @@ class Xml {
$dom->load($input); $dom->load($input);
return $dom; return $dom;
} elseif (!is_string($input)) { } elseif (!is_string($input)) {
throw new Exception(__('Invalid input.')); throw new XmlException(__('Invalid input.'));
} }
throw new Exception(__('XML cannot be read.')); throw new XmlException(__('XML cannot be read.'));
} }
/** /**
@ -141,14 +141,15 @@ class Xml {
* @param array $input Array with data * @param array $input Array with data
* @param array $options The options to use * @param array $options The options to use
* @return object SimpleXMLElement or DOMDocument * @return object SimpleXMLElement or DOMDocument
* @throws XmlException
*/ */
public static function fromArray($input, $options = array()) { public static function fromArray($input, $options = array()) {
if (!is_array($input) || count($input) !== 1) { if (!is_array($input) || count($input) !== 1) {
throw new Exception(__('Invalid input.')); throw new XmlException(__('Invalid input.'));
} }
$key = key($input); $key = key($input);
if (is_integer($key)) { if (is_integer($key)) {
throw new Exception(__('The key of input must be alphanumeric')); throw new XmlException(__('The key of input must be alphanumeric'));
} }
if (!is_array($options)) { if (!is_array($options)) {
@ -212,7 +213,7 @@ class Xml {
} }
} else { } else {
if ($key[0] === '@') { if ($key[0] === '@') {
throw new Exception(__('Invalid array')); throw new XmlException(__('Invalid array'));
} }
if (array_keys($value) === range(0, count($value) - 1)) { // List if (array_keys($value) === range(0, count($value) - 1)) { // List
foreach ($value as $item) { foreach ($value as $item) {
@ -225,7 +226,7 @@ class Xml {
} }
} }
} else { } else {
throw new Exception(__('Invalid array')); throw new XmlException(__('Invalid array'));
} }
} }
} }
@ -270,13 +271,14 @@ class Xml {
* *
* @param object $obj SimpleXMLElement, DOMDocument or DOMNode instance * @param object $obj SimpleXMLElement, DOMDocument or DOMNode instance
* @return array Array representation of the XML structure. * @return array Array representation of the XML structure.
* @throws XmlException
*/ */
public static function toArray($obj) { public static function toArray($obj) {
if ($obj instanceof DOMNode) { if ($obj instanceof DOMNode) {
$obj = simplexml_import_dom($obj); $obj = simplexml_import_dom($obj);
} }
if (!($obj instanceof SimpleXMLElement)) { if (!($obj instanceof SimpleXMLElement)) {
throw new Exception(__('The input is not instance of SimpleXMLElement, DOMDocument or DOMNode.')); throw new XmlException(__('The input is not instance of SimpleXMLElement, DOMDocument or DOMNode.'));
} }
$result = array(); $result = array();
$namespaces = array_merge(array('' => ''), $obj->getNamespaces(true)); $namespaces = array_merge(array('' => ''), $obj->getNamespaces(true));

View file

@ -230,7 +230,7 @@ class ConsoleOptionParserTest extends CakeTestCase {
/** /**
* test parsing options that do not exist. * test parsing options that do not exist.
* *
* @expectedException InvalidArgumentException * @expectedException ConsoleException
*/ */
function testOptionThatDoesNotExist() { function testOptionThatDoesNotExist() {
$parser = new ConsoleOptionParser('test', false); $parser = new ConsoleOptionParser('test', false);
@ -242,7 +242,7 @@ class ConsoleOptionParserTest extends CakeTestCase {
/** /**
* test that options with choices enforce them. * test that options with choices enforce them.
* *
* @expectedException InvalidArgumentException * @expectedException ConsoleException
* @return void * @return void
*/ */
function testOptionWithChoices() { function testOptionWithChoices() {
@ -297,7 +297,7 @@ class ConsoleOptionParserTest extends CakeTestCase {
/** /**
* test parsing arguments. * test parsing arguments.
* *
* @expectedException InvalidArgumentException * @expectedException ConsoleException
* @return void * @return void
*/ */
function testParseArgumentTooMany() { function testParseArgumentTooMany() {
@ -315,7 +315,7 @@ class ConsoleOptionParserTest extends CakeTestCase {
/** /**
* test that when there are not enough arguments an exception is raised * test that when there are not enough arguments an exception is raised
* *
* @expectedException RuntimeException * @expectedException ConsoleException
* @return void * @return void
*/ */
function testPositionalArgNotEnough() { function testPositionalArgNotEnough() {
@ -329,7 +329,7 @@ class ConsoleOptionParserTest extends CakeTestCase {
/** /**
* test that arguments with choices enforce them. * test that arguments with choices enforce them.
* *
* @expectedException InvalidArgumentException * @expectedException ConsoleException
* @return void * @return void
*/ */
function testPositionalArgWithChoices() { function testPositionalArgWithChoices() {

View file

@ -17,7 +17,7 @@
* @since CakePHP(tm) v 1.2.0.5432 * @since CakePHP(tm) v 1.2.0.5432
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/ */
App::import('Core', 'Log'); App::import('Core', 'CakeLog');
App::import('Core', 'log/FileLog'); App::import('Core', 'log/FileLog');
/** /**
@ -70,7 +70,7 @@ class CakeLogTest extends CakeTestCase {
/** /**
* test all the errors from failed logger imports * test all the errors from failed logger imports
* *
* @expectedException Exception * @expectedException CakeLogException
* @return void * @return void
*/ */
function testImportingLoggerFailure() { function testImportingLoggerFailure() {
@ -80,7 +80,7 @@ class CakeLogTest extends CakeTestCase {
/** /**
* test that loggers have to implement the correct interface. * test that loggers have to implement the correct interface.
* *
* @expectedException Exception * @expectedException CakeLogException
* @return void * @return void
*/ */
function testNotImplementingInterface() { function testNotImplementingInterface() {

View file

@ -17,9 +17,7 @@
* @since CakePHP(tm) v 2.0 * @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/ */
if (!class_exists('dispatcher')) { App::import('Core', 'Dispatcher');
require CAKE . 'dispatcher.php';
}
App::import('Core', 'CakeRequest'); App::import('Core', 'CakeRequest');
class CakeRequestTestCase extends CakeTestCase { class CakeRequestTestCase extends CakeTestCase {
@ -614,7 +612,7 @@ class CakeRequestTestCase extends CakeTestCase {
/** /**
* test __call expcetions * test __call expcetions
* *
* @expectedException Exception * @expectedException CakeException
* @return void * @return void
*/ */
function test__callExceptionOnUnknownMethod() { function test__callExceptionOnUnknownMethod() {

View file

@ -55,7 +55,7 @@ class CakeResponseTestCase extends CakeTestCase {
/** /**
* Tests the statusCode method * Tests the statusCode method
* *
* @expectedException OutOfRangeException * @expectedException CakeException
*/ */
public function testStatusCode() { public function testStatusCode() {
$response = new CakeResponse(); $response = new CakeResponse();

View file

@ -367,7 +367,7 @@ class CakeSessionTest extends CakeTestCase {
/** /**
* testWatchVar method * testWatchVar method
* *
* @expectedException Exception * @expectedException CakeSessionException
* @access public * @access public
* @return void * @return void
*/ */
@ -380,16 +380,16 @@ class CakeSessionTest extends CakeTestCase {
} }
/** /**
* undocumented function * Test that deleting watched vars causes exceptions
* *
* @expectedException Exception * @expectedException CakeSessionException
* @return void * @return void
*/ */
function testWatchVarDelete() { function testWatchVarDelete() {
TestCakeSession::write('Watching', 'I am watching you.');
TestCakeSession::watch('Watching'); TestCakeSession::watch('Watching');
TestCakeSession::delete('Watching'); TestCakeSession::delete('Watching');
$this->assertFalse(TestCakeSession::watch('Invalid.key'));
} }
/** /**

View file

@ -44,7 +44,7 @@ class PhpReaderTest extends CakeTestCase {
/** /**
* Test an exception is thrown by reading files that don't exist. * Test an exception is thrown by reading files that don't exist.
* *
* @expectedException RuntimeException * @expectedException ConfigureException
* @return void * @return void
*/ */
function testReadWithNonExistantFile() { function testReadWithNonExistantFile() {
@ -66,7 +66,7 @@ class PhpReaderTest extends CakeTestCase {
/** /**
* test reading keys with ../ doesn't work * test reading keys with ../ doesn't work
* *
* @expectedException InvalidArgumentException * @expectedException ConfigureException
* @return void * @return void
*/ */
function testReadWithDots() { function testReadWithDots() {

View file

@ -218,7 +218,7 @@ class AclComponentTest extends CakeTestCase {
* test that construtor throws an exception when Acl.classname is a * test that construtor throws an exception when Acl.classname is a
* non-existant class * non-existant class
* *
* @expectedException Exception * @expectedException CakeException
* @return void * @return void
*/ */
function testConstrutorException() { function testConstrutorException() {
@ -243,7 +243,7 @@ class AclComponentTest extends CakeTestCase {
/** /**
* test that adapter() whines when the class is not an AclBase * test that adapter() whines when the class is not an AclBase
* *
* @expectedException Exception * @expectedException CakeException
* @return void * @return void
*/ */
function testAdapterException() { function testAdapterException() {

View file

@ -6700,7 +6700,7 @@ class FormHelperTest extends CakeTestCase {
/** /**
* *
* @expectedException Exception * @expectedException CakeException
* @return void * @return void
*/ */
function testHtml5InputException() { function testHtml5InputException() {