Skip to content

Commit 10825a2

Browse files
committed
Improved Form unit tests
1 parent 18ae45c commit 10825a2

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

src/Forms/Form.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ public function __get(string $name)
130130

131131
return $this->form_errors;
132132
}
133-
134-
return parent::__get($name);
135133
}
136134

137135
/**

tests/unit/Forms/ExampleForm.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ protected static function setFields()
1717
"title" => new CharField(["required" => true]),
1818
"description" => new CharField(["widget" => Textarea::class, "max_length" => 10]),
1919
"email" => new EmailField(),
20+
"disabled" => new CharField(["disabled" => true]),
2021
);
2122
}
2223

tests/unit/Forms/FormTest.php

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,19 @@ public function testFieldObjectAccess()
4848
$this->assertInstanceOf(BoundField::class, $form['email']);
4949
}
5050

51-
public function testErrors()
51+
public function testGetErrors()
5252
{
5353
$form = new ExampleForm();
5454
$this->assertEmpty($form->errors);
5555
}
5656

57+
public function testGetNotDefinedAttribute()
58+
{
59+
$form = new ExampleForm();
60+
61+
$this->assertNull($form->undefined);
62+
}
63+
5764
public function testErrorsOfBoundedForm()
5865
{
5966
$form = new ExampleForm(["data" => array()]);
@@ -78,7 +85,7 @@ public function testClean()
7885
$this->assertEquals(array("Error on title"), (array) $form->getFieldErrors("title"));
7986
}
8087

81-
public function testCleanWithTowAddError()
88+
public function testCleanWithTwoAddError()
8289
{
8390
$form = new ExampleForm(["data" => array("title" => "Title2")]);
8491
$this->assertEquals(array("Error on title 1", "Error on title 2"), (array) $form->getFieldErrors("title"));
@@ -102,6 +109,38 @@ public function testCleanEmail()
102109
$this->assertEquals(array("Error Processing Email"), (array) $form->getFieldErrors("email"));
103110
}
104111

112+
public function testGetCleanedData()
113+
{
114+
$form = new ExampleForm();
115+
$this->assertEmpty($form->getCleanedData());
116+
}
117+
118+
public function testGetCleanedField()
119+
{
120+
$form = new ExampleForm(["data" => array("title" => "title")]);
121+
$form->isValid(); // force validation
122+
123+
$this->assertEquals("title", $form->getCleanedField("title"));
124+
$this->assertEquals("", $form->getCleanedField("email"));
125+
$this->assertNull($form->getCleanedField("unexistent"));
126+
}
127+
128+
public function testGetNonFieldErrorsEmpty()
129+
{
130+
$form = new ExampleForm([]);
131+
$result = $form->getNonFieldErrors();
132+
133+
$this->assertEquals(0, count($result));
134+
}
135+
136+
public function testGetNonFieldErrors()
137+
{
138+
$form = new ExampleForm(["data" => array("email" => "test@unit.c")]);
139+
$result = $form->getNonFieldErrors();
140+
141+
$this->assertEquals(1, count($result));
142+
}
143+
105144
public function testIsValidWithInvalidForm()
106145
{
107146
$form = new ExampleForm(["data" => array()]);
@@ -119,4 +158,37 @@ public function testIsValidWithNoDataBounded()
119158
$form = new ExampleForm();
120159
$this->assertFalse($form->IsValid());
121160
}
161+
162+
/**
163+
* @expectedException UnexpectedValueException
164+
* @expectedExceptionMessage Field 'unexistent' not found in PHPForm\Unit\Forms\ExampleForm.
165+
* Choices are: title, description, email, disabled
166+
*/
167+
public function testOffsetGetUnexistentField()
168+
{
169+
$form = new ExampleForm();
170+
$form["unexistent"];
171+
}
172+
173+
public function testOffsetExists()
174+
{
175+
$form = new ExampleForm();
176+
177+
$this->assertTrue(isset($form["disabled"]));
178+
$this->assertFalse(isset($form["unexistent"]));
179+
}
180+
181+
public function testOffsetUnset()
182+
{
183+
$form = new ExampleForm();
184+
unset($form["disabled"]);
185+
186+
$this->assertFalse(isset($form["disabled"]));
187+
}
188+
189+
public function testCount()
190+
{
191+
$form = new ExampleForm();
192+
$this->assertEquals(4, count($form));
193+
}
122194
}

0 commit comments

Comments
 (0)