From c19c1a782895249dc0c33a59e5a39c0f4373ded5 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 30 Aug 2009 19:33:21 -0400 Subject: [PATCH] Fixing issues with cakeError call. Adding tests for cakeError being called with non-existant Behaviors. --- cake/libs/model/model_behavior.php | 6 ++--- .../cases/libs/model/model_behavior.test.php | 22 +++++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/cake/libs/model/model_behavior.php b/cake/libs/model/model_behavior.php index 24171922e..060905b4f 100644 --- a/cake/libs/model/model_behavior.php +++ b/cake/libs/model/model_behavior.php @@ -300,10 +300,10 @@ class BehaviorCollection extends Object { ))); return false; } - if (!class_exists($behavior . 'Behavior')) { + if (!class_exists($class)) { $this->cakeError('missingBehaviorClass', array(array( - 'behavior' => $behavior, - 'file' => Inflector::underscore($behavior) . '.php', + 'behavior' => $class, + 'file' => Inflector::underscore($class) . '.php', 'code' => 500, 'base' => '/' ))); diff --git a/cake/tests/cases/libs/model/model_behavior.test.php b/cake/tests/cases/libs/model/model_behavior.test.php index d27ec178c..58d23f0ba 100644 --- a/cake/tests/cases/libs/model/model_behavior.test.php +++ b/cake/tests/cases/libs/model/model_behavior.test.php @@ -22,6 +22,8 @@ App::import('Model', 'AppModel'); require_once dirname(__FILE__) . DS . 'models.php'; +Mock::generatePartial('BehaviorCollection', 'MockModelBehaviorCollection', array('cakeError', '_stop')); + /** * TestBehavior class * @@ -432,7 +434,7 @@ class BehaviorTest extends CakeTestCase { * @access public * @return void */ - function tearDown() { + function endTest() { ClassRegistry::flush(); } @@ -449,7 +451,8 @@ class BehaviorTest extends CakeTestCase { $Apple->Behaviors->attach('Test', array('key' => 'value')); $this->assertIdentical($Apple->Behaviors->attached(), array('Test')); $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->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')); $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')); $expected = array( 'beforeFind' => 'off', 'afterFind' => 'off', 'key' => 'new value', @@ -503,6 +504,19 @@ class BehaviorTest extends CakeTestCase { $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 *