Browse Source

Split setter in Form::widgetLocator

Raúl Arellano 8 years ago
parent
commit
80417172a5
2 changed files with 42 additions and 16 deletions
  1. 25 14
      src/View/Helper/FormHelper.php
  2. 17 2
      tests/TestCase/View/Helper/FormHelperTest.php

+ 25 - 14
src/View/Helper/FormHelper.php

@@ -282,7 +282,10 @@ class FormHelper extends Helper
 
         parent::__construct($View, $config);
 
-        $this->widgetLocator($locator, $widgets);
+        if (!$locator) {
+            $locator = new WidgetLocator($this->templater(), $this->_View, $widgets);
+        }
+        $this->setWidgetLocator($locator);
         $this->_idPrefix = $this->getConfig('idPrefix');
     }
 
@@ -298,29 +301,37 @@ class FormHelper extends Helper
     {
         deprecationWarning('widgetRegistry is deprecated, use widgetLocator instead.');
 
-        return $this->widgetLocator($instance, $widgets);
+        if ($instance) {
+            $instance->add($widgets);
+            $this->setWidgetLocator($instance);
+        }
+
+        return $this->getWidgetLocator();
     }
 
     /**
-     * Set the widget registry the helper will use.
+     * Get the widget locator currently used by the helper.
      *
-     * @param \Cake\View\Widget\WidgetLocator|null $instance The locator instance to set.
-     * @param array $widgets An array of widgets
-     * @return \Cake\View\Widget\WidgetLocator
+     * @return \Cake\View\Widget\WidgetLocator Current locator instance
      * @since 3.6.0
      */
-    public function widgetLocator(WidgetLocator $instance = null, $widgets = [])
+    public function getWidgetLocator()
     {
-        if ($instance === null) {
-            if ($this->_locator === null) {
-                $this->_locator = new WidgetLocator($this->templater(), $this->_View, $widgets);
-            }
+        return $this->_locator;
+    }
 
-            return $this->_locator;
-        }
+    /**
+     * Set the widget locator the helper will use.
+     *
+     * @param \Cake\View\Widget\WidgetLocator $instance The locator instance to set.
+     * @return $this
+     * @since 3.6.0
+     */
+    public function setWidgetLocator(WidgetLocator $instance)
+    {
         $this->_locator = $instance;
 
-        return $this->_locator;
+        return $this;
     }
 
     /**

+ 17 - 2
tests/TestCase/View/Helper/FormHelperTest.php

@@ -26,6 +26,7 @@ use Cake\TestSuite\TestCase;
 use Cake\Utility\Security;
 use Cake\View\Helper\FormHelper;
 use Cake\View\View;
+use Cake\View\Widget\WidgetLocator;
 
 /**
  * Test stub.
@@ -228,7 +229,7 @@ class FormHelperTest extends TestCase
             ]
         ];
         $helper = new FormHelper($this->View, $config);
-        $locator = $helper->widgetLocator();
+        $locator = $helper->getWidgetLocator();
         $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
     }
 
@@ -241,11 +242,25 @@ class FormHelperTest extends TestCase
     public function testConstructWithWidgetsConfig()
     {
         $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
-        $locator = $helper->widgetLocator();
+        $locator = $helper->getWidgetLocator();
         $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
     }
 
     /**
+     * Test setting the widget locator
+     *
+     * @return void
+     */
+    public function testSetAndGetWidgetLocator()
+    {
+        $helper = new FormHelper($this->View);
+        $locator = new WidgetLocator($helper->templater(), $this->View);
+        $helper->setWidgetLocator($locator);
+
+        $this->assertSame($locator, $helper->getWidgetLocator());
+    }
+
+    /**
      * Test registering a new widget class and rendering it.
      *
      * @return void