Removing PHP4 workarounds in BehaviorCollection.

Making BehaviorCollection throw exceptions instead of use cakeError.
This commit is contained in:
mark_story 2010-07-04 00:15:36 -04:00
parent d048813af8
commit c78e869be6
2 changed files with 13 additions and 20 deletions

View file

@ -270,35 +270,25 @@ class BehaviorCollection extends ObjectCollection {
* @param array $config Behavior configuration parameters * @param array $config Behavior configuration parameters
* @param boolean $enable Whether or not this helper should be enabled by default * @param boolean $enable Whether or not this helper should be enabled by default
* @return boolean True on success, false on failure * @return boolean True on success, false on failure
* @throws MissingBehaviorFileException or MissingBehaviorClassException when a behavior could not be found.
*/ */
public function load($behavior, $config = array(), $enable = true) { public function load($behavior, $config = array(), $enable = true) {
list($plugin, $name) = pluginSplit($behavior); list($plugin, $name) = pluginSplit($behavior);
$class = $name . 'Behavior'; $class = $name . 'Behavior';
if (!App::import('Behavior', $behavior)) { if (!App::import('Behavior', $behavior)) {
$this->cakeError('missingBehaviorFile', array(array( throw new MissingBehaviorFileException(Inflector::underscore($behavior) . '.php');
'behavior' => $behavior,
'file' => Inflector::underscore($behavior) . '.php',
'code' => 500,
'base' => '/'
)));
return false;
} }
if (!class_exists($class)) { if (!class_exists($class)) {
$this->cakeError('missingBehaviorClass', array(array( throw new MissingBehaviorClassException(Inflector::underscore($class));
'behavior' => $class,
'file' => Inflector::underscore($class) . '.php',
'code' => 500,
'base' => '/'
)));
return false;
} }
if (!isset($this->{$name})) { if (!isset($this->{$name})) {
if (ClassRegistry::isKeySet($class)) { if (ClassRegistry::isKeySet($class)) {
$this->{$name} = ClassRegistry::getObject($class); $this->{$name} = ClassRegistry::getObject($class);
} else { } else {
$this->{$name} = new $class; $this->{$name} = new $class();
ClassRegistry::addObject($class, $this->{$name}); ClassRegistry::addObject($class, $this->{$name});
if (!empty($plugin)) { if (!empty($plugin)) {
ClassRegistry::addObject($plugin.'.'.$class, $this->{$name}); ClassRegistry::addObject($plugin.'.'.$class, $this->{$name});
@ -461,3 +451,9 @@ class BehaviorCollection extends ObjectCollection {
} }
} }
/**
* Runtime Exceptions for behaviors
*/
class MissingBehaviorFileException extends RuntimeException { }
class MissingBehaviorClassException extends RuntimeException { }

View file

@ -530,15 +530,12 @@ class BehaviorTest extends CakeTestCase {
/** /**
* test that attaching a non existant Behavior triggers a cake error. * test that attaching a non existant Behavior triggers a cake error.
* *
* @expectedException MissingBehaviorFileException
* @return void * @return void
*/ */
function testInvalidBehaviorCausingCakeError() { function testInvalidBehaviorCausingCakeError() {
$Apple = new Apple(); $Apple = new Apple();
$Apple->Behaviors = $this->getMock('BehaviorCollection', array('cakeError')); $Apple->Behaviors->attach('NoSuchBehavior');
$Apple->Behaviors->expects($this->once())
->method('cakeError')
->with('missingBehaviorFile');
$this->assertFalse($Apple->Behaviors->attach('NoSuchBehavior'));
} }
/** /**