Browse Source

Widgets config loader reworked with tests

Denys Kyselov 11 years ago
parent
commit
3118ee4c5c

+ 1 - 1
src/View/Helper/FormHelper.php

@@ -205,7 +205,7 @@ class FormHelper extends Helper {
 			if (is_string($config['widgets'])) {
 				$config['widgets'] = (array)$config['widgets'];
 			}
-			$widgets += $config['widgets'];
+			$widgets = $config['widgets'] + $widgets;
 			unset($config['widgets']);
 		}
 

+ 8 - 6
src/View/Widget/WidgetRegistry.php

@@ -63,9 +63,14 @@ class WidgetRegistry {
 		$this->_templates = $templates;
 		if (!empty($widgets)) {
 			$this->add($widgets);
+			foreach ($this->_widgets as $key => $widget) {
+				if(is_string($widget) && !class_exists($widget)) {
+					$this->load($widget);
+					unset($this->_widgets[$key]);
+				}
+			}
 		}
 		$this->_widgets['_view'] = $view;
-
 	}
 
 /**
@@ -105,19 +110,16 @@ class WidgetRegistry {
  * @throws \RuntimeException When class does not implement WidgetInterface.
  */
 	public function add(array $widgets) {
-		foreach ($widgets as $key=>$object) {
+		foreach ($widgets as $object) {
 			if (gettype($object) === 'object' &&
 				!($object instanceof WidgetInterface)
 			) {
 				throw new \RuntimeException(
 					'Widget objects must implement Cake\View\Widget\WidgetInterface.'
 				);
-			} elseif (is_string($object)){
-				$this->load($object);
-				unset($widgets[$key]);
 			}
 		}
-		$this->_widgets += $widgets;
+		$this->_widgets = $widgets + $this->_widgets;
 	}
 
 /**

+ 39 - 0
tests/TestCase/View/Helper/FormHelperTest.php

@@ -244,6 +244,45 @@ class FormHelperTest extends TestCase {
 	}
 
 /**
+ * Test that when specifying custom widgets config file and it should be
+ * added to widgets array. WidgetRegistry will load widgets in constructor.
+ *
+ * @return void
+ */
+	public function testConstructWithWidgetsConfig() {
+		$expected = [
+			'button' => ['Cake\View\Widget\ButtonWidget'],
+			'checkbox' => ['Cake\View\Widget\CheckboxWidget'],
+			'file' => ['Cake\View\Widget\FileWidget'],
+			'label' => ['Cake\View\Widget\LabelWidget'],
+			'nestingLabel' => ['Cake\View\Widget\NestingLabelWidget'],
+			'multicheckbox' => ['Cake\View\Widget\MultiCheckboxWidget', 'nestingLabel'],
+			'radio' => ['Cake\View\Widget\RadioWidget', 'nestingLabel'],
+			'select' => ['Cake\View\Widget\SelectBoxWidget'],
+			'textarea' => ['Cake\View\Widget\TextareaWidget'],
+			'datetime' => ['Cake\View\Widget\DateTimeWidget', 'select'],
+			'_default' => ['Cake\View\Widget\BasicWidget'],
+			0 => 'test_widgets',
+		];
+
+		$helper = $this->getMock(
+			'Cake\View\Helper\FormHelper',
+			['widgetRegistry'],
+			[],
+			'',
+			false
+		);
+		$helper->expects($this->once())
+			->method('widgetRegistry')
+			->with(null, $expected);
+
+		$config = [
+			'widgets' => 'test_widgets'
+		];
+		$helper->__construct($this->View, $config);
+	}
+
+/**
  * Test registering a new widget class and rendering it.
  *
  * @return void

+ 12 - 4
tests/TestCase/View/Widget/WidgetRegistryTest.php

@@ -71,8 +71,12 @@ class WidgetRegistryTestCase extends TestCase {
  *
  * @return void
  */
-	public function testLoadInConstructor() {
-		$inputs = new WidgetRegistry($this->templates, $this->view, 'test_widgets');
+	public function testAddWidgetsFromConfigInConstuctor() {
+		$widgets = [
+			'text' => ['Cake\View\Widget\BasicWidget'],
+			'test_widgets',
+		];
+		$inputs = new WidgetRegistry($this->templates, $this->view, $widgets);
 		$this->assertInstanceOf('Cake\View\Widget\LabelWidget', $inputs->get('text'));
 	}
 
@@ -81,9 +85,13 @@ class WidgetRegistryTestCase extends TestCase {
  *
  * @return void
  */
-	public function testLoadPluginInConstuctor() {
+	public function testAddPluginWidgetsFromConfigInConstuctor() {
 		Plugin::load('TestPlugin');
-		$inputs = new WidgetRegistry($this->templates, $this->view, 'TestPlugin.test_widgets');
+		$widgets = [
+			'text' => ['Cake\View\Widget\BasicWidget'],
+			'TestPlugin.test_widgets',
+		];
+		$inputs = new WidgetRegistry($this->templates, $this->view, $widgets);
 		$this->assertInstanceOf('Cake\View\Widget\LabelWidget', $inputs->get('text'));
 	}