Skip to content

Commit

Permalink
Fixing issues with cakeError call.
Browse files Browse the repository at this point in the history
Adding tests for cakeError being called with non-existant Behaviors.
  • Loading branch information
markstory committed Aug 30, 2009
1 parent 7ca1ef2 commit c19c1a7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
6 changes: 3 additions & 3 deletions cake/libs/model/model_behavior.php
Expand Up @@ -300,10 +300,10 @@ function attach($behavior, $config = array()) {
))); )));
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' => '/'
))); )));
Expand Down
22 changes: 18 additions & 4 deletions cake/tests/cases/libs/model/model_behavior.test.php
Expand Up @@ -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
* *
Expand Down Expand Up @@ -432,7 +434,7 @@ class BehaviorTest extends CakeTestCase {
* @access public * @access public
* @return void * @return void
*/ */
function tearDown() { function endTest() {
ClassRegistry::flush(); ClassRegistry::flush();
} }


Expand All @@ -449,7 +451,8 @@ function testBehaviorBinding() {
$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());
Expand Down Expand Up @@ -480,8 +483,6 @@ function testBehaviorBinding() {
$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',
Expand All @@ -503,6 +504,19 @@ function testBehaviorBinding() {
$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
* *
Expand Down

0 comments on commit c19c1a7

Please sign in to comment.