Browse Source

Add tests.

ADmad 7 years ago
parent
commit
b464b44def
2 changed files with 38 additions and 0 deletions
  1. 17 0
      tests/TestCase/Form/FormTest.php
  2. 21 0
      tests/TestCase/View/Form/FormContextTest.php

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

@@ -234,6 +234,22 @@ class FormTest extends TestCase
     }
 
     /**
+     * Test setting and getting form data.
+     *
+     * @return void
+     */
+    public function testDataSetGet()
+    {
+        $form = new Form();
+        $expected = ['title' => 'title', 'is_published' => true];
+        $form->setData(['title' => 'title', 'is_published' => true]);
+
+        $this->assertSame($expected, $form->getData());
+        $this->assertEquals('title', $form->getData('title'));
+        $this->assertNull($form->getData('non-existent'));
+    }
+
+    /**
      * test __debugInfo
      *
      * @return void
@@ -245,5 +261,6 @@ class FormTest extends TestCase
         $this->assertArrayHasKey('_schema', $result);
         $this->assertArrayHasKey('_errors', $result);
         $this->assertArrayHasKey('_validator', $result);
+        $this->assertArrayHasKey('_data', $result);
     }
 }

+ 21 - 0
tests/TestCase/View/Form/FormContextTest.php

@@ -94,6 +94,27 @@ class FormContextTest extends TestCase
     }
 
     /**
+     * Test reading values from form data.
+     */
+    public function testValPresentInForm()
+    {
+        $form = new Form();
+        $form->setData(['title' => 'set title']);
+
+        $context = new FormContext($this->request, ['entity' => $form]);
+
+        $this->assertEquals('set title', $context->val('title'));
+        $this->assertNull($context->val('Articles.body'));
+
+        $this->request = $this->request->withParsedBody([
+            'title' => 'New title',
+        ]);
+        $context = new FormContext($this->request, ['entity' => $form]);
+
+        $this->assertEquals('New title', $context->val('title'));
+    }
+
+    /**
      * Test getting values when the request and defaults are missing.
      *
      * @return void