Browse Source

add tests for Form::set()

andrii-pukhalevych 5 years ago
parent
commit
e9bc4b886e
2 changed files with 46 additions and 3 deletions
  1. 3 3
      src/Form/Form.php
  2. 43 0
      tests/TestCase/Form/FormTest.php

+ 3 - 3
src/Form/Form.php

@@ -273,9 +273,9 @@ class Form implements EventListenerInterface, EventDispatcherInterface, Validato
     /**
      * Saves a variable or an associative array of variables for use inside form data.
      *
-     * @param string|array $name A string or an array of data.
-     * @param mixed $value Value in case $name is a string (which then works as the key).
-     *   Unused if $name is an associative array, otherwise serves as the values to $name's keys.
+     * @param string|array $name The key to write, can be a dot notation value.
+     * Alternatively can be an array containing key(s) and value(s).
+     * @param mixed $value Value to set for var
      * @return $this
      */
     public function set($name, $value = null)

+ 43 - 0
tests/TestCase/Form/FormTest.php

@@ -196,6 +196,49 @@ class FormTest extends TestCase
     }
 
     /**
+     * Test set() with one param.
+     *
+     * @return void
+     */
+    public function testSetOneParam()
+    {
+        $form = new Form();
+        $data = ['test' => 'val', 'foo' => 'bar'];
+        $form->set($data);
+        $this->assertEquals($data, $form->getData());
+
+        $update = ['test' => 'updated'];
+        $form->set($update);
+        $this->assertSame('updated', $form->getData()['test']);
+    }
+
+    /**
+     * test set() with 2 params
+     *
+     * @return void
+     */
+    public function testSetTwoParam()
+    {
+        $form = new Form();
+        $form->set('testing', 'value');
+        $this->assertEquals(['testing' => 'value'], $form->getData());
+    }
+
+    /**
+     * test chainable set()
+     *
+     * @return void
+     */
+    public function testSetChained()
+    {
+        $form = new Form();
+        $result = $form->set('testing', 'value')
+            ->set('foo', 'bar');
+        $this->assertSame($form, $result);
+        $this->assertEquals(['testing' => 'value', 'foo' => 'bar'], $form->getData());
+    }
+
+    /**
      * Test setting and getting form data.
      *
      * @return void