diff --git a/Cake/ORM/Behavior.php b/Cake/ORM/Behavior.php index 333d4dc47f5..71fc2a19df3 100644 --- a/Cake/ORM/Behavior.php +++ b/Cake/ORM/Behavior.php @@ -14,6 +14,7 @@ */ namespace Cake\ORM; +use Cake\Error\Exception; use Cake\Event\EventListener; /** @@ -131,6 +132,7 @@ class Behavior implements EventListener { */ public function __construct(Table $table, array $settings = []) { $this->_settings = $settings + $this->_defaultSettings; + $this->verifySettings(); } /** @@ -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. * diff --git a/Cake/Test/TestCase/ORM/BehaviorTest.php b/Cake/Test/TestCase/ORM/BehaviorTest.php index d3efe3eb849..d17001c37cc 100644 --- a/Cake/Test/TestCase/ORM/BehaviorTest.php +++ b/Cake/Test/TestCase/ORM/BehaviorTest.php @@ -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 * @@ -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' + ] + ]); + } }