Skip to content

Commit 45064de

Browse files
authored
Merge pull request #6 from dipcode-software/feat/unittests
Feat/unittests
2 parents 2bc75b3 + 10825a2 commit 45064de

File tree

9 files changed

+163
-12
lines changed

9 files changed

+163
-12
lines changed

phpcs.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44

55
<!-- Scan these files -->
66
<file>src</file>
7+
<file>tests</file>
78

89
<!-- Show colors in console -->
910
<arg value="-colors"/>
10-
1111
<!-- Show sniff codes in all reports -->
1212
<arg value="s"/>
13-
1413
<!-- Use PSR-2 as a base -->
1514
<rule ref="PSR2"/>
16-
1715
</ruleset>

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
convertWarningsToExceptions="true"
88
processIsolation="false">
99
<testsuites>
10-
<testsuite name="Application Test Suite">
10+
<testsuite name="PHPForm Test Suite">
1111
<directory suffix=".php">./tests/</directory>
1212
</testsuite>
1313
</testsuites>

src/Fields/BoundField.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ public function __get($name)
6060
}
6161
return $options_cache;
6262
}
63-
64-
return null;
6563
}
6664

6765
private function getOptions(array $attrs = array())

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/Errors/ErrorListTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ public function testToString()
1111
{
1212
$error = new ErrorList(array(1, 2, 3));
1313
$expected = '<ul class="errorlist"><li>1</li><li>2</li><li>3</li></ul>';
14-
$this->assertEquals((string) $error, $expected);
14+
$this->assertEquals($expected, (string) $error);
15+
}
16+
17+
public function testToStringEmptyErrorList()
18+
{
19+
$error = new ErrorList();
20+
$this->assertEquals('', (string) $error);
1521
}
1622
}

tests/unit/Fields/BoundFieldTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,42 @@ public function testGetErrors()
5656
{
5757
$form = $this->getMockForAbstractClass(Form::class);
5858
$bound = new BoundField($form, $this->simple_field, "name");
59+
5960
$this->assertInstanceOf(ErrorList::class, $bound->errors);
6061
}
6162

62-
public function testHasErrors()
63+
public function testGetHasErrors()
6364
{
6465
$form = $this->getMockForAbstractClass(Form::class);
6566
$bound = new BoundField($form, $this->simple_field, "name");
67+
6668
$this->assertFalse($bound->has_errors);
6769
}
6870

71+
public function testGetIsRequired()
72+
{
73+
$form = $this->getMockForAbstractClass(Form::class);
74+
$bound = new BoundField($form, $this->simple_field, "name");
75+
76+
$this->assertFalse($bound->is_required);
77+
}
78+
79+
public function testGetValue()
80+
{
81+
$form = $this->getMockForAbstractClass(Form::class);
82+
$bound = new BoundField($form, $this->simple_field, "name");
83+
84+
$this->assertNull($bound->value);
85+
}
86+
87+
public function testGetNotDefinedAttribute()
88+
{
89+
$form = $this->getMockForAbstractClass(Form::class);
90+
$bound = new BoundField($form, $this->simple_field, "name");
91+
92+
$this->assertNull($bound->undefined);
93+
}
94+
6995
public function testSimpleGet()
7096
{
7197
$form = $this->getMockForAbstractClass(Form::class);
@@ -123,12 +149,26 @@ public function testToStringWithErrors()
123149
$this->assertXmlStringEqualsXmlString($expected, (string) $bound);
124150
}
125151

152+
public function testToStringWithFieldRequired()
153+
{
154+
$form = $this->getMockForAbstractClass(Form::class);
155+
$field = new CharField(['disabled' => true]);
156+
157+
$bound = new BoundField($form, $field, "name");
158+
159+
$expected = '<input type="text" id="id_name" name="name" disabled="disabled"/>';
160+
$this->assertXmlStringEqualsXmlString($expected, (string) $bound);
161+
}
162+
126163
public function testLabelTag()
127164
{
128165
$field = new CharField(array("label" => "Label"));
129166
$bound = new BoundField($this->simple_form, $field, "name");
167+
130168
$expected = '<label for="id_name">Label</label>';
169+
131170
$this->assertXmlStringEqualsXmlString($bound->labelTag(), $expected);
171+
$this->assertXmlStringEqualsXmlString($bound->label_tag, $expected);
132172
}
133173

134174
public function testLabelTagWithPrefix()

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
}

tests/unit/Widgets/WidgetTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace PHPForm\Unit\Widgets;
3+
4+
use PHPUnit\Framework\TestCase;
5+
6+
use PHPForm\Widgets\Widget;
7+
8+
class WidgetTest extends TestCase
9+
{
10+
public function setUp()
11+
{
12+
$this->widget = $this->getMockForAbstractClass(Widget::class);
13+
}
14+
15+
public function testGetOptions()
16+
{
17+
$options = $this->widget->getOptions("name", "value");
18+
$this->assertEquals(array(), $options);
19+
}
20+
21+
public function testValueFromData()
22+
{
23+
$result = $this->widget->valueFromData(array("name" => "value"), array(), "name");
24+
$this->assertEquals("value", $result);
25+
}
26+
27+
public function testValueFromDataInexistent()
28+
{
29+
$result = $this->widget->valueFromData(array(), array(), "name");
30+
$this->assertNull($result);
31+
}
32+
33+
public function testBuildAutoId()
34+
{
35+
$this->assertEquals("id_name", $this->widget->buildAutoId("name"));
36+
$this->assertEquals("id_name_1", $this->widget->buildAutoId("name", 1));
37+
}
38+
}

0 commit comments

Comments
 (0)