From b464b44def4cae5085386407b6f861f8faf5c32e Mon Sep 17 00:00:00 2001 From: ADmad Date: Sat, 2 Jun 2018 00:01:17 +0530 Subject: [PATCH] Add tests. --- tests/TestCase/Form/FormTest.php | 17 ++++++++++++++++ tests/TestCase/View/Form/FormContextTest.php | 21 ++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/tests/TestCase/Form/FormTest.php b/tests/TestCase/Form/FormTest.php index be063235f4f..0cd46ee8a8d 100644 --- a/tests/TestCase/Form/FormTest.php +++ b/tests/TestCase/Form/FormTest.php @@ -233,6 +233,22 @@ public function testExecuteValid() $this->assertTrue($form->execute($data)); } + /** + * 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 * @@ -245,5 +261,6 @@ public function testDebugInfo() $this->assertArrayHasKey('_schema', $result); $this->assertArrayHasKey('_errors', $result); $this->assertArrayHasKey('_validator', $result); + $this->assertArrayHasKey('_data', $result); } } diff --git a/tests/TestCase/View/Form/FormContextTest.php b/tests/TestCase/View/Form/FormContextTest.php index 14947d7ed0c..9f7025cf950 100644 --- a/tests/TestCase/View/Form/FormContextTest.php +++ b/tests/TestCase/View/Form/FormContextTest.php @@ -93,6 +93,27 @@ public function testValPresent() $this->assertNull($context->val('Articles.nope')); } + /** + * 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. *