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/
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/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/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 = '
';
- $this->assertEquals((string) $error, $expected);
+ $this->assertEquals($expected, (string) $error);
+ }
+
+ public function testToStringEmptyErrorList()
+ {
+ $error = new ErrorList();
+ $this->assertEquals('', (string) $error);
}
}
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()
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));
+ }
}
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));
+ }
+}