Skip to content

Commit 8be7e31

Browse files
committed
prevent invalid config
if a method is configured to be callable that isn't callable - bark on construction
1 parent 3663cc9 commit 8be7e31

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Cake/ORM/Behavior.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
namespace Cake\ORM;
1616

17+
use Cake\Error\Exception;
1718
use Cake\Event\EventListener;
1819

1920
/**
@@ -131,6 +132,7 @@ class Behavior implements EventListener {
131132
*/
132133
public function __construct(Table $table, array $settings = []) {
133134
$this->_settings = $settings + $this->_defaultSettings;
135+
$this->verifySettings();
134136
}
135137

136138
/**
@@ -142,6 +144,29 @@ public function settings() {
142144
return $this->_settings;
143145
}
144146

147+
/**
148+
* verifySettings
149+
*
150+
* Check that implemented* keys contain values pointing at callable
151+
*
152+
* @return void
153+
* @throws Cake\Error\Exception if settings are invalid
154+
*/
155+
public function verifySettings() {
156+
$keys = ['implementedFinders', 'implementedMethods'];
157+
foreach ($keys as $key) {
158+
if (!isset($this->_settings[$key])) {
159+
continue;
160+
}
161+
162+
foreach ($this->_settings[$key] as $method) {
163+
if (!is_callable([$this, $method])) {
164+
throw new Exception(__d('cake_dev', 'The method %s is not callable on class %s', $method, get_class($this)));
165+
}
166+
}
167+
}
168+
}
169+
145170
/**
146171
* Get the Model callbacks this behavior is interested in.
147172
*

Cake/Test/TestCase/ORM/BehaviorTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,23 @@ public function testImplementedMethodsDisabled() {
151151
$this->assertEquals($expected, $behavior->implementedMethods());
152152
}
153153

154+
/**
155+
* testImplementedMethodsInvalid
156+
*
157+
* @expectedException Cake\Error\Exception
158+
* @expectedExceptionMessage The method iDoNotExist is not callable on class Cake\Test\TestCase\ORM\Test2Behavior
159+
*
160+
* @return void
161+
*/
162+
public function testImplementedMethodsInvalid() {
163+
$table = $this->getMock('Cake\ORM\Table');
164+
$behavior = new Test2Behavior($table, [
165+
'implementedMethods' => [
166+
'aliased' => 'iDoNotExist'
167+
]
168+
]);
169+
}
170+
154171
/**
155172
* testImplementedFinders
156173
*
@@ -197,4 +214,20 @@ public function testImplementedFindersDisabled() {
197214
$this->assertEquals($expected, $behavior->implementedFinders());
198215
}
199216

217+
/**
218+
* testImplementedFinderInvalid
219+
*
220+
* @expectedException Cake\Error\Exception
221+
* @expectedExceptionMessage The method findNotDefined is not callable on class Cake\Test\TestCase\ORM\Test2Behavior
222+
*
223+
* @return void
224+
*/
225+
public function testImplementedFinderInvalid() {
226+
$table = $this->getMock('Cake\ORM\Table');
227+
$behavior = new Test2Behavior($table, [
228+
'implementedFinders' => [
229+
'aliased' => 'findNotDefined'
230+
]
231+
]);
232+
}
200233
}

0 commit comments

Comments
 (0)