Skip to content

Commit

Permalink
prevent invalid config
Browse files Browse the repository at this point in the history
if a method is configured to be callable that isn't callable - bark on
construction
  • Loading branch information
AD7six committed Nov 10, 2013
1 parent 3663cc9 commit 8be7e31
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Cake/ORM/Behavior.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\ORM;

use Cake\Error\Exception;
use Cake\Event\EventListener;

/**
Expand Down Expand Up @@ -131,6 +132,7 @@ class Behavior implements EventListener {
*/
public function __construct(Table $table, array $settings = []) {
$this->_settings = $settings + $this->_defaultSettings;
$this->verifySettings();
}

/**
Expand All @@ -142,6 +144,29 @@ public function settings() {
return $this->_settings;
}

/**
* verifySettings
*
* Check that implemented* keys contain values pointing at callable
*
* @return void
* @throws Cake\Error\Exception if settings are invalid
*/
public function verifySettings() {
$keys = ['implementedFinders', 'implementedMethods'];
foreach ($keys as $key) {
if (!isset($this->_settings[$key])) {
continue;
}

foreach ($this->_settings[$key] as $method) {
if (!is_callable([$this, $method])) {
throw new Exception(__d('cake_dev', 'The method %s is not callable on class %s', $method, get_class($this)));
}
}
}
}

/**
* Get the Model callbacks this behavior is interested in.
*
Expand Down
33 changes: 33 additions & 0 deletions Cake/Test/TestCase/ORM/BehaviorTest.php
Expand Up @@ -151,6 +151,23 @@ public function testImplementedMethodsDisabled() {
$this->assertEquals($expected, $behavior->implementedMethods());
}

/**
* testImplementedMethodsInvalid
*
* @expectedException Cake\Error\Exception
* @expectedExceptionMessage The method iDoNotExist is not callable on class Cake\Test\TestCase\ORM\Test2Behavior
*
* @return void
*/
public function testImplementedMethodsInvalid() {
$table = $this->getMock('Cake\ORM\Table');
$behavior = new Test2Behavior($table, [
'implementedMethods' => [
'aliased' => 'iDoNotExist'
]
]);
}

/**
* testImplementedFinders
*
Expand Down Expand Up @@ -197,4 +214,20 @@ public function testImplementedFindersDisabled() {
$this->assertEquals($expected, $behavior->implementedFinders());
}

/**
* testImplementedFinderInvalid
*
* @expectedException Cake\Error\Exception
* @expectedExceptionMessage The method findNotDefined is not callable on class Cake\Test\TestCase\ORM\Test2Behavior
*
* @return void
*/
public function testImplementedFinderInvalid() {
$table = $this->getMock('Cake\ORM\Table');
$behavior = new Test2Behavior($table, [
'implementedFinders' => [
'aliased' => 'findNotDefined'
]
]);
}
}

0 comments on commit 8be7e31

Please sign in to comment.