Fixing issues with cakeError call.

Adding tests for cakeError being called with non-existant Behaviors.
This commit is contained in:
mark_story 2009-08-30 19:33:21 -04:00
parent 7ca1ef22f0
commit c19c1a7828
2 changed files with 21 additions and 7 deletions

View file

@ -300,10 +300,10 @@ class BehaviorCollection extends Object {
))); )));
return false; return false;
} }
if (!class_exists($behavior . 'Behavior')) { if (!class_exists($class)) {
$this->cakeError('missingBehaviorClass', array(array( $this->cakeError('missingBehaviorClass', array(array(
'behavior' => $behavior, 'behavior' => $class,
'file' => Inflector::underscore($behavior) . '.php', 'file' => Inflector::underscore($class) . '.php',
'code' => 500, 'code' => 500,
'base' => '/' 'base' => '/'
))); )));

View file

@ -22,6 +22,8 @@
App::import('Model', 'AppModel'); App::import('Model', 'AppModel');
require_once dirname(__FILE__) . DS . 'models.php'; require_once dirname(__FILE__) . DS . 'models.php';
Mock::generatePartial('BehaviorCollection', 'MockModelBehaviorCollection', array('cakeError', '_stop'));
/** /**
* TestBehavior class * TestBehavior class
* *
@ -432,7 +434,7 @@ class BehaviorTest extends CakeTestCase {
* @access public * @access public
* @return void * @return void
*/ */
function tearDown() { function endTest() {
ClassRegistry::flush(); ClassRegistry::flush();
} }
@ -449,7 +451,8 @@ class BehaviorTest extends CakeTestCase {
$Apple->Behaviors->attach('Test', array('key' => 'value')); $Apple->Behaviors->attach('Test', array('key' => 'value'));
$this->assertIdentical($Apple->Behaviors->attached(), array('Test')); $this->assertIdentical($Apple->Behaviors->attached(), array('Test'));
$this->assertEqual(strtolower(get_class($Apple->Behaviors->Test)), 'testbehavior'); $this->assertEqual(strtolower(get_class($Apple->Behaviors->Test)), 'testbehavior');
$this->assertEqual($Apple->Behaviors->Test->settings['Apple'], array('beforeFind' => 'on', 'afterFind' => 'off', 'key' => 'value')); $expected = array('beforeFind' => 'on', 'afterFind' => 'off', 'key' => 'value');
$this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
$this->assertEqual(array_keys($Apple->Behaviors->Test->settings), array('Apple')); $this->assertEqual(array_keys($Apple->Behaviors->Test->settings), array('Apple'));
$this->assertIdentical($Apple->Sample->Behaviors->attached(), array()); $this->assertIdentical($Apple->Sample->Behaviors->attached(), array());
@ -480,8 +483,6 @@ class BehaviorTest extends CakeTestCase {
$Apple->Parent->Behaviors->attach('Test', array('key' => 'value', 'key2' => 'value', 'key3' => 'value', 'beforeFind' => 'off')); $Apple->Parent->Behaviors->attach('Test', array('key' => 'value', 'key2' => 'value', 'key3' => 'value', 'beforeFind' => 'off'));
$this->assertNotEqual($Apple->Parent->Behaviors->Test->settings['Parent'], $Apple->Sample->Behaviors->Test->settings['Sample']); $this->assertNotEqual($Apple->Parent->Behaviors->Test->settings['Parent'], $Apple->Sample->Behaviors->Test->settings['Sample']);
$this->assertFalse($Apple->Behaviors->attach('NoSuchBehavior'));
$Apple->Behaviors->attach('Plugin.Test', array('key' => 'new value')); $Apple->Behaviors->attach('Plugin.Test', array('key' => 'new value'));
$expected = array( $expected = array(
'beforeFind' => 'off', 'afterFind' => 'off', 'key' => 'new value', 'beforeFind' => 'off', 'afterFind' => 'off', 'key' => 'new value',
@ -503,6 +504,19 @@ class BehaviorTest extends CakeTestCase {
$this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected); $this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
} }
/**
* test that attaching a non existant Behavior triggers a cake error.
*
* @return void
**/
function testInvalidBehaviorCausingCakeError() {
$Apple =& new Apple();
$Apple->Behaviors =& new MockModelBehaviorCollection();
$Apple->Behaviors->expectOnce('cakeError');
$Apple->Behaviors->expectAt(0, 'cakeError', array('missingBehaviorFile', '*'));
$this->assertFalse($Apple->Behaviors->attach('NoSuchBehavior'));
}
/** /**
* testBehaviorToggling method * testBehaviorToggling method
* *