Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

<!-- Scan these files -->
<file>src</file>
<file>tests</file>

<!-- Show colors in console -->
<arg value="-colors"/>

<!-- Show sniff codes in all reports -->
<arg value="s"/>

<!-- Use PSR-2 as a base -->
<rule ref="PSR2"/>

</ruleset>
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
convertWarningsToExceptions="true"
processIsolation="false">
<testsuites>
<testsuite name="Application Test Suite">
<testsuite name="PHPForm Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
Expand Down
2 changes: 0 additions & 2 deletions src/Fields/BoundField.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ public function __get($name)
}
return $options_cache;
}

return null;
}

private function getOptions(array $attrs = array())
Expand Down
2 changes: 0 additions & 2 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ public function __get(string $name)

return $this->form_errors;
}

return parent::__get($name);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/Errors/ErrorListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public function testToString()
{
$error = new ErrorList(array(1, 2, 3));
$expected = '<ul class="errorlist"><li>1</li><li>2</li><li>3</li></ul>';
$this->assertEquals((string) $error, $expected);
$this->assertEquals($expected, (string) $error);
}

public function testToStringEmptyErrorList()
{
$error = new ErrorList();
$this->assertEquals('', (string) $error);
}
}
42 changes: 41 additions & 1 deletion tests/unit/Fields/BoundFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 = '<input type="text" id="id_name" name="name" disabled="disabled"/>';
$this->assertXmlStringEqualsXmlString($expected, (string) $bound);
}

public function testLabelTag()
{
$field = new CharField(array("label" => "Label"));
$bound = new BoundField($this->simple_form, $field, "name");

$expected = '<label for="id_name">Label</label>';

$this->assertXmlStringEqualsXmlString($bound->labelTag(), $expected);
$this->assertXmlStringEqualsXmlString($bound->label_tag, $expected);
}

public function testLabelTagWithPrefix()
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Forms/ExampleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
);
}

Expand Down
76 changes: 74 additions & 2 deletions tests/unit/Forms/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]);
Expand All @@ -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"));
Expand All @@ -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()]);
Expand All @@ -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));
}
}
38 changes: 38 additions & 0 deletions tests/unit/Widgets/WidgetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace PHPForm\Unit\Widgets;

use PHPUnit\Framework\TestCase;

use PHPForm\Widgets\Widget;

class WidgetTest extends TestCase
{
public function setUp()
{
$this->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));
}
}