Skip to content

Commit 65649f3

Browse files
committed
Fixed unit tests
1 parent f19129b commit 65649f3

File tree

8 files changed

+98
-124
lines changed

8 files changed

+98
-124
lines changed

src/Fields/CharField.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,22 @@ class CharField extends Field
1212
{
1313
protected $widget = TextInput::class;
1414

15-
/**
16-
* @var int Max value length.
17-
*/
18-
protected $max_length;
19-
20-
/**
21-
* @var int Min value length.
22-
*/
23-
protected $min_length;
24-
2515
/**
2616
* Constructor with extra args min_length and max_length
2717
*
2818
* @param array
2919
*/
3020
public function __construct(array $args = array())
3121
{
22+
$this->min_length = array_key_exists('min_length', $args) ? $args['min_length'] : null;
23+
$this->max_length = array_key_exists('max_length', $args) ? $args['max_length'] : null;
24+
3225
parent::__construct($args);
3326

34-
if (isset($this->min_length)) {
27+
if (!is_null($this->min_length)) {
3528
$this->validators[] = new MinLengthValidator($this->min_length);
3629
}
37-
if (isset($this->max_length)) {
30+
if (!is_null($this->max_length)) {
3831
$this->validators[] = new MaxLengthValidator($this->max_length);
3932
}
4033
}
@@ -49,10 +42,10 @@ public function widgetAttrs($widget)
4942
{
5043
$attrs = parent::widgetAttrs($widget);
5144

52-
if (isset($this->min_length) && !is_null($this->min_length)) {
45+
if (!is_null($this->min_length)) {
5346
$attrs['minlength'] = $this->min_length;
5447
}
55-
if (isset($this->max_length) && !is_null($this->max_length)) {
48+
if (!is_null($this->max_length)) {
5649
$attrs['maxlength'] = $this->max_length;
5750
}
5851

src/Fields/IntegerField.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,22 @@ class IntegerField extends Field
1313
{
1414
protected $widget = NumberInput::class;
1515

16-
/**
17-
* @var int Max value.
18-
*/
19-
protected $max_value;
20-
21-
/**
22-
* @var int Min value.
23-
*/
24-
protected $min_value;
25-
2616
protected $error_messages = array(
2717
'invalid' => 'Enter a whole number.'
2818
);
2919

3020

3121
public function __construct(array $args = array())
3222
{
23+
$this->min_value = array_key_exists('min_value', $args) ? $args['min_value'] : null;
24+
$this->max_value = array_key_exists('max_value', $args) ? $args['max_value'] : null;
25+
3326
parent::__construct($args);
3427

35-
if (isset($this->min_value)) {
28+
if (!is_null($this->min_value)) {
3629
$this->validators[] = new MinValueValidator($this->min_value);
3730
}
38-
if (isset($this->max_value)) {
31+
if (!is_null($this->max_value)) {
3932
$this->validators[] = new MaxValueValidator($this->max_value);
4033
}
4134
}
@@ -53,10 +46,10 @@ public function widgetAttrs($widget)
5346
{
5447
$attrs = parent::widgetAttrs($widget);
5548

56-
if (isset($this->min_value) && !is_null($this->min_value)) {
49+
if (!is_null($this->min_value)) {
5750
$attrs['min'] = $this->min_value;
5851
}
59-
if (isset($this->max_value) && !is_null($this->max_value)) {
52+
if (!is_null($this->max_value)) {
6053
$attrs['max'] = $this->max_value;
6154
}
6255

tests/unit/Fields/BoundFieldTest.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testConstruct()
2828

2929
public function testConstructWithPrefix()
3030
{
31-
$form = $this->getMockForAbstractClass(Form::class, array(null, null, "form"));
31+
$form = $this->getMockForAbstractClass(Form::class, array(["prefix" => "form"]));
3232
$bound = new BoundField($form, $this->simple_field, "name");
3333

3434
$this->assertAttributeEquals("form-name", "html_name", $bound);
@@ -66,17 +66,22 @@ public function testSimpleGet()
6666

6767
public function testToString()
6868
{
69-
$form = $this->getMockForAbstractClass(Form::class, array(array("name" => "value")));
69+
$form_args = ["data" => ["name" => "value"]];
70+
$form = $this->getMockForAbstractClass(Form::class, array($form_args));
71+
7072
$bound = new BoundField($form, $this->simple_field, "name");
73+
7174
$expected = '<input type="text" id="id_name" name="name" value="value"/>';
7275
$this->assertXmlStringEqualsXmlString((string) $bound, $expected);
7376
}
7477

7578
public function testToStringWithPrefix()
7679
{
77-
$data = array("prefix-name" => "value");
78-
$form = $this->getMockForAbstractClass(Form::class, array($data, null, "prefix"));
80+
$form_args = ["data" => ["prefix-name" => "value"], "prefix" => "prefix"];
81+
$form = $this->getMockForAbstractClass(Form::class, array($form_args));
82+
7983
$bound = new BoundField($form, $this->simple_field, "name");
84+
8085
$expected = '<input type="text" id="id_prefix-name" name="prefix-name" value="value"/>';
8186
$this->assertXmlStringEqualsXmlString((string) $bound, $expected);
8287
}
@@ -91,9 +96,13 @@ public function testLabelTag()
9196

9297
public function testLabelTagWithPrefix()
9398
{
94-
$form = $this->getMockForAbstractClass(Form::class, array(null, null, "prefix"));
99+
$form_args = ["prefix" => "prefix"];
100+
$form = $this->getMockForAbstractClass(Form::class, array($form_args));
101+
95102
$field = new CharField(array("label" => "Label"));
103+
96104
$bound = new BoundField($form, $field, "name");
105+
97106
$expected = '<label for="id_prefix-name">Label</label>';
98107
$this->assertXmlStringEqualsXmlString($bound->labelTag(), $expected);
99108
}

tests/unit/Fields/CharFieldTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
namespace PHPForm\Unit\Fields;
3+
4+
use PHPUnit\Framework\TestCase;
5+
6+
use PHPForm\Exceptions\ValidationError;
7+
use PHPForm\Fields\CharField;
8+
use PHPForm\Widgets\TextInput;
9+
10+
class CharFieldTest extends TestCase
11+
{
12+
public function setUp()
13+
{
14+
$this->field = new CharField(["min_length" => 10, "max_length" => 20]);
15+
}
16+
17+
public function testConstruct()
18+
{
19+
$this->assertInstanceOf(TextInput::class, $this->field->getWidget());
20+
}
21+
22+
public function testConstructWithValidators()
23+
{
24+
$this->assertAttributeEquals(10, "min_length", $this->field);
25+
$this->assertAttributeEquals(20, "max_length", $this->field);
26+
}
27+
28+
public function testValidatorNoException()
29+
{
30+
$this->assertEquals($this->field->clean("12345678910"), "12345678910");
31+
}
32+
33+
/**
34+
* @expectedException PHPForm\Exceptions\ValidationError
35+
*/
36+
public function testMinValidator()
37+
{
38+
$this->field->clean("12345");
39+
}
40+
41+
/**
42+
* @expectedException PHPForm\Exceptions\ValidationError
43+
*/
44+
public function testMaxValidator()
45+
{
46+
$this->field->clean("123456789123456789123456789");
47+
}
48+
49+
public function testWidgetAttrs()
50+
{
51+
$expected = array("minlength" => 10, "maxlength" => 20);
52+
$this->assertEquals($expected, $this->field->widgetAttrs(null));
53+
}
54+
}

tests/unit/Fields/FieldTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99

1010
class FieldTest extends TestCase
1111
{
12-
/**
13-
* @expectedException InvalidArgumentException
14-
*/
15-
public function testConstructWithInvalidArgument()
16-
{
17-
$this->getMockForAbstractClass(Field::class, array(array("a" => true)));
18-
}
19-
2012
public function testConstruct()
2113
{
2214
$args = array("label" => "label", "required" => true);

tests/unit/Fields/IntegerFieldTest.php

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,41 @@
99

1010
class IntegerFieldTest extends TestCase
1111
{
12+
public function setUp()
13+
{
14+
$this->field = new IntegerField(["min_value" => 10, "max_value" => 40]);
15+
}
16+
1217
public function testConstruct()
1318
{
14-
$field = new IntegerField();
15-
$this->assertInstanceOf(NumberInput::class, $field->getWidget());
19+
$this->assertInstanceOf(NumberInput::class, $this->field->getWidget());
1620
}
1721

1822
public function testConstructWithValidators()
1923
{
20-
$args = array("min_value" => 10, "max_value" => 40);
21-
$field = new IntegerField($args);
22-
$this->assertAttributeEquals(10, "min_value", $field);
23-
$this->assertAttributeEquals(40, "max_value", $field);
24+
$this->assertAttributeEquals(10, "min_value", $this->field);
25+
$this->assertAttributeEquals(40, "max_value", $this->field);
2426
}
2527

2628
public function testValidatorNoException()
2729
{
28-
$args = array("min_value" => 10, "max_value" => 40);
29-
$field = new IntegerField($args);
30-
$this->assertEquals($field->clean("25"), 25);
30+
$this->assertEquals($this->field->clean("25"), 25);
3131
}
3232

3333
/**
3434
* @expectedException PHPForm\Exceptions\ValidationError
3535
*/
3636
public function testMinValidatorError()
3737
{
38-
$args = array("min_value" => 10, "max_value" => 40);
39-
$field = new IntegerField($args);
40-
$field->clean("1");
38+
$this->field->clean("1");
4139
}
4240

4341
/**
4442
* @expectedException PHPForm\Exceptions\ValidationError
4543
*/
4644
public function testMaxValidatorError()
4745
{
48-
$args = array("min_value" => 10, "max_value" => 20);
49-
$field = new IntegerField($args);
50-
$field->clean("100");
46+
$this->field->clean("100");
5147
}
5248

5349
/**
@@ -56,16 +52,12 @@ public function testMaxValidatorError()
5652
*/
5753
public function testInvalidValue()
5854
{
59-
$field = new IntegerField();
60-
$field->clean("aa");
55+
$this->field->clean("aa");
6156
}
6257

6358
public function testWidgetAttrs()
6459
{
65-
$args = array("min_value" => 10, "max_value" => 40);
66-
$field = new IntegerField($args);
67-
6860
$expected = array("min" => 10, "max" => 40);
69-
$this->assertEquals($field->widgetAttrs(null), $expected);
61+
$this->assertEquals($this->field->widgetAttrs(null), $expected);
7062
}
7163
}

tests/unit/Fields/TextFieldTest.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

tests/unit/Forms/FormTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public function testConstructor()
2020

2121
public function testConstructorWithBoundedForm()
2222
{
23-
$form = new ExampleForm(['data' => ["description" => "Description"]]);
23+
$data = ["description" => "Description"];
24+
$form = new ExampleForm(['data' => $data]);
25+
2426
$this->assertAttributeEquals(true, "is_bound", $form);
2527
$this->assertAttributeEquals($data, "data", $form);
2628
}

0 commit comments

Comments
 (0)