From dbba7994b58f7e30c8c644d0d4028b34e9c988df Mon Sep 17 00:00:00 2001 From: Sandro Rodrigues Date: Sat, 9 Dec 2017 21:44:49 +0000 Subject: [PATCH 1/5] Improved configs --- phpcs.xml | 4 +--- phpunit.xml | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/phpcs.xml b/phpcs.xml index 6e7a5d5..d6130bc 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -4,14 +4,12 @@ src + tests - - - diff --git a/phpunit.xml b/phpunit.xml index b1658ae..3d4a756 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertWarningsToExceptions="true" processIsolation="false"> - + ./tests/ From 4e4b7b3ff7143aa10d68e4bc7c6187ab2bc1ab10 Mon Sep 17 00:00:00 2001 From: Sandro Rodrigues Date: Sat, 9 Dec 2017 21:45:16 +0000 Subject: [PATCH 2/5] Improved ErrorList unit tests --- tests/unit/Errors/ErrorListTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/unit/Errors/ErrorListTest.php b/tests/unit/Errors/ErrorListTest.php index e65ac7d..48ea161 100644 --- a/tests/unit/Errors/ErrorListTest.php +++ b/tests/unit/Errors/ErrorListTest.php @@ -11,6 +11,12 @@ public function testToString() { $error = new ErrorList(array(1, 2, 3)); $expected = '
  • 1
  • 2
  • 3
'; - $this->assertEquals((string) $error, $expected); + $this->assertEquals($expected, (string) $error); + } + + public function testToStringEmptyErrorList() + { + $error = new ErrorList(); + $this->assertEquals('', (string) $error); } } From 4a032389041b39be0c6d3106f671fca8280345f9 Mon Sep 17 00:00:00 2001 From: Sandro Rodrigues Date: Sat, 9 Dec 2017 21:45:39 +0000 Subject: [PATCH 3/5] Added Widget unit tests. --- tests/unit/Widgets/WidgetTest.php | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/unit/Widgets/WidgetTest.php diff --git a/tests/unit/Widgets/WidgetTest.php b/tests/unit/Widgets/WidgetTest.php new file mode 100644 index 0000000..cafc637 --- /dev/null +++ b/tests/unit/Widgets/WidgetTest.php @@ -0,0 +1,38 @@ +widget = $this->getMockForAbstractClass(Widget::class); + } + + public function testGetOptions() + { + $options = $this->widget->getOptions("name", "value"); + $this->assertEquals(array(), $options); + } + + public function testValueFromData() + { + $result = $this->widget->valueFromData(array("name" => "value"), array(), "name"); + $this->assertEquals("value", $result); + } + + public function testValueFromDataInexistent() + { + $result = $this->widget->valueFromData(array(), array(), "name"); + $this->assertNull($result); + } + + public function testBuildAutoId() + { + $this->assertEquals("id_name", $this->widget->buildAutoId("name")); + $this->assertEquals("id_name_1", $this->widget->buildAutoId("name", 1)); + } +} From 18ae45ccb892260e304d37fbc713dc144453910a Mon Sep 17 00:00:00 2001 From: Sandro Rodrigues Date: Sat, 9 Dec 2017 22:09:24 +0000 Subject: [PATCH 4/5] Improved BoundField unittest --- src/Fields/BoundField.php | 2 -- tests/unit/Fields/BoundFieldTest.php | 42 +++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/Fields/BoundField.php b/src/Fields/BoundField.php index 887c59d..7ccddb2 100644 --- a/src/Fields/BoundField.php +++ b/src/Fields/BoundField.php @@ -60,8 +60,6 @@ public function __get($name) } return $options_cache; } - - return null; } private function getOptions(array $attrs = array()) diff --git a/tests/unit/Fields/BoundFieldTest.php b/tests/unit/Fields/BoundFieldTest.php index c0135b6..17b869b 100644 --- a/tests/unit/Fields/BoundFieldTest.php +++ b/tests/unit/Fields/BoundFieldTest.php @@ -56,16 +56,42 @@ public function testGetErrors() { $form = $this->getMockForAbstractClass(Form::class); $bound = new BoundField($form, $this->simple_field, "name"); + $this->assertInstanceOf(ErrorList::class, $bound->errors); } - public function testHasErrors() + public function testGetHasErrors() { $form = $this->getMockForAbstractClass(Form::class); $bound = new BoundField($form, $this->simple_field, "name"); + $this->assertFalse($bound->has_errors); } + public function testGetIsRequired() + { + $form = $this->getMockForAbstractClass(Form::class); + $bound = new BoundField($form, $this->simple_field, "name"); + + $this->assertFalse($bound->is_required); + } + + public function testGetValue() + { + $form = $this->getMockForAbstractClass(Form::class); + $bound = new BoundField($form, $this->simple_field, "name"); + + $this->assertNull($bound->value); + } + + public function testGetNotDefinedAttribute() + { + $form = $this->getMockForAbstractClass(Form::class); + $bound = new BoundField($form, $this->simple_field, "name"); + + $this->assertNull($bound->undefined); + } + public function testSimpleGet() { $form = $this->getMockForAbstractClass(Form::class); @@ -123,12 +149,26 @@ public function testToStringWithErrors() $this->assertXmlStringEqualsXmlString($expected, (string) $bound); } + public function testToStringWithFieldRequired() + { + $form = $this->getMockForAbstractClass(Form::class); + $field = new CharField(['disabled' => true]); + + $bound = new BoundField($form, $field, "name"); + + $expected = ''; + $this->assertXmlStringEqualsXmlString($expected, (string) $bound); + } + public function testLabelTag() { $field = new CharField(array("label" => "Label")); $bound = new BoundField($this->simple_form, $field, "name"); + $expected = ''; + $this->assertXmlStringEqualsXmlString($bound->labelTag(), $expected); + $this->assertXmlStringEqualsXmlString($bound->label_tag, $expected); } public function testLabelTagWithPrefix() From 10825a239e14048ba379ad204b37c02e890d7f8e Mon Sep 17 00:00:00 2001 From: Sandro Rodrigues Date: Sat, 9 Dec 2017 22:37:56 +0000 Subject: [PATCH 5/5] Improved Form unit tests --- src/Forms/Form.php | 2 - tests/unit/Forms/ExampleForm.php | 1 + tests/unit/Forms/FormTest.php | 76 +++++++++++++++++++++++++++++++- 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/src/Forms/Form.php b/src/Forms/Form.php index cc56c10..e08cdc4 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -130,8 +130,6 @@ public function __get(string $name) return $this->form_errors; } - - return parent::__get($name); } /** diff --git a/tests/unit/Forms/ExampleForm.php b/tests/unit/Forms/ExampleForm.php index eb7c77c..2ed6f2a 100644 --- a/tests/unit/Forms/ExampleForm.php +++ b/tests/unit/Forms/ExampleForm.php @@ -17,6 +17,7 @@ protected static function setFields() "title" => new CharField(["required" => true]), "description" => new CharField(["widget" => Textarea::class, "max_length" => 10]), "email" => new EmailField(), + "disabled" => new CharField(["disabled" => true]), ); } diff --git a/tests/unit/Forms/FormTest.php b/tests/unit/Forms/FormTest.php index a74f30c..93aa730 100644 --- a/tests/unit/Forms/FormTest.php +++ b/tests/unit/Forms/FormTest.php @@ -48,12 +48,19 @@ public function testFieldObjectAccess() $this->assertInstanceOf(BoundField::class, $form['email']); } - public function testErrors() + public function testGetErrors() { $form = new ExampleForm(); $this->assertEmpty($form->errors); } + public function testGetNotDefinedAttribute() + { + $form = new ExampleForm(); + + $this->assertNull($form->undefined); + } + public function testErrorsOfBoundedForm() { $form = new ExampleForm(["data" => array()]); @@ -78,7 +85,7 @@ public function testClean() $this->assertEquals(array("Error on title"), (array) $form->getFieldErrors("title")); } - public function testCleanWithTowAddError() + public function testCleanWithTwoAddError() { $form = new ExampleForm(["data" => array("title" => "Title2")]); $this->assertEquals(array("Error on title 1", "Error on title 2"), (array) $form->getFieldErrors("title")); @@ -102,6 +109,38 @@ public function testCleanEmail() $this->assertEquals(array("Error Processing Email"), (array) $form->getFieldErrors("email")); } + public function testGetCleanedData() + { + $form = new ExampleForm(); + $this->assertEmpty($form->getCleanedData()); + } + + public function testGetCleanedField() + { + $form = new ExampleForm(["data" => array("title" => "title")]); + $form->isValid(); // force validation + + $this->assertEquals("title", $form->getCleanedField("title")); + $this->assertEquals("", $form->getCleanedField("email")); + $this->assertNull($form->getCleanedField("unexistent")); + } + + public function testGetNonFieldErrorsEmpty() + { + $form = new ExampleForm([]); + $result = $form->getNonFieldErrors(); + + $this->assertEquals(0, count($result)); + } + + public function testGetNonFieldErrors() + { + $form = new ExampleForm(["data" => array("email" => "test@unit.c")]); + $result = $form->getNonFieldErrors(); + + $this->assertEquals(1, count($result)); + } + public function testIsValidWithInvalidForm() { $form = new ExampleForm(["data" => array()]); @@ -119,4 +158,37 @@ public function testIsValidWithNoDataBounded() $form = new ExampleForm(); $this->assertFalse($form->IsValid()); } + + /** + * @expectedException UnexpectedValueException + * @expectedExceptionMessage Field 'unexistent' not found in PHPForm\Unit\Forms\ExampleForm. + * Choices are: title, description, email, disabled + */ + public function testOffsetGetUnexistentField() + { + $form = new ExampleForm(); + $form["unexistent"]; + } + + public function testOffsetExists() + { + $form = new ExampleForm(); + + $this->assertTrue(isset($form["disabled"])); + $this->assertFalse(isset($form["unexistent"])); + } + + public function testOffsetUnset() + { + $form = new ExampleForm(); + unset($form["disabled"]); + + $this->assertFalse(isset($form["disabled"])); + } + + public function testCount() + { + $form = new ExampleForm(); + $this->assertEquals(4, count($form)); + } }