Skip to content
Permalink
Browse files

Add missing errors and type checks in InputRegistry

Providing string classnames should work. If an object is provided it
should be checked for the correct interface.
  • Loading branch information...
markstory committed Feb 9, 2014
1 parent 4677cb9 commit d8c075fd7199c2da693e75574b97e922e16c5526
Showing with 37 additions and 2 deletions.
  1. +12 −2 src/View/Widget/InputRegistry.php
  2. +25 −0 tests/TestCase/View/Widget/InputRegistryTest.php
@@ -136,15 +136,25 @@ public function clear() {
* implement WidgetInterface.
*/
protected function _resolveWidget($widget) {
if (is_object($widget)) {
$type = gettype($widget);
if ($type === 'object' && $widget instanceof WidgetInterface) {
return $widget;
}
if ($type === 'object') {
throw new \RuntimeException(
'Input objects must implement Cake\View\Widget\WidgetInterface.'
);
}
if ($type === 'string') {
$widget = [$widget];
}
$class = array_shift($widget);
$className = App::classname($class, 'View/Input');
if ($className === false || !class_exists($className)) {
throw new \RuntimeException(sprintf('Unable to locate widget class "%s"', $class));
}
if (count($widget)) {
if ($type === 'array' && count($widget)) {
$reflection = new ReflectionClass($className);
$arguments = [$this->_templates];
foreach ($widget as $requirement) {
@@ -58,6 +58,31 @@ public function testAdd() {
'text' => ['Cake\View\Widget\Basic'],
]);
$this->assertNull($result);
$result = $inputs->get('text');
$this->assertInstanceOf('Cake\View\Widget\WidgetInterface', $result);
$inputs = new InputRegistry($this->templates);
$result = $inputs->add([
'hidden' => 'Cake\View\Widget\Basic',
]);
$this->assertNull($result);
$result = $inputs->get('hidden');
$this->assertInstanceOf('Cake\View\Widget\WidgetInterface', $result);
}
/**
* Test adding an instance of an invalid type.
*
* @expectedException \RuntimeException
* @expectedExceptionMessage Input objects must implement Cake\View\Widget\WidgetInterface
* @return void
*/
public function testAddInvalidType() {
$inputs = new InputRegistry($this->templates);
$inputs->add([
'text' => new \StdClass()
]);
$inputs->get('text');
}
/**

0 comments on commit d8c075f

Please sign in to comment.
You can’t perform that action at this time.